Skip to content

Commit 02ce741

Browse files
authored
feat: add config parameter to choose type of line ending (#15)
* feat: config switch for line ending type * doc: document line_ending_type config parameter
1 parent 925fee4 commit 02ce741

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ To print each json object to a new line, set `new_line` to `true`:
3939
#{formatter => {jsonformat, #{ new_line => true }}}
4040
```
4141

42+
To specify which kind of line ending to use, set `new_line_type` to
43+
one of `nl`, `crlf`, `cr`, `unix`, `windows` or `macos9`. These
44+
correspond to:
45+
46+
* `nl` or `unix` means `\n`, ASCII character 0x0A
47+
* `crlf` or `windows` means `\r\n`, ASCII characters 0x0D 0x0A
48+
* `cr` or `macos9` means `\r`, ASCII character 0x0D
49+
50+
The default line ending if none is specified is `nl`.
51+
52+
```erlang
53+
#{formatter => {jsonformat, #{ new_line => true, new_line_type => crlf }}}
54+
```
55+
4256
To control what is being included in the log object from the metadata, there
4357
are two ways. One can opt-out from fields. Default opts out is `[report_cb]`.
4458

src/jsonformat.erl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
%%%_* Types ============================================================
3030
-type config() :: #{ new_line => boolean()
31+
, new_line_type => nl | crlf | cr | unix | windows | macos9
3132
, key_mapping => #{atom() => atom()}
3233
, format_funs => #{atom() => fun((_) -> _)}}.
3334

@@ -90,7 +91,7 @@ merge_meta(Msg, Meta0, Config) ->
9091
encode(Data, Config) ->
9192
Json = jsx:encode(Data),
9293
case new_line(Config) of
93-
true -> [Json, <<"\n">>];
94+
true -> [Json, new_line_type(Config)];
9495
false -> Json
9596
end.
9697

@@ -133,6 +134,14 @@ apply_key_mapping(Data, _) ->
133134

134135
new_line(Config) -> maps:get(new_line, Config, ?NEW_LINE).
135136

137+
new_line_type(#{new_line_type := nl}) -> <<"\n">>;
138+
new_line_type(#{new_line_type := unix}) -> <<"\n">>;
139+
new_line_type(#{new_line_type := crlf}) -> <<"\r\n">>;
140+
new_line_type(#{new_line_type := windows}) -> <<"\r\n">>;
141+
new_line_type(#{new_line_type := cr}) -> <<"\r">>;
142+
new_line_type(#{new_line_type := macos9}) -> <<"\r">>;
143+
new_line_type(_Default) -> <<"\n">>.
144+
136145
meta_without(Meta, Config) ->
137146
maps:without(maps:get(meta_without, Config, [report_cb]), Meta).
138147

@@ -224,6 +233,16 @@ meta_with_test() ->
224233
jsx:decode(format(Error, Config2))),
225234
ok.
226235

236+
newline_test() ->
237+
ConfigDefault = #{ new_line => true },
238+
?assertEqual( [<<"{\"level\":\"alert\",\"text\":\"derp\"}">>, <<"\n">>]
239+
, format(#{level => alert, msg => {string, "derp"}, meta => #{}}, ConfigDefault) ),
240+
ConfigCRLF = #{ new_line_type => crlf
241+
, new_line => true
242+
},
243+
?assertEqual( [<<"{\"level\":\"alert\",\"text\":\"derp\"}">>, <<"\r\n">>]
244+
, format(#{level => alert, msg => {string, "derp"}, meta => #{}}, ConfigCRLF) ).
245+
227246
-endif.
228247

229248
%%%_* Emacs ============================================================

0 commit comments

Comments
 (0)