Skip to content

Commit bd4a6a3

Browse files
committed
Implement verbose->loglevel + format log events
1 parent 04e5777 commit bd4a6a3

File tree

6 files changed

+27
-13
lines changed

6 files changed

+27
-13
lines changed

deps/rabbit/src/rabbit_cli_cmd_list_exchanges.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ list_exchanges(Args) ->
3636
[VHost, InfoKeys]),
3737
case is_list(Ret) of
3838
true ->
39-
rabbit_cli_output:notify(
39+
rabbit_cli_io:notify(
4040
{info_table, #{keys => InfoKeys,
4141
rows => Ret,
4242
callbacks => #{
@@ -53,7 +53,7 @@ list_exchanges(Args) ->
5353
end
5454
}}});
5555
false ->
56-
rabbit_cli_output:notify(Ret)
56+
rabbit_cli_io:notify(Ret)
5757
end.
5858

5959
get_nodename(#{node := Nodename}) ->

deps/rabbit/src/rabbit_cli_cmd_version.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ cli() ->
1414
show_version(_) ->
1515
ok = application:load(rabbit),
1616
{ok, Version} = application:get_key(rabbit, vsn),
17-
rabbit_cli_output:sync_notify(Version).
17+
rabbit_cli_io:notify(Version).

deps/rabbit/src/rabbit_cli_global_options.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ cli() ->
1212
action => store},
1313

1414
#{name => vhost,
15-
short => $v,
15+
short => $p,
1616
long => "-vhost",
1717
type => binary,
1818
action => store},
1919

2020
#{name => verbose,
21-
short => $V,
21+
short => $v,
2222
long => "-verbose",
2323
action => count}
2424
]}.

deps/rabbit/src/rabbit_cli_output.erl renamed to deps/rabbit/src/rabbit_cli_io.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-module(rabbit_cli_output).
1+
-module(rabbit_cli_io).
22

33
-export([setup/1,
44
close/0,
@@ -13,7 +13,7 @@ setup(Args) ->
1313
{ok, Pid} ->
1414
ok = gen_event:add_sup_handler(
1515
Pid,
16-
rabbit_cli_output_console,
16+
rabbit_cli_io_console,
1717
Args),
1818
ok = logger:add_handler(
1919
?EVENT_MGR_REF, ?MODULE, #{}),
@@ -23,7 +23,7 @@ setup(Args) ->
2323
end.
2424

2525
close() ->
26-
gen_event:stop(rabbit_cli_output).
26+
gen_event:stop(?EVENT_MGR_REF).
2727

2828
notify(Event) ->
2929
gen_event:notify(?EVENT_MGR_REF, Event).

deps/rabbit/src/rabbit_cli_output_console.erl renamed to deps/rabbit/src/rabbit_cli_io_console.erl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-module(rabbit_cli_output_console).
1+
-module(rabbit_cli_io_console).
22
-behaviour(gen_event).
33

44
-include_lib("stdout_formatter/include/stdout_formatter.hrl").
@@ -29,6 +29,11 @@ init(_) ->
2929
handle_call(_Request, State) ->
3030
{ok, ok, State}.
3131

32+
handle_event(
33+
{log_event, LogEvent, #{formatter := {FModule, FConfig}}},
34+
State) ->
35+
io:put_chars(FModule:format(LogEvent, FConfig)),
36+
{ok, State};
3237
handle_event(
3338
{info_table, #{keys := Keys, rows := Rows0, callbacks := CBs}},
3439
State)

deps/rabbit/src/rabbit_cli_main.erl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ run(_ProgName, RawArgs) ->
1111
Ret = argparse:parse(RawArgs, CommandMap),
1212

1313
%% 2. Output setup.
14-
rabbit_cli_output:setup(Ret),
15-
logger:set_primary_config(level, debug),
16-
%logger:i(),
14+
rabbit_cli_io:setup(Ret),
15+
set_log_level(Ret),
1716
logger:debug("argparse:parse/2 -> ~p~n", [Ret]),
1817

1918
%% 3. Command execution.
@@ -29,7 +28,7 @@ run(_ProgName, RawArgs) ->
2928

3029

3130
%% 4. Close output and return exit status.
32-
rabbit_cli_output:close(),
31+
rabbit_cli_io:close(),
3332
ok.
3433

3534
discover_commands() ->
@@ -39,6 +38,16 @@ discover_commands() ->
3938
rabbit_cli_cmd_version
4039
].
4140

41+
set_log_level(#{verbose := Verbose}) ->
42+
if
43+
Verbose >= 2 -> logger:set_primary_config(level, debug);
44+
true -> logger:set_primary_config(level, info)
45+
end;
46+
set_log_level(ArgMap) when is_map(ArgMap) ->
47+
logger:set_primary_config(level, notice);
48+
set_log_level({ArgMap, _}) ->
49+
set_log_level(ArgMap).
50+
4251
%% -------------------------------------------------------------------
4352
%% Copied from `cli` module (argparse 1.1.0).
4453
%% -------------------------------------------------------------------

0 commit comments

Comments
 (0)