25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-30 16:36:29 +01:00

Add support for changing the loglevels for given modules (EJAB-225)

ejabberd_loglevel:set/1 now understands more input formats:

set(Param) -> {module, ejabberd_logger}
    Param = Level | {Level, CustomLevels}
    CustomLevels = [CustomLevel]
    CustomLevel = {Module, Level}
    Module = atom()
    Level = integer() | atom()

set(Level) is equivalent to set({Level, []}), so it clears all module
level customizations. log_src/2 adds additional function heads for those
_msg function and module combinations that need non-default behavior,
i.e. add or filter logging.

ejabberd_loglevel:get/0 only returns the default log level. To retrieve
the complete setting use ejabberd_logger:get/0 instead.
This commit is contained in:
Andreas Köhler 2010-10-13 12:52:56 +02:00 committed by Badlop
parent cc76869da5
commit 81555d8def

View File

@ -59,18 +59,20 @@
function = debug_msg, event_type = info_msg, msg_prefix = "D"}]). function = debug_msg, event_type = info_msg, msg_prefix = "D"}]).
get() -> get() ->
Level = ejabberd_logger:get(), {DefaultLevel, _CustomLevels} = ejabberd_logger:get(),
case lists:keysearch(Level, #loglevel.ordinal, ?LOG_LEVELS) of case lists:keysearch(DefaultLevel, #loglevel.ordinal, ?LOG_LEVELS) of
{value, Result = #loglevel{}} -> {value, Result = #loglevel{}} ->
{Result#loglevel.ordinal, Result#loglevel.name, Result#loglevel.description}; {Result#loglevel.ordinal, Result#loglevel.name, Result#loglevel.description};
_ -> _ ->
erlang:error({no_such_loglevel, Level}) erlang:error({no_such_loglevel, DefaultLevel})
end. end.
set(DefaultLevel) when is_atom(DefaultLevel) orelse is_integer(DefaultLevel) ->
set(LogLevel) when is_atom(LogLevel) -> set({DefaultLevel, []});
set(level_to_integer(LogLevel)); set({DefaultLevel, CustomLevels}) when is_list(CustomLevels) ->
set(Loglevel) when is_integer(Loglevel) -> DefaultInt = level_to_integer(DefaultLevel),
CustomInts = [level_to_integer(C) || C <- CustomLevels],
Loglevel = {DefaultInt, CustomInts},
try try
{Mod,Code} = dynamic_compile:from_string(ejabberd_logger_src(Loglevel)), {Mod,Code} = dynamic_compile:from_string(ejabberd_logger_src(Loglevel)),
code:load_binary(Mod, ?LOGMODULE ++ ".erl", Code) code:load_binary(Mod, ?LOGMODULE ++ ".erl", Code)
@ -78,8 +80,12 @@ set(Loglevel) when is_integer(Loglevel) ->
Type:Error -> ?CRITICAL_MSG("Error compiling logger (~p): ~p~n", [Type, Error]) Type:Error -> ?CRITICAL_MSG("Error compiling logger (~p): ~p~n", [Type, Error])
end; end;
set(_) -> set(_) ->
exit("Loglevel must be an integer"). exit("Invalid loglevel format").
level_to_integer(Level) when is_integer(Level) ->
Level;
level_to_integer({Module, Level}) ->
{Module, level_to_integer(Level)};
level_to_integer(Level) -> level_to_integer(Level) ->
case lists:keysearch(Level, #loglevel.name, ?LOG_LEVELS) of case lists:keysearch(Level, #loglevel.name, ?LOG_LEVELS) of
{value, #loglevel{ordinal = Int}} -> Int; {value, #loglevel{ordinal = Int}} -> Int;
@ -109,21 +115,35 @@ header_src() ->
". ".
get_src(Loglevel) -> get_src(Loglevel) ->
["get() -> ", integer_to_list(Loglevel), ". io_lib:format("get() -> ~w.
"]. ", [Loglevel]).
log_src(_Loglevel, #loglevel{function = no_log}) -> log_src(_Loglevel, #loglevel{function = no_log}) ->
[]; [];
log_src(Loglevel, Spec = #loglevel{ordinal = MinLevel}) log_src({DefaultLevel, [{Module, Level} | Tail]}, Spec = #loglevel{ordinal = MinLevel})
when Loglevel < MinLevel -> when Level < MinLevel andalso MinLevel =< DefaultLevel ->
[atom_to_list(Spec#loglevel.function), "(", atom_to_list(Module), ", _, _, _) -> ok;
", log_src({DefaultLevel, Tail}, Spec)];
log_src({DefaultLevel, [{Module, Level} | Tail]}, Spec = #loglevel{ordinal = MinLevel})
when DefaultLevel < MinLevel andalso MinLevel =< Level ->
[atom_to_list(Spec#loglevel.function), "(", atom_to_list(Module), " = Module, Line, Format, Args) ->",
log_notify_src(Spec), ";
", log_src({DefaultLevel, Tail}, Spec)];
log_src({DefaultLevel, [_Head | Tail]}, Spec = #loglevel{}) ->
log_src({DefaultLevel, Tail}, Spec);
log_src({DefaultLevel, []}, Spec = #loglevel{ordinal = MinLevel})
when DefaultLevel < MinLevel ->
[atom_to_list(Spec#loglevel.function), "(_, _, _, _) -> ok. [atom_to_list(Spec#loglevel.function), "(_, _, _, _) -> ok.
"]; "];
log_src(_Loglevel, Spec = #loglevel{}) -> log_src({_DefaultLevel, []}, Spec = #loglevel{}) ->
[atom_to_list(Spec#loglevel.function), "(Module, Line, Format, Args) -> [atom_to_list(Spec#loglevel.function), "(Module, Line, Format, Args) ->",
notify(", atom_to_list(Spec#loglevel.event_type), ", log_notify_src(Spec), ".
\"", Spec#loglevel.msg_prefix, "(~p:~p:~p) : \"++Format++\"~n\", "].
[self(), Module, Line | Args]).
"]. log_notify_src(Spec = #loglevel{}) ->
["notify(", atom_to_list(Spec#loglevel.event_type), ",
\"", Spec#loglevel.msg_prefix, "(~p:~p:~p) : \"++Format++\"~n\",
[self(), Module, Line | Args])"].
notify_src() -> notify_src() ->
%% Distribute the message to the Erlang error logger %% Distribute the message to the Erlang error logger