Current section

Files

Jump to
glog src glog.erl
Raw

src/glog.erl

-module(glog).
-compile(no_auto_import).
-export([new/0, new_with/1, has_field/2, get_field/2, add/3, add_field/2, add_result/3, add_option/3, add_error/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, set_primary_log_level/1, set_handler_log_level/2, set_default_config/0]).
-export_type([glog/0]).
-opaque glog() :: {glog,
gleam@map:map_(gleam@erlang@atom:atom_(), gleam@dynamic:dynamic())}.
-spec new() -> glog().
new() ->
{glog, gleam@map:new()}.
-spec new_with(gleam@option:option(list(glog@field:field()))) -> glog().
new_with(With) ->
case With of
{some, Fields} ->
_pipe = new(),
add_fields(_pipe, Fields);
none ->
new()
end.
-spec has_field(glog(), binary()) -> boolean().
has_field(Logger, Key) ->
gleam@map:has_key(erlang:element(2, Logger), erlang:binary_to_atom(Key)).
-spec get_field(glog(), binary()) -> {ok, glog@field:field()} | {error, nil}.
get_field(Logger, Key) ->
_pipe = erlang:element(2, Logger),
_pipe@1 = gleam@map:get(_pipe, erlang:binary_to_atom(Key)),
gleam@result:map(_pipe@1, fun(Value) -> glog@field:new(Key, Value) end).
-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_result(glog(), binary(), {ok, any()} | {error, any()}) -> glog().
add_result(Logger, Key, R) ->
case R of
{ok, V} ->
_pipe = Logger,
add(_pipe, Key, V);
{error, E} ->
_pipe@1 = Logger,
add_error(_pipe@1, E)
end.
-spec add_option(glog(), binary(), gleam@option:option(any())) -> glog().
add_option(Logger, Key, O) ->
case O of
{some, V} ->
_pipe = Logger,
add(_pipe, Key, V);
none ->
Logger
end.
-spec add_error(glog(), any()) -> glog().
add_error(Logger, Error) ->
_pipe = Logger,
add(_pipe, <<"error"/utf8>>, Error).
-spec add_fields(glog(), list(glog@field:field())) -> 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(), list(glog@arg:arg())) -> 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(), list(glog@arg:arg())) -> 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(), list(glog@arg:arg())) -> 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(), list(glog@arg:arg())) -> 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(), list(glog@arg:arg())) -> 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(), list(glog@arg:arg())) -> 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(), list(glog@arg:arg())) -> 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(), list(glog@arg:arg())) -> 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)),
Logger.
-spec logf(glog(), glog@level:level(), binary(), list(glog@arg:arg())) -> glog().
logf(Logger, Level, String, Values) ->
log(Logger, Level, io_lib:format(String, args_to_dynamic(Values))).
-spec args_to_dynamic(list(glog@arg:arg())) -> list(gleam@dynamic:dynamic()).
args_to_dynamic(Args) ->
_pipe = Args,
gleam@list:map(_pipe, fun(A) -> gleam@dynamic:from(glog@arg:value(A)) end).
-spec fields_to_dynamic(list(glog@field:field())) -> gleam@map:map_(gleam@erlang@atom:atom_(), gleam@dynamic:dynamic()).
fields_to_dynamic(Fields) ->
gleam@map:from_list(
begin
_pipe = Fields,
gleam@list:map(
_pipe,
fun(F) ->
{erlang:binary_to_atom(glog@field:key(F)),
glog@field:value(F)}
end
)
end
).
-spec set_primary_log_level(glog@level:config_level()) -> nil.
set_primary_log_level(Level) ->
logger:set_primary_config(
erlang:binary_to_atom(<<"level"/utf8>>),
gleam@dynamic:from(Level)
).
-spec set_handler_log_level(binary(), glog@level:config_level()) -> nil.
set_handler_log_level(Handler, Level) ->
logger:set_handler_config(
erlang:binary_to_atom(Handler),
erlang:binary_to_atom(<<"level"/utf8>>),
gleam@dynamic:from(Level)
).
-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>>)}]
)
)}
)
).