-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Add @create_log_macro
for making custom styled logging macros
#52196
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
Changes from all commits
7a91126
a80071e
a910e8a
78f4369
fb49856
bd9064d
b196e65
9345c5d
4b46117
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ for sym in [ | |
Symbol("@warn"), | ||
Symbol("@error"), | ||
Symbol("@logmsg"), | ||
:custom_log_levels, | ||
:with_logger, | ||
:current_logger, | ||
:global_logger, | ||
|
@@ -29,6 +30,41 @@ for sym in [ | |
@eval const $sym = Base.CoreLogging.$sym | ||
end | ||
|
||
""" | ||
@create_log_macro(name::Symbol, level::Int, color::Union{Int,Symbol}) | ||
|
||
Creates a custom log macro like `@info`, `@warn` etc. with a given `name`, `level` and | ||
`color`. The macro created is named with the lowercase form of `name` but the given form | ||
is used for the printing. | ||
|
||
The available color keys can be seen by typing `Base.text_colors` in the help mode of the REPL | ||
|
||
```julia-repl | ||
julia> @create_log_macro(:MyLog, 200, :magenta) | ||
@mylog (macro with 1 method) | ||
|
||
julia> @mylog "hello" | ||
[ MyLog: hello | ||
``` | ||
""" | ||
macro create_log_macro(name, level, color) | ||
macro_name = Symbol(lowercase(string(name))) | ||
macro_string = QuoteNode(name) | ||
loglevel = LogLevel(level) | ||
if loglevel in (BelowMinLevel, Debug, Info, Warn, Error, AboveMaxLevel) | ||
throw(ArgumentError("Cannot use the same log level as a built in log macro")) | ||
end | ||
if haskey(custom_log_levels, loglevel) | ||
throw(ArgumentError("Custom log macro already exists for given log level")) | ||
end | ||
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. I guess we're ok with reusing existing colors? Maybe it would be helpful to at least note the colors the builtin log levels are already using? 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. I think it's ok to leave that up to the user. They can see the colors of existing logs and figure out what they want from that I think |
||
quote | ||
$(custom_log_levels)[$(esc(loglevel))] = ($(macro_string), $(esc(color))) | ||
macro $(esc(macro_name))(exs...) | ||
$(Base.CoreLogging.logmsg_code)(($(Base.CoreLogging.@_sourceinfo))..., $(esc(loglevel)), exs...) | ||
end | ||
end | ||
end | ||
|
||
# LogLevel aliases (re-)documented here (JuliaLang/julia#40978) | ||
""" | ||
Debug | ||
|
@@ -67,6 +103,7 @@ export | |
@warn, | ||
@error, | ||
@logmsg, | ||
@create_log_macro, | ||
with_logger, | ||
current_logger, | ||
global_logger, | ||
|
Uh oh!
There was an error while loading. Please reload this page.