Skip to content

Commit

Permalink
Merge pull request #7 from psrenergy/gb/add_success_level
Browse files Browse the repository at this point in the history
add success level
  • Loading branch information
raphasampaio authored Dec 12, 2023
2 parents f822c0c + 5067a46 commit 60ef3cf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/constants.jl
Original file line number Diff line number Diff line change
@@ -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(language::AbstractString)
POLYGLOT_LANGUAGE[1] = language
Expand Down
20 changes: 15 additions & 5 deletions src/logger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -97,6 +99,7 @@ end
"Debug Level" => ["[", "]"],
"Debug" => ["[", "]"],
"Info" => ["[", "]"],
"Success" => ["[", "]"],
"Warn" => ["[", "]"],
"Error" => ["[", "]"],
"Fatal Error" => ["[", "]"],
Expand All @@ -106,6 +109,7 @@ end
"Debug Level" => "Debug Level",
"Debug" => "Debug",
"Info" => "Info",
"Success" => "Success",
"Warn" => "Warn",
"Error" => "Error"
)
Expand All @@ -114,6 +118,7 @@ end
"Debug Level" => :cyan,
"Debug" => :cyan,
"Info" => :cyan,
"Success" => :green,
"Warn" => :yellow,
"Error" => :red,
"Fatal Error" => :red
Expand All @@ -123,6 +128,7 @@ end
"Debug Level" => false,
"Debug" => false,
"Info" => false,
"Success" => false,
"Warn" => false,
"Error" => false,
"Fatal Error" => true
Expand All @@ -137,6 +143,7 @@ function create_polyglot_logger(
"Debug Level" => ["[", "]"],
"Debug" => ["[", "]"],
"Info" => ["[", "]"],
"Success" => ["[", "]"],
"Warn" => ["[", "]"],
"Error" => ["[", "]"],
"Fatal Error" => ["[", "]"],
Expand All @@ -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",
Expand All @@ -153,6 +161,7 @@ function create_polyglot_logger(
"Debug Level" => :cyan,
"Debug" => :cyan,
"Info" => :cyan,
"Success" => :green,
"Warn" => :yellow,
"Error" => :red,
"Fatal Error" => :red,
Expand All @@ -161,6 +170,7 @@ function create_polyglot_logger(
"Debug Level" => false,
"Debug" => false,
"Info" => false,
"Success" => false,
"Warn" => false,
"Error" => false,
"Fatal Error" => true,
Expand Down
9 changes: 7 additions & 2 deletions src/logs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ function info(msg::AbstractString)
return nothing
end

function success(msg::AbstractString)
@logmsg Logging.LogLevel(SUCCESS_LEVEL) msg
return nothing
end

function warn(msg::AbstractString)
@warn msg
return nothing
Expand All @@ -24,10 +29,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
Expand Down
1 change: 1 addition & 0 deletions test/test_log_levels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 60ef3cf

Please sign in to comment.