-
Notifications
You must be signed in to change notification settings - Fork 455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable lager to use the OTP logger's event pipeline #524
Open
Vagabond
wants to merge
35
commits into
master
Choose a base branch
from
adt/lager_use_logger-option
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
e1c4ed1
Add a `lager_use_logger` parse transform to rewrite lager calls to lo…
Vagabond f1c7e3a
Couple fixes
Vagabond 7aaad5c
Handle log messages with no format arguments
Vagabond a68e456
Backwards
Vagabond ad25bf8
Add an env var to stop lager actually booting
Vagabond 62d95d2
Add a module to convert logger formatters to lager formatters
Vagabond 0970513
Clear colors at end of line
Vagabond e53ecb0
Typo
Vagabond 8a2cf8d
Add some formatters for the standard OTP reports
Vagabond 0176941
Fallback for unhandled reports
Vagabond c793c03
Tweak
Vagabond 9b8ee23
Honor supervisor message suppression config
Vagabond 4f8d245
Feh
Vagabond 9ed7ee8
Add supervisor error reports
Vagabond 8ac924b
Debugging
Vagabond 7e247dc
Typo:
Vagabond 0ba40a2
Add application start
Vagabond 24651de
Use the right key names
Vagabond 0e07296
Add application stops
Vagabond 5667c95
Try to switch to using a report_cb
Vagabond 7b8d2c5
More fighting with report_cb
Vagabond ce11e41
Use report_cb from config or from metadata
Vagabond 7af01cc
Try to supress log messages better
Vagabond 566d0f3
Fix
Vagabond 60f6b38
Typo
Vagabond 2259907
Fix
Vagabond 3d38191
Fix
Vagabond 3c275e4
Use better function
Vagabond 0803df0
How the heck did this slip in?
Vagabond 0dff911
Add some docs
Vagabond c62732d
WIP function to turn lager config into logger config
Vagabond 6746643
Rework logger configuration and add logger config generator
Vagabond 244463e
logger_std_h supports file rotation better than disk_log, so use that
Vagabond 1121d28
Handle columns in transform
Vagabond 20ee759
Attempt to handle modern wacky crash reports better
Vagabond File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,8 @@ | |
get_loglevel/1, get_loglevel/2, set_loglevel/2, set_loglevel/3, set_loglevel/4, get_loglevels/1, | ||
update_loglevel_config/1, posix_error/1, set_loghwm/2, set_loghwm/3, set_loghwm/4, | ||
safe_format/3, safe_format_chop/3, unsafe_format/2, dispatch_log/5, dispatch_log/7, dispatch_log/9, | ||
do_log/9, do_log/10, do_log_unsafe/10, pr/2, pr/3, pr_stacktrace/1, pr_stacktrace/2]). | ||
do_log/9, do_log/10, do_log_unsafe/10, pr/2, pr/3, pr_stacktrace/1, pr_stacktrace/2, | ||
generate_logger_config/0, configure_logger/0]). | ||
|
||
-type log_level() :: none | debug | info | notice | warning | error | critical | alert | emergency. | ||
-type log_level_number() :: 0..7. | ||
|
@@ -689,6 +690,65 @@ rotate_handler(Handler) -> | |
rotate_handler(Handler, Sink) -> | ||
gen_event:call(Sink, Handler, rotate, ?ROTATE_TIMEOUT). | ||
|
||
generate_logger_config() -> | ||
Handlers = application:get_env(lager, handlers, lager_app:default_handlers()), | ||
{Level, NewHandlers} = generate_logger_handlers(Handlers, {notice, []}), | ||
{kernel, [{logger_level, Level}, {logger, NewHandlers}]}. | ||
|
||
configure_logger() -> | ||
Handlers = application:get_env(lager, handlers, lager_app:default_handlers()), | ||
WhitelistedLoggerHandlers = application:get_env(lager, whitelisted_logger_handlers, []), | ||
[ ok = logger:remove_handler(Id) || #{id := Id} <- logger:get_handler_config(), not lists:member(Id, WhitelistedLoggerHandlers) ], | ||
{Level, NewHandlers} = generate_logger_handlers(Handlers, {notice, []}), | ||
logger:set_primary_config(maps:merge(logger:get_primary_config(), #{level => Level})), | ||
[ ok = logger:add_handler(HandlerId, HandlerModule, HandlerConfig) || {handler, HandlerId, HandlerModule, HandlerConfig} <- NewHandlers ], | ||
ok. | ||
|
||
generate_logger_handlers([], Acc) -> | ||
Acc; | ||
generate_logger_handlers([{lager_console_backend, Config}|Tail], {CurrentLevel, Acc}) -> | ||
Level = proplists:get_value(level, Config, info), | ||
Formatter = proplists:get_value(formatter, Config, lager_default_formatter), | ||
FormatterConfig = proplists:get_value(formatter_config, Config, []), | ||
Handler = {handler, console, logger_std_h, #{level => Level, formatter => | ||
{lager_logger_formatter, #{report_cb => fun lager_logger_formatter:report_cb/1, | ||
formatter => Formatter, | ||
formatter_config => FormatterConfig}}}}, | ||
NewLevel = case lager_util:level_to_num(Level) > lager_util:level_to_num(CurrentLevel) of | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of that you can use |
||
true -> | ||
Level; | ||
false -> | ||
CurrentLevel | ||
end, | ||
generate_logger_handlers(Tail, {NewLevel, [Handler|Acc]}); | ||
generate_logger_handlers([{lager_file_backend, Config}|Tail], {CurrentLevel, Acc}) -> | ||
Level = proplists:get_value(level, Config, info), | ||
File = proplists:get_value(file, Config), | ||
LogRoot = application:get_env(lager, log_root, ""), | ||
Size = proplists:get_value(size, Config, 10485760), | ||
Count = proplists:get_value(count, Config, 5), | ||
Formatter = proplists:get_value(formatter, Config, lager_default_formatter), | ||
FormatterConfig = proplists:get_value(formatter_config, Config, []), | ||
%% the standard log handler has a file mode with size based rotation support that is much saner than | ||
%% disk_log's, so use that here | ||
Handler = {handler, list_to_atom(File), logger_std_h, #{level => Level, | ||
config => #{type => file, | ||
file => filename:join(LogRoot, File), | ||
max_no_files => Count, | ||
max_no_bytes => Size}, | ||
formatter => | ||
{lager_logger_formatter, #{report_cb => fun lager_logger_formatter:report_cb/1, | ||
formatter => Formatter, | ||
formatter_config => FormatterConfig}}}}, | ||
NewLevel = case lager_util:level_to_num(Level) > lager_util:level_to_num(CurrentLevel) of | ||
true -> | ||
Level; | ||
false -> | ||
CurrentLevel | ||
end, | ||
generate_logger_handlers(Tail, {NewLevel, [Handler|Acc]}). | ||
|
||
|
||
%% @private | ||
trace_func(#trace_func_state_v1{pid=Pid, level=Level, format_string=Fmt}=FuncState, Event, ProcState) -> | ||
lager:log(Level, Pid, Fmt, [Event, ProcState]), | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe instead of "translating" Lager handlers (which will not support custom handlers anyway) to logger handlers, add new handler that will forward all messages from the logger to lager's
gen_event
as a compatibility layer.