Skip to content

Commit ff5be48

Browse files
authored
Merge pull request #39 from Arkoniak/38-fix-quotes
fix: json quotes (#38)
2 parents 6b4c0d7 + ab2aa2b commit ff5be48

File tree

4 files changed

+74
-32
lines changed

4 files changed

+74
-32
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
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 and contributors"]
4-
version = "0.5.0"
4+
version = "0.5.1"
55

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

src/jsonlogger.jl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,30 +101,36 @@ function handle_message(logger::JsonLogger, level, message, _module, group, id,
101101
elseif val == "id"
102102
print(iob, "\"", id, "\"")
103103
elseif val == "message"
104-
print(iob, "\"")
105-
showmessage(iob, message, logger, logger.mode)
104+
mbuf = IOBuffer()
105+
miob = IOContext(mbuf, io)
106+
107+
showmessage(miob, message, logger, logger.mode)
106108
if length(kwargs) > 0 && !isempty(message)
107-
print(iob, " ")
109+
print(miob, " ")
108110
end
109111

110112
iscomma = false
111113
for (k, v) in kwargs
112114
if string(k) == v
113-
print(iob, k)
115+
print(miob, k)
114116
iscomma = false
115117
else
116-
iscomma && print(iob, ", ")
117-
print(iob, k, " = ")
118-
showvalue(iob, v, logger, logger.mode)
118+
iscomma && print(miob, ", ")
119+
print(miob, k, " = ")
120+
showvalue(miob, v, logger, logger.mode)
119121
iscomma = true
120122
end
121123
end
122124
print(iob, "\"")
125+
write(iob, postprocess(JsonSquash(), logger.squash_delimiter, mbuf))
126+
print(iob, "\"")
123127
else
124128
print(iob, val)
125129
end
126130
end
127-
write(io, postprocess(logger.mode, logger.squash_delimiter, buf))
131+
write(io, take!(buf))
132+
write(io, '\n')
133+
# write(io, postprocess(logger.mode, logger.squash_delimiter, buf))
128134

129135
if logger.flush
130136
if logger.flush_threshold <= 0

src/modes.jl

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ function squash(msg, logger, mode::Union{Squash, FullSquash})
1313
smsg = replace(smsg, "\n" => logger.squash_delimiter)
1414
end
1515

16-
function squash(msg, logger, mode::JsonSquash)
17-
smsg = string(msg)
18-
# Far from perfect, but should cover most cases. If we run into any issues, we can add more complicated processing
19-
smsg = replace(smsg, "\"" => "\\\"")
20-
smsg = replace(smsg, "\r" => "")
21-
smsg = replace(smsg, "\n" => logger.squash_delimiter)
22-
end
16+
# function squash(msg, logger, mode::JsonSquash)
17+
# smsg = string(msg)
18+
# # Far from perfect, but should cover most cases. If we run into any issues, we can add more complicated processing
19+
# smsg = replace(smsg, "\"" => "\\\"")
20+
# smsg = replace(smsg, "\r" => "")
21+
# smsg = replace(smsg, "\n" => logger.squash_delimiter)
22+
# end
2323

2424
showvalue(io, msg, logger, mode) = print(io, squash(msg, logger, mode))
2525

@@ -48,7 +48,7 @@ function postprocess(mode, delimiter, iobuf)
4848
take!(iobuf)
4949
end
5050

51-
function postprocess(mode::Union{FullSquash, JsonSquash}, delimiter, iobuf)
51+
function postprocess(mode::FullSquash, delimiter, iobuf)
5252
buf = take!(iobuf)
5353
delm = Vector{UInt8}(delimiter)
5454
res = similar(buf)
@@ -80,3 +80,25 @@ function postprocess(mode::Union{FullSquash, JsonSquash}, delimiter, iobuf)
8080

8181
return res
8282
end
83+
84+
function postprocess(mode::JsonSquash, delimiter, iobuf)
85+
buf0 = String(take!(iobuf))
86+
buf = IOBuffer()
87+
iob = IOContext(buf)
88+
@inbounds for c in buf0
89+
c == '\r' && continue
90+
if c == '\n'
91+
print(iob, delimiter)
92+
elseif c == '\\'
93+
print(iob, '\\')
94+
print(iob, '\\')
95+
elseif c == '\"'
96+
print(iob, '\\')
97+
print(iob, '\"')
98+
else
99+
print(iob, c)
100+
end
101+
end
102+
103+
return take!(buf)
104+
end

test/test04_jsonlogger.jl

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ comptokens(t1, t2) = all(x.val == y for (x, y) in zip(t1, t2))
1414

1515
@testset "comma separated" begin
1616
res1 = tokenize(JsonLoggerTokenizer(), "x,y")
17-
@show res1
1817
@test comptokens(res1, ["{", "\"x\"", ":", "x", ",", "\"y\"", ":", "y", "}"])
1918
end
2019

@@ -30,22 +29,37 @@ comptokens(t1, t2) = all(x.val == y for (x, y) in zip(t1, t2))
3029
end
3130

3231
@testset "JsonLogger printing" begin
33-
x = 1
34-
y = "asd"
35-
x1 = 2
36-
z = "z"
37-
38-
d = Dict("asd" => 1, 'a' => 0.34)
39-
40-
buf = IOBuffer()
41-
iob = IOContext(buf, stdout)
42-
JsonLogger(io = iob, format = tokenize(JsonLoggerTokenizer(), "severity:level,source:{line:level,level},message"), minlevel = MiniLoggers.Debug) |> global_logger
43-
begin
44-
@debug "V:" x y
32+
@testset "Simple logging" begin
33+
x = 1
34+
y = "asd"
35+
x1 = 2
36+
z = "z"
37+
38+
d = Dict("asd" => 1, 'a' => 0.34)
39+
40+
buf = IOBuffer()
41+
iob = IOContext(buf, stdout)
42+
JsonLogger(io = iob, format = tokenize(JsonLoggerTokenizer(), "severity:level,source:{line:level,level},message"), minlevel = MiniLoggers.Debug) |> global_logger
43+
begin
44+
@debug "V:" x y
45+
end
46+
47+
s = String(take!(buf))
48+
@test s == "{\"severity\":\"Debug\",\"source\":{\"line\":\"Debug\",\"level\":\"Debug\"},\"message\":\"V: x = 1, y = asd\"}\n"
4549
end
4650

47-
s = String(take!(buf))
48-
@test s == "{\"severity\":\"Debug\",\"source\":{\"line\":\"Debug\",\"level\":\"Debug\"},\"message\":\"V: x = 1, y = asd\"}\n"
51+
@testset "quotes escape" begin
52+
s = "\""
53+
buf = IOBuffer()
54+
iob = IOContext(buf, stdout)
55+
JsonLogger(io = iob, format = tokenize(JsonLoggerTokenizer(), "message"), minlevel = MiniLoggers.Debug) |> global_logger
56+
begin
57+
@debug "V:" s
58+
end
59+
60+
s1 = String(take!(buf))
61+
@test s1 == "{\"message\":\"V: s = \\\"\"}\n"
62+
end
4963
end
5064

5165
end # module

0 commit comments

Comments
 (0)