Skip to content

Commit

Permalink
Merge pull request #18 from Arkoniak/upgrade_03
Browse files Browse the repository at this point in the history
Upgrade 03
  • Loading branch information
Arkoniak authored Aug 7, 2021
2 parents b77e08b + b06504a commit 7d48e61
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 25 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
name = "MiniLoggers"
uuid = "93f3dd0f-005d-4452-894a-a31841fa4078"
authors = ["Andrey Oskin"]
version = "0.2.6"
version = "0.3.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"

[compat]
julia = "1"
Expand Down
72 changes: 51 additions & 21 deletions src/minilogger.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
struct MiniLogger{IOT1 <: IO, IOT2 <: IO, DFT <: DateFormat} <: AbstractLogger
abstract type AbstractMode end
struct NoTransformations <: AbstractMode end
struct Squash <: AbstractMode end

struct MiniLogger{AM <: AbstractMode, IOT1 <: IO, IOT2 <: IO, DFT <: DateFormat} <: AbstractLogger
io::IOT1
ioerr::IOT2
errlevel::LogLevel
Expand All @@ -7,7 +11,7 @@ struct MiniLogger{IOT1 <: IO, IOT2 <: IO, DFT <: DateFormat} <: AbstractLogger
flush::Bool
format::Vector{Token}
dtformat::DFT
squash_message::Bool
mode::AM
flush_threshold::Int
lastflush::Base.RefValue{Int}
end
Expand All @@ -18,6 +22,16 @@ getio(io::AbstractString, append) = open(io, append ? "a" : "w")
getflushthreshold(x::Integer) = x
getflushthreshold(x::TimePeriod) = Dates.value(Millisecond(x))

getmode(mode) = mode
getmode(mode::AbstractString) = getmode(Symbol(mode))
function getmode(mode::Symbol)
if mode == :notransformations
return NoTransformations()
elseif mode == :squash
return Squash()
end
end

function Base.close(logger::MiniLogger)
if logger.io != stdout && logger.io != stderr && isopen(logger.io)
close(logger.io)
Expand All @@ -40,12 +54,14 @@ Supported keyword arguments include:
* `errlevel` (default `Error`): determines which output IO to use for log messages. If you want for all messages to go to `io`, set this parameter to `MiniLoggers.AboveMaxLevel`. If you want for all messages to go to `ioerr`, set this parameter to `MiniLoggers.BelowMinLevel`.
* `minlevel` (default: `Info`): messages below this level are ignored. For example with default setting `@debug "foo"` is ignored.
* `append` (default: `false`): defines whether to append to output stream or to truncate file initially. Used only if `io` or `ioerr` is a file path.
* `squash_message` (default: `true`): if `squash_message` is set to `true`, then message is squashed to a single line, i.e. all `\\n` are changed to ` ` and `\\r` are removed.
* `message_mode` (default: `:squash`): choose how message is transformed before being printed out. Following modes are supported:
* `:notransformations`: message printed out as is, without any extra transformations
* `:squash`: message is squashed to a single line, i.e. all `\\n` are changed to ` ` and `\\r` are removed.
* `flush` (default: `true`): whether to `flush` IO stream for each log message. Flush behaviour also affected by `flush_threshold` argument.
* `flush_threshold::Union{Integer, TimePeriod}` (default: 0): if this argument is nonzero and `flush` is `true`, then `io` is flushed only once per `flush_threshold` milliseconds. I.e. if time between two consecutive log messages is less then `flush_threshold`, then second message is not flushed and will have to wait for the next log event.
* `dtformat` (default: "yyyy-mm-dd HH:MM:SS"): if `datetime` parameter is used in `format` argument, this dateformat is applied for output timestamps.
* `format` (default: "{[{datetime}]:func} {message}"): format for output log message. It accepts following keywords, which should be provided in curly brackets:
* `datetime`: timestamp of the log message
* `format` (default: "[{timestamp:func}] {level:func}: {message}"): format for output log message. It accepts following keywords, which should be provided in curly brackets:
* `timestamp`: timestamp of the log message
* `level`: name of log level (Debug, Info, etc)
* `filepath`: filepath of the file, which produced log message
* `basename`: basename of the filepath of the file, which produced log message
Expand All @@ -61,11 +77,21 @@ Colour information is applied recursively without override, so `{{line} {module:
If part of the format is not a recognised keyword, then it is just used as is, for example `{foo:red}` means that output log message contain word "foo" printed in red.
"""
function MiniLogger(; io = stdout, ioerr = stderr, errlevel = Error, minlevel = Info, append = false, message_limits = Dict{Any, Int}(), flush = true, format = "{[{datetime}]:func} {message}", dtformat = dateformat"yyyy-mm-dd HH:MM:SS", squash_message = true, flush_threshold = 0)
function MiniLogger(; io = stdout, ioerr = stderr, errlevel = Error, minlevel = Info, append = false, message_limits = Dict{Any, Int}(), flush = true, format = "[{timestamp:func}] {level:func}: {message}", dtformat = dateformat"yyyy-mm-dd HH:MM:SS", flush_threshold = 0, message_mode = Squash())
tio = getio(io, append)
tioerr = io == ioerr ? tio : getio(ioerr, append)
lastflush = Dates.value(Dates.now())
MiniLogger(tio, tioerr, errlevel, minlevel, message_limits, flush, tokenize(format), dtformat, squash_message, getflushthreshold(flush_threshold), Ref(lastflush))
MiniLogger(tio,
tioerr,
errlevel,
minlevel,
message_limits,
flush,
tokenize(format),
dtformat,
getmode(message_mode),
getflushthreshold(flush_threshold),
Ref(lastflush))
end

shouldlog(logger::MiniLogger, level, _module, group, id) =
Expand All @@ -84,20 +110,24 @@ end
showvalue(io, ex::Exception) = Base.showerror(io, ex)
showvalue(io, ex::AbstractVector{Union{Ptr{Nothing}, Base.InterpreterIP}}) = Base.show_backtrace(io, ex)

function showmessage(io, msg, squash)
if squash
msglines = split(chomp(string(msg)), '\n')
print(io, replace(msglines[1], "\r" => ""))
for i in 2:length(msglines)
print(io, " ", replace(msglines[i], "\r" => ""))
end
else
print(io, msg)
# Here we are fighting with multiple dispatch.
# If message is `Exception` or `Tuple{Exception, Any}` or anything else
# then we want to ignore third argument.
# But if it is any other sort of message we want to dispatch result
# on the type of message transformation
function _showmessage(io, msg, ::Squash)
msglines = split(chomp(string(msg)), '\n')
print(io, replace(msglines[1], "\r" => ""))
for i in 2:length(msglines)
print(io, " ", replace(msglines[i], "\r" => ""))
end
end
showmessage(io, e::Tuple{Exception,Any}, squash) = showvalue(io, e)
showmessage(io, ex::Exception, squash) = showvalue(io, e)
showmessage(io, ex::AbstractVector{Union{Ptr{Nothing}, Base.InterpreterIP}}, squash) = Base.show_backtrace(io, ex)
_showmessage(io, msg, ::NoTransformations) = print(io, msg)

showmessage(io, msg, mode) = _showmessage(io, msg, mode)
showmessage(io, e::Tuple{Exception,Any}, _) = showvalue(io, e)
showmessage(io, ex::Exception, _) = showvalue(io, ex)
showmessage(io, ex::AbstractVector{Union{Ptr{Nothing}, Base.InterpreterIP}}, _) = Base.show_backtrace(io, ex)

tsnow(dtf) = Dates.format(Dates.now(), dtf)

Expand Down Expand Up @@ -149,10 +179,10 @@ function handle_message(logger::MiniLogger, level, message, _module, group, id,
for token in logger.format
c = extractcolor(token, level, _module, group, id, filepath, line)
val = token.val
if val == "datetime"
if val == "timestamp"
printwcolor(iob, tsnow(logger.dtformat), c)
elseif val == "message"
showmessage(iob, message, logger.squash_message)
showmessage(iob, message, logger.mode)

iscomma = false
for (key, val) in kwargs
Expand Down
22 changes: 19 additions & 3 deletions test/test02_loggerformat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ conts(s, sub) = match(Regex(sub), s) !== nothing
@testset "basic format" begin
io = IOBuffer()
logger = MiniLogger(io = io, minlevel = MiniLoggers.Debug,
format = "{datetime}:{level}:{module}:{basename}:{filepath}:{line}:{group}:{module}:{id}:{message}")
format = "{timestamp}:{level}:{module}:{basename}:{filepath}:{line}:{group}:{module}:{id}:{message}")
with_logger(logger) do
@info "foo"
s = String(take!(io))
@test !conts(s, "datetime")
@test !conts(s, "timestamp")
@test !conts(s, "level")
@test !conts(s, "module")
@test !conts(s, "basename")
Expand All @@ -36,7 +36,7 @@ end
@test s == "foo bar\n"
end

logger = MiniLogger(io = io, format = "{message}", squash_message = false)
logger = MiniLogger(io = io, format = "{message}", message_mode = :notransformations)
with_logger(logger) do
@info "foo\nbar"

Expand Down Expand Up @@ -92,6 +92,22 @@ end

s = String(take!(io))
@test conts(s, "^foo exception = ERROR\n *Stacktrace")

try
error("ERROR")
catch err
@error err
end
s = String(take!(io))
@test s == "ERROR\n"

try
error("ERROR")
catch err
@error catch_backtrace()
end
s = String(take!(io))
@test conts(s, "^\n *Stacktrace")
end
end

Expand Down
9 changes: 9 additions & 0 deletions test/test03_misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module MiscTest

using ReTest
using MiniLoggers
using MiniLoggers: getmode, NoTransformations, Squash

using Dates

@testset "flush" begin
Expand Down Expand Up @@ -97,4 +99,11 @@ end
end
end

@testset "modes" begin
@test getmode("notransformations") isa NoTransformations
@test getmode("squash") isa Squash
@test getmode(:notransformations) isa NoTransformations
@test getmode(:squash) isa Squash
end

end # module

2 comments on commit 7d48e61

@Arkoniak
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/42372

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.0 -m "<description of version>" 7d48e6129102527abc4f6d719b606b1cd5e44a3d
git push origin v0.3.0

Please sign in to comment.