Repository navigation
Remove Memento and add support for Logging.jl #99
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 1 commit
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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| const _LOGGER = Ref{Logging.ConsoleLogger}() | ||
|
Collaborator
Author
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. This is the main file for the logging. The |
||
|
|
||
| """ | ||
| silence() | ||
|
|
||
| Silence logging within InfrastructureModels. | ||
|
|
||
| This is equivalent to calling `logger_config!("error")`. | ||
| """ | ||
| silence() = logger_config!("error") | ||
|
|
||
| function _meta_formatter(level::Logging.LogLevel, _module, args...) | ||
| return Logging.default_logcolor(level), "$(_module) | $level]:", "" | ||
|
Collaborator
Author
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. Here is where we can change how the log messages are formatted. For now, it just prefaces the log message with something like In future we could add more information like file and line number. Or we could standardise this function into |
||
| end | ||
|
|
||
| function logger_config!(level::Logging.LogLevel) | ||
| _LOGGER[] = | ||
| Logging.ConsoleLogger(stdout, level; meta_formatter = _meta_formatter) | ||
|
Collaborator
Author
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. This function updates the global |
||
| return | ||
| end | ||
|
|
||
| """ | ||
| logger_config!(level::String) | ||
|
|
||
| Set the logging level within PowerModels. `level` must be one of `"error"`, | ||
| `"warn"`, `"info"`, or `"debug"`. | ||
| """ | ||
| function logger_config!(level::String) | ||
| return getfield(Logging, level |> titlecase |> Symbol) |> logger_config! | ||
|
Collaborator
Author
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. This is a bit messy. The other option is a big If you miss-type, you get this error: julia> getfield(Logging, "err" |> titlecase |> Symbol)
ERROR: UndefVarError: `Err` not defined in `Logging`
Suggestion: check for spelling errors or missing imports. |
||
| end | ||
|
|
||
| function _log_if_level(f, level, logger = _LOGGER[]) | ||
| if level >= Logging.min_enabled_level(logger) | ||
| Logging.with_logger(f, logger) | ||
|
Collaborator
Author
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. The |
||
| end | ||
| return | ||
| end | ||
|
|
||
| macro _error(msg) | ||
| return quote | ||
| $_log_if_level(() -> @error($msg), $(Logging.Error)) | ||
| error($msg) | ||
|
Collaborator
Author
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. Note that |
||
| end |> esc | ||
| end | ||
|
|
||
| macro _warn(msg) | ||
| return :($_log_if_level(() -> @warn($msg), $(Logging.Warn)) )|> esc | ||
| end | ||
|
|
||
| macro _debug(msg) | ||
| return :($_log_if_level(() -> @debug($msg), $(Logging.Debug))) |> esc | ||
| end | ||
|
|
||
| macro _info(msg) | ||
| return :($_log_if_level(() -> @info($msg), $(Logging.Info)) )|> esc | ||
| end | ||
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.
Same as
Memento.register, we need to enable the logger on__init__.