1 |
|
-module(mongoose_logs). |
2 |
|
|
3 |
|
-export([set_global_loglevel/1]). |
4 |
|
-export([get_global_loglevel/0]). |
5 |
|
-export([set_module_loglevel/2]). |
6 |
|
-export([clear_module_loglevel/1]). |
7 |
|
-export([get_log_files/0]). |
8 |
|
-export([dir/0]). |
9 |
|
-export([loglevel_keyword_to_number/1]). |
10 |
|
|
11 |
|
-export_type([atom_log_level/0]). |
12 |
|
|
13 |
|
-ignore_xref([clear_module_loglevel/1, set_module_loglevel/2]). |
14 |
|
|
15 |
|
-type atom_log_level() :: none | logger:level() | all. |
16 |
|
-type int_log_level() :: -1..8. |
17 |
|
-type level() :: atom_log_level() | int_log_level(). |
18 |
|
|
19 |
|
%% Sets primary log level |
20 |
|
-spec get_global_loglevel() -> atom_log_level(). |
21 |
|
get_global_loglevel() -> |
22 |
22 |
maps:get(level, logger:get_primary_config()). |
23 |
|
|
24 |
|
-spec set_global_loglevel(level()) -> |
25 |
|
ok | {error, {invalid_level, term()}}. |
26 |
|
set_global_loglevel(Level) when is_integer(Level) -> |
27 |
:-( |
set_global_loglevel(loglevel_number_to_keyword(Level)); |
28 |
|
set_global_loglevel(Level) -> |
29 |
64 |
logger:update_primary_config(#{level => Level}). |
30 |
|
|
31 |
|
-spec set_module_loglevel(module(), level()) -> |
32 |
|
ok | {error, term()}. |
33 |
|
set_module_loglevel(Module, Level) when is_integer(Level) -> |
34 |
:-( |
set_module_loglevel(Module, loglevel_number_to_keyword(Level)); |
35 |
|
set_module_loglevel(Module, Level) -> |
36 |
:-( |
logger:set_module_level(Module, Level). |
37 |
|
|
38 |
|
-spec clear_module_loglevel(module()) -> ok | {error, term()}. |
39 |
|
clear_module_loglevel(Module) -> |
40 |
:-( |
set_module_loglevel(Module, get_global_loglevel()). |
41 |
|
|
42 |
|
-spec get_log_files() -> [file:filename()]. |
43 |
|
get_log_files() -> |
44 |
118 |
[ File || #{config := #{file := File}} <- logger:get_handler_config() ]. |
45 |
|
|
46 |
|
-spec dir() -> string(). |
47 |
|
dir() -> |
48 |
103 |
case logger:get_handler_config(disk_log) of |
49 |
|
{ok, #{config := #{file := Path}}} -> |
50 |
103 |
filename:dirname(Path); |
51 |
|
_ -> |
52 |
:-( |
"" |
53 |
|
end. |
54 |
|
|
55 |
|
-spec loglevel_number_to_keyword(int_log_level()) -> atom_log_level(). |
56 |
:-( |
loglevel_number_to_keyword(-1) -> none; |
57 |
:-( |
loglevel_number_to_keyword(0) -> emergency; |
58 |
:-( |
loglevel_number_to_keyword(1) -> alert; |
59 |
:-( |
loglevel_number_to_keyword(2) -> critical; |
60 |
:-( |
loglevel_number_to_keyword(3) -> error; |
61 |
:-( |
loglevel_number_to_keyword(4) -> warning; |
62 |
:-( |
loglevel_number_to_keyword(5) -> notice; |
63 |
:-( |
loglevel_number_to_keyword(6) -> info; |
64 |
:-( |
loglevel_number_to_keyword(7) -> debug; |
65 |
:-( |
loglevel_number_to_keyword(8) -> all. |
66 |
|
|
67 |
|
-spec loglevel_keyword_to_number(atom_log_level()) -> int_log_level(). |
68 |
:-( |
loglevel_keyword_to_number(none) -> -1; |
69 |
:-( |
loglevel_keyword_to_number(emergency) -> 0; |
70 |
:-( |
loglevel_keyword_to_number(alert) -> 1; |
71 |
:-( |
loglevel_keyword_to_number(critical) -> 2; |
72 |
:-( |
loglevel_keyword_to_number(error) -> 3; |
73 |
42 |
loglevel_keyword_to_number(warning) -> 4; |
74 |
:-( |
loglevel_keyword_to_number(notice) -> 5; |
75 |
:-( |
loglevel_keyword_to_number(info) -> 6; |
76 |
:-( |
loglevel_keyword_to_number(debug) -> 7; |
77 |
:-( |
loglevel_keyword_to_number(all) -> 8. |