From a4236bd73a3f4db372f4c0e300507bbbe78effe8 Mon Sep 17 00:00:00 2001 From: Guillaume Hivert Date: Sun, 15 Dec 2024 04:33:09 +0100 Subject: [PATCH] Improve palabre output --- apps/backend/src/backend.gleam | 17 +++++++++- apps/backend/src/backend_ffi.erl | 28 +++++----------- packages/palabre/src/palabre.gleam | 12 +++++-- packages/palabre/src/palabre_ffi.erl | 48 +++++++++++++++++----------- 4 files changed, 63 insertions(+), 42 deletions(-) diff --git a/apps/backend/src/backend.gleam b/apps/backend/src/backend.gleam index 444ea18..42b4c66 100644 --- a/apps/backend/src/backend.gleam +++ b/apps/backend/src/backend.gleam @@ -5,6 +5,7 @@ import backend/workers import envoy import gleam/erlang/process import gleam/function +import gleam/http import gleam/int import gleam/option.{Some} import gleam/otp/supervisor @@ -29,7 +30,7 @@ fn configure_logger() { palabre.options() |> palabre.json(False) |> palabre.level(log_level) - |> palabre.color(True) + |> palabre.color(False) |> palabre.configure } @@ -45,11 +46,25 @@ fn start_http_server(ctx) { router.handle_request(_, ctx) |> wisp_mist.handler(secret_key_base) |> mist.new + |> mist.after_start(palabre_mist_start) |> mist.bind("0.0.0.0") |> mist.port(port) |> mist.start_http_server } +fn palabre_mist_start( + port: Int, + scheme: http.Scheme, + _ip_address: mist.IpAddress, +) -> Nil { + palabre.info() + |> palabre.int("port", port) + |> palabre.string("host", "0.0.0.0") + |> palabre.string("scheme", http.scheme_to_string(scheme)) + |> palabre.message("Server started, listening") + |> palabre.dump +} + fn start_periodic_workers(ctx) { use children <- supervisor.start() use children <- function.tap(children) diff --git a/apps/backend/src/backend_ffi.erl b/apps/backend/src/backend_ffi.erl index 6c46943..c042c69 100644 --- a/apps/backend/src/backend_ffi.erl +++ b/apps/backend/src/backend_ffi.erl @@ -21,22 +21,14 @@ extract_tar(Binary, BaseName, Version, Slug) -> ContentDest = <>, Content = <>, case erl_tar:extract({binary, Binary}, [{cwd, PackagePath}]) of - {error, _} -> - {error, nil}; + {error, _} -> {error, nil}; _ -> - Url = - <<"https://hexdocs.pm/", - BaseName/binary, - "/", - Version/binary, - "/package-interface.json">>, + Url = <<"https://hexdocs.pm/", BaseName/binary, "/", Version/binary, "/package-interface.json">>, erl_tar:extract(Content, [{cwd, ContentDest}, compressed]), {PackageInterface, Result} = case httpc:request(Url) of - {ok, {{_, 200, _}, _, HttpBody}} -> - save_file(ContentDest, HttpBody); - {ok, {200, HttpBody}} -> - save_file(ContentDest, HttpBody); + {ok, {{_, 200, _}, _, HttpBody}} -> save_file(ContentDest, HttpBody); + {ok, {200, HttpBody}} -> save_file(ContentDest, HttpBody); {error, _} -> BuildCmd = <<"cd ", ContentDest/binary, " && gleam docs build">>, Res = os:cmd(binary_to_list(BuildCmd)), @@ -54,18 +46,14 @@ remove_tar(Slug) -> is_match(Version, Requirement) -> case verl:is_match(Version, Requirement) of - {error, _} -> - {error, nil}; - Bool -> - {ok, Bool} + {error, _} -> {error, nil}; + Bool -> {ok, Bool} end. get_home() -> case init:get_argument(home) of - {ok, Content} -> - {ok, unicode:characters_to_binary(Content)}; - error -> - {error, nil} + {ok, Content} -> {ok, unicode:characters_to_binary(Content)}; + error -> {error, nil} end. set_level(Level) -> diff --git a/packages/palabre/src/palabre.gleam b/packages/palabre/src/palabre.gleam index 29f9e46..15bfdeb 100644 --- a/packages/palabre/src/palabre.gleam +++ b/packages/palabre/src/palabre.gleam @@ -1,4 +1,5 @@ import gleam/float +import gleam/function import gleam/http import gleam/int import gleam/list @@ -113,7 +114,13 @@ pub fn float(log: Log, key: String, value: Float) { } pub fn dump(log_: Log) -> Nil { - let text = option.unwrap(log_.message, "") + let text = + log_.message + |> case utils.is_color() { + True -> option.map(_, fn(m) { "\u{1b}[1m" <> m <> "\u{1b}[0m" }) + False -> function.identity + } + |> option.unwrap("") case log_.level { level.Emergency -> log(log_.level, log_.fields, text) level.Alert -> log(log_.level, log_.fields, text) @@ -130,8 +137,7 @@ pub fn dump(log_: Log) -> Nil { fn log(level: level.Level, message: a, text: String) -> Nil fn init(level: level.Level) { - [#("when", [utils.iso8601()]), #("id", [utils.uuid()])] - |> Log(level:, fields: _, message: None) + Log(level:, fields: [], message: None) } fn append_field( diff --git a/packages/palabre/src/palabre_ffi.erl b/packages/palabre/src/palabre_ffi.erl index 5a4abde..9e440e1 100644 --- a/packages/palabre/src/palabre_ffi.erl +++ b/packages/palabre/src/palabre_ffi.erl @@ -46,31 +46,42 @@ format(#{level := Level, msg := Msg, meta := _Meta}, #{json := Json, color := Co format_level(Level, #{color := Color}) -> case Level of - emergency when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31memergency\x1b[0m"; - alert when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31malert\x1b[0m"; - critical when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31mcritical\x1b[0m"; - error when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31merror\x1b[0m"; - warning when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;33mwarning\x1b[0m"; - notice when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;32mnotice\x1b[0m"; + emergency when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31memrg\x1b[0m"; + alert when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31malrt\x1b[0m"; + critical when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31mcrit\x1b[0m"; + error when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;31meror\x1b[0m"; + warning when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;33mwarn\x1b[0m"; + notice when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;32mntce\x1b[0m"; info when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;34minfo\x1b[0m"; - debug when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;36mdebug\x1b[0m"; - emergency -> "level=emergency"; - alert -> "level=alert"; - critical -> "level=critical"; - error -> "level=error"; - warning -> "level=warning"; - notice -> "level=notice"; + debug when Color -> "\x1b[32mlevel\x1b[31m=\x1b[1;36mdebg\x1b[0m"; + emergency -> "level=emrg"; + alert -> "level=alrt"; + critical -> "level=crit"; + error -> "level=eror"; + warning -> "level=warn"; + notice -> "level=ntce"; info -> "level=info"; - debug -> "level=debug" + debug -> "level=debg" end. format_msg(Report0, #{json := Json}) -> case Report0 of - {string, Msg} -> json_wrap([$\s, Msg]); + {string, Msg} -> + case is_json() of + true -> maps:put("id", uuid(), maps:put("when", format_iso8601(), json_wrap([$\s, Msg]))); + false -> + Defaults = [{<<"when"/utf8>>, [format_iso8601()]}, {<<"id"/utf8>>, [uuid()]}], + Converted = palabre@internals@converter:format_fields(Defaults), + case is_color() of + false -> json_wrap([$\s, Converted, $\s, Msg]); + true -> json_wrap([$\s, Converted, $\s, "\x1b[1m", Msg, "\x1b[0m"]) + end + end; {report, [{palabre, Fields, Text}]} -> + Fields1 = [{<<"when"/utf8>>, [format_iso8601()]}, {<<"id"/utf8>>, [uuid()]} | Fields], case Json of - false -> [$\s, palabre@internals@converter:format_fields(Fields), $\s, Text]; - true -> palabre@internals@converter:format_json(Fields, Text) + false -> [$\s, palabre@internals@converter:format_fields(Fields1), $\s, Text]; + true -> palabre@internals@converter:format_json(Fields1, Text) end; {report, Report1} when is_map(Report1) -> json_wrap(format_kv(maps:to_list(Report1))); {report, Report1} when is_list(Report1) -> json_wrap(format_kv(Report1)); @@ -114,4 +125,5 @@ is_json() -> persistent_term:get(json, false). is_color() -> - persistent_term:get(color, false). + not (persistent_term:get(json, false)) + andalso persistent_term:get(color, false).