Skip to content

Commit 1e6b64f

Browse files
author
Andrey Oskin
committed
added markdown support
1 parent cc232be commit 1e6b64f

File tree

6 files changed

+21
-3
lines changed

6 files changed

+21
-3
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MiniLoggers"
22
uuid = "93f3dd0f-005d-4452-894a-a31841fa4078"
33
authors = ["Andrey Oskin"]
4-
version = "0.3.0"
4+
version = "0.3.1"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ In it's most simple form, `MiniLoggers.jl` is used as
2020
```julia
2121
using MiniLoggers
2222

23-
MiniLogger(minlevel = MiniLogger.Debug) |> global_logger
23+
MiniLogger(minlevel = MiniLoggers.Debug) |> global_logger
2424

2525
x = 1
2626
y = "asd"
@@ -33,7 +33,7 @@ y = "asd"
3333

3434
and produces
3535

36-
![default_fmt](images/default_fmt.png)
36+
![default_fmt](images/default_fmt2.png)
3737

3838
But one can make it more colourful and add more details with initilization like the following
3939

images/default_fmt2.png

35.8 KB
Loading

src/MiniLoggers.jl

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module MiniLoggers
22

33
using Dates
4+
using Markdown
45
import Logging: AbstractLogger, shouldlog, min_enabled_level, catch_exceptions, handle_message, LogLevel
56
using Logging: Warn, Info, Debug, Error, BelowMinLevel, AboveMaxLevel, global_logger, with_logger, default_logcolor
67

src/minilogger.jl

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
abstract type AbstractMode end
22
struct NoTransformations <: AbstractMode end
33
struct Squash <: AbstractMode end
4+
struct MDown <: AbstractMode end
45

56
struct MiniLogger{AM <: AbstractMode, IOT1 <: IO, IOT2 <: IO, DFT <: DateFormat} <: AbstractLogger
67
io::IOT1
@@ -29,6 +30,8 @@ function getmode(mode::Symbol)
2930
return NoTransformations()
3031
elseif mode == :squash
3132
return Squash()
33+
elseif mode == :markdown
34+
return MDown()
3235
end
3336
end
3437

@@ -57,6 +60,7 @@ Supported keyword arguments include:
5760
* `message_mode` (default: `:squash`): choose how message is transformed before being printed out. Following modes are supported:
5861
* `:notransformations`: message printed out as is, without any extra transformations
5962
* `:squash`: message is squashed to a single line, i.e. all `\\n` are changed to ` ` and `\\r` are removed.
63+
* `:markdown`: message is treated as if it is written in markdown
6064
* `flush` (default: `true`): whether to `flush` IO stream for each log message. Flush behaviour also affected by `flush_threshold` argument.
6165
* `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.
6266
* `dtformat` (default: "yyyy-mm-dd HH:MM:SS"): if `datetime` parameter is used in `format` argument, this dateformat is applied for output timestamps.
@@ -122,6 +126,7 @@ function _showmessage(io, msg, ::Squash)
122126
print(io, " ", replace(msglines[i], "\r" => ""))
123127
end
124128
end
129+
_showmessage(io, msg, ::MDown) = show(io, MIME"text/plain"(), Markdown.parse(msg))
125130
_showmessage(io, msg, ::NoTransformations) = print(io, msg)
126131

127132
showmessage(io, msg, mode) = _showmessage(io, msg, mode)

test/test02_loggerformat.jl

+12
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ end
4545
end
4646
end
4747

48+
@testset "markdown support" begin
49+
io = IOBuffer()
50+
ioc = IOContext(io, :color => true)
51+
logger = MiniLogger(io = ioc, format = "{message}", message_mode = :markdown)
52+
with_logger(logger) do
53+
@info "**foo**"
54+
55+
s = String(take!(io))
56+
@test s == " \e[1mfoo\e[22m\n"
57+
end
58+
end
59+
4860
@testset "badcaret" begin
4961
io = IOBuffer()
5062
logger = MiniLogger(io = io, format = "{message}")

0 commit comments

Comments
 (0)