diff --git a/src/logger_formatter_json.erl b/src/logger_formatter_json.erl index 3bf93ab..412b1c7 100644 --- a/src/logger_formatter_json.erl +++ b/src/logger_formatter_json.erl @@ -174,10 +174,10 @@ to_string(X, Config) when is_list(X) -> to_string(<<>>, _Config) -> <<>>; to_string(X, Config) when is_binary(X) -> - case re:run(X, <<"[:print]+">>, [{capture, none}, unicode]) of - match -> X; - _ -> io_lib:format(p(Config), [X]) - end; + case re:run(X, <<"[[:^print:]]">>, [{capture, none}, unicode]) of + match -> io_lib:format(p(Config), [X]); + _ -> X + end; to_string(X, Config) -> io_lib:format(p(Config), [X]). diff --git a/test/logger_formatter_json_SUITE.erl b/test/logger_formatter_json_SUITE.erl index 6d25bff..30c1eb0 100644 --- a/test/logger_formatter_json_SUITE.erl +++ b/test/logger_formatter_json_SUITE.erl @@ -68,6 +68,57 @@ unstructured(_) -> ) ) ), + ?assertEqual( + <<"{\"msg\":\"hello world\",\"level\":\"info\",\"request_id\":\"F6R64Fh3F9NzEscAAAaB\"}\n">>, + iolist_to_binary( + logger_formatter_json:format( + #{ + level => info, + msg => {"hello ~s", ["world"]}, + meta => #{request_id => <<"F6R64Fh3F9NzEscAAAaB">>} + }, + #{} + ) + ) + ), + ?assertEqual( + <<"{\"msg\":\"hello world\",\"level\":\"info\",\"request_id\":\"string with spaces\"}\n">>, + iolist_to_binary( + logger_formatter_json:format( + #{ + level => info, + msg => {"hello ~s", ["world"]}, + meta => #{request_id => <<"string with spaces">>} + }, + #{} + ) + ) + ), + ?assertEqual( + << + "{\"msg\":\"hello world\",\"level\":\"info\",\"foo\":\"<<\\\"string with control char\\\\n\\\">>\"}\n" + >>, + iolist_to_binary( + logger_formatter_json:format( + #{ + level => info, + msg => {"hello ~s", ["world"]}, + meta => #{foo => <<"string with control char\n">>} + }, + #{} + ) + ) + ), + % Binary data + ?assertEqual( + <<"{\"msg\":\"hello world\",\"level\":\"info\",\"foo\":\"<<0,1,2,3>>\"}\n">>, + iolist_to_binary( + logger_formatter_json:format( + #{level => info, msg => {"hello ~s", ["world"]}, meta => #{foo => <<0, 1, 2, 3>>}}, + #{} + ) + ) + ), ok.