diff --git a/src/logger.jl b/src/logger.jl index fca5d5e204..c8405010ec 100644 --- a/src/logger.jl +++ b/src/logger.jl @@ -2,6 +2,18 @@ import Logging, Dates export ModelLogger, shouldlog, min_enabled_level, catch_exceptions, handle_message +# ------------------------------------------------------------------------------------- +# Custom LogLevels + +const Diagnostic = Logging.LogLevel(-500) # Sits between Debug and Info +const Setup = Logging.LogLevel(500) + +# ------------------------------------------------------------------------------------- +# Custom Logging Macros + +macro diagnostic( msg ) @Logging.logmsg Diagnostic msg end +macro setup( msg ) @Logging.logmsg Setup msg end + # ------------------------------------------------------------------------------------ # ModelLogger _model_logger_docs = """ @@ -10,7 +22,7 @@ _model_logger_docs = """ Based on Logging.SimpleLogger it tries to log all messages in the following format - [dd/mm/yyyy HH:MM:SS] module source_file:line_number: message + message --- [dd/mm/yyyy HH:MM:SS] log_level source_file:line_number The logger will handle any message from @debug up. """ @@ -27,6 +39,14 @@ Logging.min_enabled_level(logger::ModelLogger) = logger.min_level Logging.catch_exceptions(logger::ModelLogger) = false +function level_to_string(level::Logging.LogLevel) + if level == Diagnostic "Diagnostic" + elseif level == Setup "Setup" + elseif level == Logging.Warn "Warning" + else string(level) + end +end + function Logging.handle_message(logger::ModelLogger, level, message, _module, group, id, filepath, line; maxlog = nothing, kwargs...) if maxlog !== nothing && maxlog isa Integer remaining = get!(logger.message_limits, id, maxlog) @@ -35,23 +55,13 @@ function Logging.handle_message(logger::ModelLogger, level, message, _module, gr end buf = IOBuffer() iob = IOContext(buf, logger.stream) - level = level == Logging.Warn ? "Warning" : string(level) + level_name = level_to_string(level) module_name = something(_module, "nothing") file_name = something(filepath, "nothing") line_number = something(line, "nothing") msg_timestamp = Dates.format(Dates.now(), "[dd/mm/yyyy HH:MM:SS]") - formatted_message = "$msg_timestamp $module_name $file_name.jl:$line_number: $message" + formatted_message = "$message --- $msg_timestamp $level_name $file_name:$line_number" println(iob, formatted_message) write(logger.stream, take!(buf)) nothing end - -# ------------------------------------------------------------------------------------- -# Custom LogStates - -const Diagnostic = Logging.LogLevel(-500) # Sits between Debug and Info - -# ------------------------------------------------------------------------------------- -# Custom Logging Macros - -# macro diagnostic(exs...) Logging.logmsg_code((@Logging._sourceinfo)..., :Diagnostic, exs...) end \ No newline at end of file