Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make timestamp format configurable #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ And finally, configure `lager` app with something like this:
* outputs: `tcp`, `udp`, `file`
* formats: `json`
* json encoders: `jsx`, `jiffy`
* configurable `timestamp_format`:
- `original` - e.g. `"2017-06-14T04:02:22.845"`
- `unix_seconds` - e.g. `"1497412942"`
- `iso8601` - e.g. `"2017-06-14T04:02:22.845Z"`

## JSON formatter

Expand All @@ -59,13 +63,17 @@ Here's how you would use the included JSON formatter with the
{file, "log/lager_logstash.log"},
{level, info},
{formatter, lager_logstash_json_formatter},
{formatter_config, [{json_encoder, jsx}]},
{formatter_config, [{json_encoder, jsx}, {timestamp_format, iso8601}]},
{size, 10485760},
{date, "$D0"},
{count, 5}
]}
```

## Configuration options

Some options can be passed to the formatter via lager's `formatter_config` configuration item:

## TODOs

* reconnect
Expand Down
23 changes: 20 additions & 3 deletions src/lager_logstash_json_formatter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
format(LagerMsg, Config) ->
Encoder = value(json_encoder, Config, ?DEFAULT_JSON_FORMATTER),
Level = lager_msg:severity(LagerMsg),
Timestamp = timestamp(lager_msg:datetime(LagerMsg)),
TsFormat = value(timestamp_format, Config, original),
Timestamp = timestamp(TsFormat, LagerMsg),
Message = lager_msg:message(LagerMsg),
Metadata = lager_msg:metadata(LagerMsg),
Data = [{type, lager_logstash},
Expand All @@ -41,14 +42,30 @@ format(LagerMsg, Config) ->
format(Message, Config, _) ->
format(Message, Config).

timestamp(original, LagerMsg) ->
{Date, Time} = lager_msg:datetime(LagerMsg),
[Date, $T, Time];
timestamp(unix_seconds, LagerMsg) ->
{Megas, Secs, _Micros} = lager_msg:timestamp(LagerMsg),
integer_to_list(Megas * 1000 * 1000 + Secs);
timestamp(iso8601, LagerMsg) ->
TS = {_Megas, _Secs, Micros} = lager_msg:timestamp(LagerMsg),
{{Y, Mo, D}, {H, Mi, S}} = calendar:now_to_universal_time(TS),
Millis = Micros div 1000,
[integer_to_list(Y), $-, i2l(Mo), $-, i2l(D), $T, i2l(H), $:, i2l(Mi), $:, i2l(S), $., i3l(Millis), $Z].

%% @doc Lifted from lager_util.
i2l(I) when I < 10 -> [$0, $0+I];
i2l(I) -> integer_to_list(I).
i3l(I) when I < 100 -> [$0 | i2l(I)];
i3l(I) -> integer_to_list(I).

value(Name, Config, Default) ->
case lists:keyfind(Name, 1, Config) of
{Name, Value} -> Value;
false -> Default
end.

timestamp({Date, Time}) -> [Date, $T, Time].

convert(Data) -> lists:foldl(fun convert/2, [], Data).

convert({_, undefined}, Acc) -> Acc;
Expand Down