Current section
Files
Jump to
Current section
Files
src/glog.erl
-module(glog).
-compile(no_auto_import).
-export([new/0, add/3, add_field/2, add_fields/2, emergency/2, emergencyf/3, alert/2, alertf/3, critical/2, criticalf/3, error/2, errorf/3, warning/2, warningf/3, info/2, infof/3, notice/2, noticef/3, debug/2, debugf/3]).
-export_type([glog/0]).
-opaque glog() :: {glog,
gleam@map:map_(gleam@erlang@atom:atom_(), gleam@dynamic:dynamic())}.
-spec new() -> glog().
new() ->
set_default_config(),
{glog, gleam@map:new()}.
-spec add(glog(), binary(), any()) -> glog().
add(Logger, Key, Value) ->
{glog,
begin
_pipe = erlang:element(2, Logger),
gleam@map:insert(
_pipe,
erlang:binary_to_atom(Key),
gleam@dynamic:from(Value)
)
end}.
-spec add_field(glog(), glog@field:field()) -> glog().
add_field(Logger, F) ->
{glog,
begin
_pipe = erlang:element(2, Logger),
gleam@map:insert(
_pipe,
erlang:binary_to_atom(glog@field:key(F)),
glog@field:value(F)
)
end}.
-spec add_fields(glog(), glog@field:fields()) -> glog().
add_fields(Logger, F) ->
{glog, gleam@map:merge(erlang:element(2, Logger), fields_to_dynamic(F))}.
-spec emergency(glog(), binary()) -> glog().
emergency(Logger, Message) ->
log(Logger, emergency, Message).
-spec emergencyf(glog(), binary(), glog@arg:args()) -> glog().
emergencyf(Logger, String, Values) ->
logf(Logger, emergency, String, Values).
-spec alert(glog(), binary()) -> glog().
alert(Logger, Message) ->
log(Logger, alert, Message).
-spec alertf(glog(), binary(), glog@arg:args()) -> glog().
alertf(Logger, String, Values) ->
logf(Logger, alert, String, Values).
-spec critical(glog(), binary()) -> glog().
critical(Logger, Message) ->
log(Logger, critical, Message).
-spec criticalf(glog(), binary(), glog@arg:args()) -> glog().
criticalf(Logger, String, Values) ->
logf(Logger, critical, String, Values).
-spec error(glog(), binary()) -> glog().
error(Logger, Message) ->
log(Logger, error, Message).
-spec errorf(glog(), binary(), glog@arg:args()) -> glog().
errorf(Logger, String, Values) ->
logf(Logger, error, String, Values).
-spec warning(glog(), binary()) -> glog().
warning(Logger, Message) ->
log(Logger, warning, Message).
-spec warningf(glog(), binary(), glog@arg:args()) -> glog().
warningf(Logger, String, Values) ->
logf(Logger, warning, String, Values).
-spec info(glog(), binary()) -> glog().
info(Logger, Message) ->
log(Logger, info, Message).
-spec infof(glog(), binary(), glog@arg:args()) -> glog().
infof(Logger, String, Values) ->
logf(Logger, info, String, Values).
-spec notice(glog(), binary()) -> glog().
notice(Logger, Message) ->
log(Logger, notice, Message).
-spec noticef(glog(), binary(), glog@arg:args()) -> glog().
noticef(Logger, String, Values) ->
logf(Logger, notice, String, Values).
-spec debug(glog(), binary()) -> glog().
debug(Logger, Message) ->
log(Logger, debug, Message).
-spec debugf(glog(), binary(), glog@arg:args()) -> glog().
debugf(Logger, String, Values) ->
logf(Logger, debug, String, Values).
-spec log(glog(), glog@level:level(), binary()) -> glog().
log(Logger, Level, Message) ->
New_logger = begin
_pipe = Logger,
add(_pipe, <<"msg"/utf8>>, Message)
end,
logger:log(Level, erlang:element(2, New_logger)),
{glog, gleam@map:new()}.
-spec logf(glog(), glog@level:level(), binary(), glog@arg:args()) -> glog().
logf(Logger, Level, String, Values) ->
New_logger = begin
_pipe = Logger,
add(
_pipe,
<<"msg"/utf8>>,
io_lib:format(String, args_to_dynamic(Values))
)
end,
logger:log(Level, erlang:element(2, New_logger)),
{glog, gleam@map:new()}.
-spec args_to_dynamic(glog@arg:args()) -> list(gleam@dynamic:dynamic()).
args_to_dynamic(Args) ->
{args, Largs} = Args,
_pipe = Largs,
gleam@list:map(_pipe, fun(A) -> gleam@dynamic:from(glog@arg:value(A)) end).
-spec fields_to_dynamic(glog@field:fields()) -> gleam@map:map_(gleam@erlang@atom:atom_(), gleam@dynamic:dynamic()).
fields_to_dynamic(Fields) ->
{fields, Lfields} = Fields,
gleam@map:from_list(
begin
_pipe = Lfields,
gleam@list:map(
_pipe,
fun(F) ->
{erlang:binary_to_atom(glog@field:key(F)),
glog@field:value(F)}
end
)
end
).
-spec set_default_config() -> nil.
set_default_config() ->
logger:set_handler_config(
erlang:binary_to_atom(<<"default"/utf8>>),
erlang:binary_to_atom(<<"formatter"/utf8>>),
gleam@dynamic:from(
{gleam@dynamic:from(
erlang:binary_to_atom(<<"logger_formatter"/utf8>>)
),
gleam@dynamic:from(
gleam@map:from_list(
[{erlang:binary_to_atom(<<"single_line"/utf8>>),
erlang:binary_to_atom(<<"true"/utf8>>)},
{erlang:binary_to_atom(<<"legacy_header"/utf8>>),
erlang:binary_to_atom(<<"false"/utf8>>)}]
)
)}
)
).