From 75b35c6ae67830be0550f5075a6a7993f402437b Mon Sep 17 00:00:00 2001 From: raphasampaio Date: Tue, 12 Dec 2023 10:49:56 -0300 Subject: [PATCH 1/8] Updates --- README.md | 8 ++++---- src/logger.jl | 52 ++++++++++++++++++++++++++++++++--------------- test/test_logs.jl | 2 +- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 75e3ded..d498a95 100644 --- a/README.md +++ b/README.md @@ -94,9 +94,9 @@ The arguments that can be passed using `LoggingPolyglot.create_polyglot_logger`: * `min_level_console`: Minimum level shown in console. Default: Logging.Info * `min_level_file`: Minimum level shown in file. Default: Logging.Debug * `append_log`: Boolean input to append logs in existing log file (if true) or overwrite/create log file (if false). Default is false -* `brackets_dict`: select the brackets for each LogLevel. As default, +* `bracket_dict`: select the brackets for each LogLevel. As default, ```julia -brackets_dict = Dict( +bracket_dict = Dict( "Debug Level" => ["[", "]"], "Debug" => ["[", "]"], "Info" => ["[", "]"], @@ -173,7 +173,7 @@ LoggingPolyglot.create_polyglot_logger(log_file; level_dict, color_dict, backgro The next example shows how to remove the `info` tag ```julia log_file = "my_application.log" -brackets_dict = Dict( +bracket_dict = Dict( "Debug Level" => ["[", "]"], "Debug" => ["[", "]"], "Info" => ["", ""], @@ -189,7 +189,7 @@ level_dict = Dict( "Error" => "Error", "Fatal Error" => "Fatal Error", ) -LoggingPolyglot.create_polyglot_logger(log_file; brackets_dict, level_dict) +LoggingPolyglot.create_polyglot_logger(log_file; bracket_dict, level_dict) LoggingPolyglot.info("info msg") LoggingPolyglot.warn("warn msg") LoggingPolyglot.remove_log_file_path_on_logger_creation(log_file) diff --git a/src/logger.jl b/src/logger.jl index c55b3a5..23fc157 100644 --- a/src/logger.jl +++ b/src/logger.jl @@ -32,11 +32,11 @@ function remove_log_file_path_on_logger_creation(path::AbstractString) end function choose_level_to_print(level::LogLevel, level_dict::Dict) - level_str = get_level_string(level) - if level_str == "Debug Level" - return string(level_dict[level_str], " ", level.level) + key = get_level_string(level) + if key == "Debug Level" + return string(level_dict[key], " ", level.level) else - return string(level_dict[level_str]) + return string(level_dict[key]) end end @@ -58,17 +58,28 @@ function get_level_string(level::LogLevel) end end -function get_tag_brackets(level::LogLevel, brackets_dict::Dict) - level_str = get_level_string(level) - tag_brackets = brackets_dict[level_str] +function get_tag_brackets(level::LogLevel, bracket_dict::Dict) + key = get_level_string(level) + value = bracket_dict[key] - if !isempty(tag_brackets) - return tag_brackets + if !isempty(value) + return value else return ["", ""] end end +function get_separator(level::LogLevel, separator_dict::Dict) + key = get_level_string(level) + value = separator_dict[key] + + if !isempty(value) + return value + else + return "" + end +end + function treat_empty_tag(level_to_print::AbstractString, close_bracket::AbstractString) if level_to_print == "" && close_bracket == "" return "" @@ -92,7 +103,7 @@ end * `min_level_console`: Minimum level shown in console. Default: Logging.Info * `min_level_file`: Minimum level shown in file. Default: Logging.Debug * `append_log`: Boolean input to append logs in existing log file (if true) or overwrite/create log file (if false). Default is false -* `brackets_dict`: select the brackets for each LogLevel. As default, +* `bracket_dict`: select the brackets for each LogLevel. As default, Dict( "Debug Level" => ["[", "]"], "Debug" => ["[", "]"], @@ -133,7 +144,7 @@ function create_polyglot_logger( min_level_console::Logging.LogLevel = Logging.Info, min_level_file::Logging.LogLevel = Logging.Debug, append_log::Bool = false, - brackets_dict::Dict = Dict( + bracket_dict::Dict = Dict( "Debug Level" => ["[", "]"], "Debug" => ["[", "]"], "Info" => ["[", "]"], @@ -165,6 +176,14 @@ function create_polyglot_logger( "Error" => false, "Fatal Error" => true, ), + separator_dict::Dict = Dict( + "Debug Level" => " ", + "Debug" => " ", + "Info" => " ", + "Warn" => " ", + "Error" => " ", + "Fatal Error" => " ", + ), ) if !append_log remove_log_file_path_on_logger_creation(log_file_path) @@ -173,7 +192,7 @@ function create_polyglot_logger( # console logger only min_level_console and up format_logger_console = FormatLogger() do io, args level_to_print = choose_level_to_print(args.level, level_dict) - open_bracket, close_bracket = get_tag_brackets(args.level, brackets_dict) + open_bracket, close_bracket = get_tag_brackets(args.level, bracket_dict) space_before_msg = treat_empty_tag(level_to_print, close_bracket) io = choose_terminal_io(args.level) print(io, open_bracket) @@ -186,7 +205,7 @@ function create_polyglot_logger( # file logger logs min_level_file and up format_logger_file = FormatLogger(log_file_path; append = true) do io, args level_to_print = choose_level_to_print(args.level, level_dict) - open_bracket, close_bracket = get_tag_brackets(args.level, brackets_dict) + open_bracket, close_bracket = get_tag_brackets(args.level, bracket_dict) space_before_msg = treat_empty_tag(level_to_print, close_bracket) println( io, @@ -215,12 +234,13 @@ function print_colored( color_dict::Dict{String, Symbol}, reverse_dict::Dict{String, Bool}, ) - level_str = get_level_string(level) + key = get_level_string(level) - color = color_dict[level_str] - reverse = reverse_dict[level_str] + color = color_dict[key] + reverse = reverse_dict[key] print_colored(io, str; color = color, reverse = reverse) + return nothing end diff --git a/test/test_logs.jl b/test/test_logs.jl index 06facd6..de32d43 100644 --- a/test/test_logs.jl +++ b/test/test_logs.jl @@ -218,7 +218,7 @@ function test_empty_brackets() logger_path = "brackets.log" logger = LoggingPolyglot.create_polyglot_logger( logger_path; - brackets_dict = brackets, + bracket_dict = brackets, level_dict = level, ) From b0aeec3bd6ae06a327a557fbd606d3d54bf50d63 Mon Sep 17 00:00:00 2001 From: raphasampaio Date: Tue, 12 Dec 2023 10:59:06 -0300 Subject: [PATCH 2/8] Updates --- src/logger.jl | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/logger.jl b/src/logger.jl index 086fa9f..b332658 100644 --- a/src/logger.jl +++ b/src/logger.jl @@ -56,7 +56,7 @@ function get_level_string(level::LogLevel) elseif Logging.Debug < level < Logging.Info return "Debug Level" else - string(level) + return string(level) end end @@ -71,20 +71,14 @@ function get_tag_brackets(level::LogLevel, bracket_dict::Dict) end end -function get_separator(level::LogLevel, separator_dict::Dict) +function get_separator(level::LogLevel, close_bracket::AbstractString, separator_dict::Dict) key = get_level_string(level) value = separator_dict[key] - if !isempty(value) - return value - else - return "" - end -end - -function treat_empty_tag(level_to_print::AbstractString, close_bracket::AbstractString) if level_to_print == "" && close_bracket == "" return "" + elseif !isempty(value) + return value else return " " end @@ -203,11 +197,12 @@ function create_polyglot_logger( format_logger_console = FormatLogger() do io, args level_to_print = choose_level_to_print(args.level, level_dict) open_bracket, close_bracket = get_tag_brackets(args.level, bracket_dict) - space_before_msg = treat_empty_tag(level_to_print, close_bracket) + separator = get_separator(level_to_print, close_bracket, separator_dict) io = choose_terminal_io(args.level) + print(io, open_bracket) print_colored(io, level_to_print, args.level, color_dict, background_reverse_dict) - println(io, close_bracket, space_before_msg, args.message) + println(io, close_bracket, separator, args.message) end console_logger = MinLevelLogger(format_logger_console, min_level_console) @@ -216,7 +211,8 @@ function create_polyglot_logger( format_logger_file = FormatLogger(log_file_path; append = true) do io, args level_to_print = choose_level_to_print(args.level, level_dict) open_bracket, close_bracket = get_tag_brackets(args.level, bracket_dict) - space_before_msg = treat_empty_tag(level_to_print, close_bracket) + separator = get_separator(level_to_print, close_bracket, separator_dict) + println( io, now(), @@ -224,7 +220,7 @@ function create_polyglot_logger( open_bracket, level_to_print, close_bracket, - space_before_msg, + separator, args.message, ) end From d5a9d74c1f5826193736095d8593c7e174214feb Mon Sep 17 00:00:00 2001 From: raphasampaio Date: Tue, 12 Dec 2023 11:10:50 -0300 Subject: [PATCH 3/8] Updates --- src/logger.jl | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/logger.jl b/src/logger.jl index 0251c3d..b9ca87e 100644 --- a/src/logger.jl +++ b/src/logger.jl @@ -33,10 +33,12 @@ end function choose_level_to_print(level::LogLevel, level_dict::Dict) key = get_level_string(level) + value = level_dict[key] + if key == "Debug Level" - return string(level_dict[key], " ", level.level) + return string(value, " ", level.level) else - return string(level_dict[key]) + return string(value) end end @@ -138,6 +140,15 @@ end "Error" => false, "Fatal Error" => true ) +* `separator_dict`: Dictionary to select logging tag separator to print. Default: + Dict( + "Debug Level" => " ", + "Debug" => " ", + "Info" => " ", + "Warn" => " ", + "Error" => " ", + "Fatal Error" => " " + ) """ function create_polyglot_logger( log_file_path::AbstractString; From 6103a36d40b7958d074b422e8b741f978ab276e9 Mon Sep 17 00:00:00 2001 From: raphasampaio Date: Tue, 12 Dec 2023 11:11:22 -0300 Subject: [PATCH 4/8] Updates --- Project.toml | 2 +- src/logger.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index d0564a0..2801b14 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "LoggingPolyglot" uuid = "211639cc-9b11-4cfd-abc6-8f7477829344" -version = "0.2.2" +version = "0.2.3" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/logger.jl b/src/logger.jl index b9ca87e..51d5d5f 100644 --- a/src/logger.jl +++ b/src/logger.jl @@ -198,7 +198,7 @@ function create_polyglot_logger( "Warn" => " ", "Error" => " ", "Fatal Error" => " ", - ), + ), ) if !append_log remove_log_file_path_on_logger_creation(log_file_path) From 3ab5550b86c124695168280dbffcfb1500d82658 Mon Sep 17 00:00:00 2001 From: raphasampaio Date: Tue, 12 Dec 2023 11:12:33 -0300 Subject: [PATCH 5/8] Updates --- src/logger.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/logger.jl b/src/logger.jl index 51d5d5f..46ad8b2 100644 --- a/src/logger.jl +++ b/src/logger.jl @@ -51,14 +51,14 @@ function choose_terminal_io(level::LogLevel) end function get_level_string(level::LogLevel) - if level == SUCCESS_LEVEL - return "Success" + return if level == SUCCESS_LEVEL + "Success" elseif level == FATAL_ERROR_LEVEL - return "Fatal Error" + "Fatal Error" elseif Logging.Debug < level < Logging.Info - return "Debug Level" + "Debug Level" else - return string(level) + string(level) end end From 476300bfb1a0c01dca1c7845f20527ed584a56bf Mon Sep 17 00:00:00 2001 From: raphasampaio Date: Tue, 12 Dec 2023 11:38:08 -0300 Subject: [PATCH 6/8] Updates --- src/logger.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logger.jl b/src/logger.jl index 46ad8b2..9afefb1 100644 --- a/src/logger.jl +++ b/src/logger.jl @@ -52,7 +52,7 @@ end function get_level_string(level::LogLevel) return if level == SUCCESS_LEVEL - "Success" + "Success" elseif level == FATAL_ERROR_LEVEL "Fatal Error" elseif Logging.Debug < level < Logging.Info From 648219c71718acf7e6253adc542c4777ed4ec218 Mon Sep 17 00:00:00 2001 From: raphasampaio Date: Tue, 12 Dec 2023 11:44:20 -0300 Subject: [PATCH 7/8] Updates --- src/logger.jl | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/src/logger.jl b/src/logger.jl index 9afefb1..d624ef5 100644 --- a/src/logger.jl +++ b/src/logger.jl @@ -32,13 +32,12 @@ function remove_log_file_path_on_logger_creation(path::AbstractString) end function choose_level_to_print(level::LogLevel, level_dict::Dict) - key = get_level_string(level) - value = level_dict[key] + level_string = get_level_string(level) - if key == "Debug Level" - return string(value, " ", level.level) + if level_string == "Debug Level" + return string(level_dict[level_string], " ", level.level) else - return string(value) + return string(level_dict[level_string]) end end @@ -63,24 +62,24 @@ function get_level_string(level::LogLevel) end function get_tag_brackets(level::LogLevel, bracket_dict::Dict) - key = get_level_string(level) - value = bracket_dict[key] + level_string = get_level_string(level) + bracket = bracket_dict[level_string] - if !isempty(value) - return value + if !isempty(bracket) + return bracket else return ["", ""] end end function get_separator(level::LogLevel, close_bracket::AbstractString, separator_dict::Dict) - key = get_level_string(level) - value = separator_dict[key] + level_string = get_level_string(level) + separator = separator_dict[level_string] - if level_to_print == "" && close_bracket == "" + if level_string == "" && close_bracket == "" return "" - elseif !isempty(value) - return value + elseif !isempty(separator) + return separator else return " " end @@ -208,7 +207,7 @@ function create_polyglot_logger( format_logger_console = FormatLogger() do io, args level_to_print = choose_level_to_print(args.level, level_dict) open_bracket, close_bracket = get_tag_brackets(args.level, bracket_dict) - separator = get_separator(level_to_print, close_bracket, separator_dict) + separator = get_separator(args.level, close_bracket, separator_dict) io = choose_terminal_io(args.level) print(io, open_bracket) @@ -222,7 +221,7 @@ function create_polyglot_logger( format_logger_file = FormatLogger(log_file_path; append = true) do io, args level_to_print = choose_level_to_print(args.level, level_dict) open_bracket, close_bracket = get_tag_brackets(args.level, bracket_dict) - separator = get_separator(level_to_print, close_bracket, separator_dict) + separator = get_separator(args.level, close_bracket, separator_dict) println( io, @@ -251,10 +250,10 @@ function print_colored( color_dict::Dict{String, Symbol}, reverse_dict::Dict{String, Bool}, ) - key = get_level_string(level) + level_string = get_level_string(level) - color = color_dict[key] - reverse = reverse_dict[key] + color = color_dict[level_string] + reverse = reverse_dict[level_string] print_colored(io, str; color = color, reverse = reverse) From 4d780afb17202194933d8a6a167ee980d9418c4b Mon Sep 17 00:00:00 2001 From: raphasampaio Date: Tue, 12 Dec 2023 11:50:40 -0300 Subject: [PATCH 8/8] Updates --- Project.toml | 2 +- src/logger.jl | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 2801b14..5736efa 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "LoggingPolyglot" uuid = "211639cc-9b11-4cfd-abc6-8f7477829344" -version = "0.2.3" +version = "0.3.0" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/logger.jl b/src/logger.jl index d624ef5..7f8dd2f 100644 --- a/src/logger.jl +++ b/src/logger.jl @@ -90,7 +90,7 @@ end log_file_path::AbstractString; min_level_console::Logging.LogLevel, min_level_file::Logging.LogLevel, - brackets, + bracket_dict, level_dict, color_dict, background_reverse_dict @@ -144,6 +144,7 @@ end "Debug Level" => " ", "Debug" => " ", "Info" => " ", + "Success" => " ", "Warn" => " ", "Error" => " ", "Fatal Error" => " " @@ -194,6 +195,7 @@ function create_polyglot_logger( "Debug Level" => " ", "Debug" => " ", "Info" => " ", + "Success" => " ", "Warn" => " ", "Error" => " ", "Fatal Error" => " ",