Packages
logger_json
7.0.3
7.0.4
7.0.3
7.0.2
7.0.1
7.0.0
6.2.1
6.2.0
6.0.3
6.0.2
6.0.1
6.0.0
6.0.0-rc.4
6.0.0-rc.3
6.0.0-rc.2
6.0.0-rc.1
6.0.0-rc.0
5.1.4
5.1.3
5.1.2
5.1.1
5.1.0
5.0.0
4.3.0
4.2.0
4.1.0
4.0.0
3.3.0
3.2.0
3.1.2
3.1.1
3.1.0
3.0.3
3.0.2
3.0.1
3.0.0
2.0.1
2.0.0
1.3.0
1.2.1
1.2.0
1.1.0
1.0.1
1.0.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.0
This package includes a set of :logger formatters designed to output logs in JSON format. It is compatible with a variety of log management systems that support JSON, including Google Cloud Logging and Error Reporting, Datadog, ElasticSearch, LogStash, FileBeat, and Kibana.
Current section
Files
Jump to
Current section
Files
lib/logger_json/formatter/datetime.ex
defmodule LoggerJSON.Formatter.DateTime do
@moduledoc false
@doc """
Returns either a `time` taken from metadata or current time in RFC3339 UTC "Zulu" format.
"""
def utc_time(%{time: time}) when is_integer(time) and time >= 0 do
system_time_to_rfc3339(time)
end
def utc_time(_meta) do
:os.system_time(:microsecond)
|> system_time_to_rfc3339()
end
defp system_time_to_rfc3339(system_time) do
micro = rem(system_time, 1_000_000)
{date, {hours, minutes, seconds}} = :calendar.system_time_to_universal_time(system_time, :microsecond)
[format_date(date), ?T, format_time({hours, minutes, seconds, div(micro, 1000)}), ?Z]
|> IO.iodata_to_binary()
end
defp format_time({hh, mi, ss, ms}) do
[pad2(hh), ?:, pad2(mi), ?:, pad2(ss), ?., pad3(ms)]
end
defp format_date({yy, mm, dd}) do
[Integer.to_string(yy), ?-, pad2(mm), ?-, pad2(dd)]
end
defp pad3(int) when int < 10, do: [?0, ?0, Integer.to_string(int)]
defp pad3(int) when int < 100, do: [?0, Integer.to_string(int)]
defp pad3(int), do: Integer.to_string(int)
defp pad2(int) when int < 10, do: [?0, Integer.to_string(int)]
defp pad2(int), do: Integer.to_string(int)
end