From 3579e631b025189b5f0ad3e1536929b84428a492 Mon Sep 17 00:00:00 2001 From: guilhermebodin Date: Thu, 7 Dec 2023 22:48:03 -0300 Subject: [PATCH] add success level --- src/constants.jl | 3 ++- src/logger.jl | 20 +++++++++++++++----- src/logs.jl | 8 ++++++-- test/test_log_levels.jl | 1 + 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/constants.jl b/src/constants.jl index 366f868..34bfd13 100644 --- a/src/constants.jl +++ b/src/constants.jl @@ -1,4 +1,5 @@ -const FatalErrorLevel = Logging.LogLevel(3000) +const FATAL_ERROR_LEVEL = Logging.LogLevel(3000) +const SUCCESS_LEVEL = Logging.LogLevel(1) function set_language(lang::AbstractString) POLYGLOT_LANG[1] = lang diff --git a/src/logger.jl b/src/logger.jl index 7038be6..6046ad9 100644 --- a/src/logger.jl +++ b/src/logger.jl @@ -49,13 +49,15 @@ function choose_terminal_io(level::LogLevel) end function get_level_string(level::LogLevel) - if Logging.Info <= level <= Logging.Error || level == Logging.Debug - return string(level) + if level == SUCCESS_LEVEL + return "Success" + elseif level == FATAL_ERROR_LEVEL + return "Fatal Error" elseif Logging.Debug < level < Logging.Info return "Debug Level" - elseif level == FatalErrorLevel - return "Fatal Error" - end + else + string(level) + end end function get_tag_brackets(level::LogLevel, brackets_dict::Dict) @@ -97,6 +99,7 @@ end "Debug Level" => ["[", "]"], "Debug" => ["[", "]"], "Info" => ["[", "]"], + "Success" => ["[", "]"], "Warn" => ["[", "]"], "Error" => ["[", "]"], "Fatal Error" => ["[", "]"], @@ -106,6 +109,7 @@ end "Debug Level" => "Debug Level", "Debug" => "Debug", "Info" => "Info", + "Success" => "Success", "Warn" => "Warn", "Error" => "Error" ) @@ -114,6 +118,7 @@ end "Debug Level" => :cyan, "Debug" => :cyan, "Info" => :cyan, + "Success" => :green, "Warn" => :yellow, "Error" => :red, "Fatal Error" => :red @@ -123,6 +128,7 @@ end "Debug Level" => false, "Debug" => false, "Info" => false, + "Success" => false, "Warn" => false, "Error" => false, "Fatal Error" => true @@ -137,6 +143,7 @@ function create_polyglot_logger( "Debug Level" => ["[", "]"], "Debug" => ["[", "]"], "Info" => ["[", "]"], + "Success" => ["[", "]"], "Warn" => ["[", "]"], "Error" => ["[", "]"], "Fatal Error" => ["[", "]"], @@ -145,6 +152,7 @@ function create_polyglot_logger( "Debug Level" => "Debug Level", "Debug" => "Debug", "Info" => "Info", + "Success" => "Success", "Warn" => "Warn", "Error" => "Error", "Fatal Error" => "Fatal Error", @@ -153,6 +161,7 @@ function create_polyglot_logger( "Debug Level" => :cyan, "Debug" => :cyan, "Info" => :cyan, + "Success" => :green, "Warn" => :yellow, "Error" => :red, "Fatal Error" => :red, @@ -161,6 +170,7 @@ function create_polyglot_logger( "Debug Level" => false, "Debug" => false, "Info" => false, + "Success" => false, "Warn" => false, "Error" => false, "Fatal Error" => true, diff --git a/src/logs.jl b/src/logs.jl index 35f4b1d..8e5cd4a 100644 --- a/src/logs.jl +++ b/src/logs.jl @@ -8,6 +8,10 @@ function info(msg::AbstractString) @info msg return nothing end +function success(msg::AbstractString) + @logmsg Logging.LogLevel(SUCCESS_LEVEL) msg + return nothing +end function warn(msg::AbstractString) @warn msg return nothing @@ -20,10 +24,10 @@ function fatal_error( msg::AbstractString; exception::Exception = ErrorException("Fatal error"), ) + @logmsg FATAL_ERROR_LEVEL msg + logger = Logging.global_logger() close_polyglot_logger(logger) - - @logmsg FatalErrorLevel msg throw(exception) return nothing end diff --git a/test/test_log_levels.jl b/test/test_log_levels.jl index b00bbc8..a014ca2 100644 --- a/test/test_log_levels.jl +++ b/test/test_log_levels.jl @@ -81,6 +81,7 @@ function test_log_levels_on_file() ) LoggingPolyglot.debug("debug") LoggingPolyglot.info("info") + LoggingPolyglot.success("success") LoggingPolyglot.warn("warn") LoggingPolyglot.non_fatal_error("non_fatal_error") logs_on_file = readlines(log_path)