Created
August 2, 2012 15:43
-
-
Save w495/3238037 to your computer and use it in GitHub Desktop.
lager_app improvement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% | |
%% ... | |
%% | |
start(_StartType, _StartArgs) -> | |
%% until lager is completely started, allow all messages to go through | |
lager_mochiglobal:put(loglevel, {?DEBUG, []}), | |
{ok, Pid} = lager_sup:start_link(), | |
Handlers = case application:get_env(lager, handlers) of | |
undefined -> | |
[{lager_console_backend, info}, | |
{lager_file_backend, [{"log/error.log", error, 10485760, "", 5}, | |
{"log/console.log", info, 10485760, "", 5}]}]; | |
{ok, Val} -> | |
{Date, Time} = lager_util:format_time(), | |
Node = erlang:atom_to_list(node()), | |
Raw_file_backends = proplists:get_value(lager_file_backend, Val), | |
Rest_backends = proplists:delete(lager_file_backend, Val), | |
File_backends = lists:map( | |
fun | |
({File,Level,Size,Rotate,Index}) -> | |
{ | |
re:replace( | |
re:replace( | |
re:replace(File, "~[{]node[(][)][}]", Node, [{return, list}]), | |
"~[{]date[(][)][}]", Date, [{return, list}]), | |
"~[{]time[(][)][}]", Time, [{return, list}]), | |
Level, | |
Size, | |
Rotate, | |
Index | |
}; | |
(X)-> X | |
end, | |
Raw_file_backends | |
), | |
[{lager_file_backend, File_backends}|Rest_backends] | |
end, | |
%% handlers failing to start are handled in the handler_watcher | |
_ = [supervisor:start_child(lager_handler_watcher_sup, [lager_event, Module, Config]) || | |
{Module, Config} <- expand_handlers(Handlers)], | |
%% mask the messages we have no use for | |
MinLog = lager:minimum_loglevel(lager:get_loglevels()), | |
{_, Traces} = lager_mochiglobal:get(loglevel), | |
lager_mochiglobal:put(loglevel, {MinLog, Traces}), | |
SavedHandlers = case application:get_env(lager, error_logger_redirect) of | |
{ok, false} -> | |
[]; | |
_ -> | |
case supervisor:start_child(lager_handler_watcher_sup, [error_logger, error_logger_lager_h, []]) of | |
{ok, _} -> | |
%% Should we allow user to whitelist handlers to not be removed? | |
[begin error_logger:delete_report_handler(X), X end || | |
X <- gen_event:which_handlers(error_logger) -- [error_logger_lager_h]]; | |
{error, _} -> | |
[] | |
end | |
end, | |
{ok, Pid, SavedHandlers}. | |
%% | |
%% ... | |
%% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
%% ... | |
%% What handlers to install with what arguments | |
{lager, [ | |
{handlers, [ | |
{lager_console_backend, info}, | |
{lager_file_backend, [ | |
{"priv/logs/~{node()}/~{date()}/~{time()}/debug.log", debug, 10485760, "$D0", 5}, | |
{"priv/logs/~{node()}/~{date()}/~{time()}/info.log", info, 10485760, "$D0", 5}, | |
{"priv/logs/~{node()}/~{date()}/~{time()}/notice.log", notice, 10485760, "$D0", 5}, | |
{"priv/logs/~{node()}/~{date()}/warning.log", warning, 10485760, "$D0", 5}, | |
{"priv/logs/~{node()}/~{date()}/error.log", error, 10485760, "$D0", 5}, | |
{"priv/logs/~{node()}/critical.log", critical, 10485760, "$D0", 5}, | |
{"priv/logs/~{node()}/alert.log", alert, 10485760, "$D0", 5}, | |
{"priv/logs/emergency.log", emergency, 10485760, "$D0", 5} | |
]} | |
]}, | |
%% Whether to write a crash log, and where. Undefined means no crash logger. | |
{crash_log, "priv/logs/crash.log"}, | |
%% Maximum size in bytes of events in the crash log - defaults to 65536 | |
{crash_log_msg_size, 65536}, | |
%% Maximum size of the crash log in bytes, before its rotated, set | |
%% to 0 to disable rotation - default is 0 | |
{crash_log_size, 10485760}, | |
%% What time to rotate the crash log - default is no time | |
%% rotation. See the README for a description of this format. | |
{crash_log_date, "$D0"}, | |
%% Number of rotated crash logs to keep, 0 means keep only the | |
%% current one - default is 0 | |
{crash_log_count, 5}, | |
%% Whether to redirect error_logger messages into lager - defaults to true | |
{error_logger_redirect, true} | |
]}, | |
%% ... | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment