Skip to content

Commit

Permalink
Add Separator Parameter (#9)
Browse files Browse the repository at this point in the history
* Updates

* Updates

* Updates

* Updates

* Updates

* Updates

* Updates

* Updates
  • Loading branch information
raphasampaio authored Dec 12, 2023
1 parent 06d0c4e commit 90e3a49
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 32 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" => ["[", "]"],
Expand Down Expand Up @@ -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" => ["", ""],
Expand All @@ -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)
Expand Down
82 changes: 55 additions & 27 deletions src/logger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ 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)
level_string = get_level_string(level)

if level_string == "Debug Level"
return string(level_dict[level_string], " ", level.level)
else
return string(level_dict[level_str])
return string(level_dict[level_string])
end
end

Expand All @@ -49,31 +50,36 @@ 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
string(level)
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)
level_string = get_level_string(level)
bracket = bracket_dict[level_string]

if !isempty(tag_brackets)
return tag_brackets
if !isempty(bracket)
return bracket
else
return ["", ""]
end
end

function treat_empty_tag(level_to_print::AbstractString, close_bracket::AbstractString)
if level_to_print == "" && close_bracket == ""
function get_separator(level::LogLevel, close_bracket::AbstractString, separator_dict::Dict)
level_string = get_level_string(level)
separator = separator_dict[level_string]

if level_string == "" && close_bracket == ""
return ""
elseif !isempty(separator)
return separator
else
return " "
end
Expand All @@ -84,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
Expand All @@ -94,7 +100,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" => ["[", "]"],
Expand Down Expand Up @@ -133,13 +139,23 @@ end
"Error" => false,
"Fatal Error" => true
)
* `separator_dict`: Dictionary to select logging tag separator to print. Default:
Dict(
"Debug Level" => " ",
"Debug" => " ",
"Info" => " ",
"Success" => " ",
"Warn" => " ",
"Error" => " ",
"Fatal Error" => " "
)
"""
function create_polyglot_logger(
log_file_path::AbstractString;
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" => ["[", "]"],
Expand Down Expand Up @@ -175,6 +191,15 @@ function create_polyglot_logger(
"Error" => false,
"Fatal Error" => true,
),
separator_dict::Dict = Dict(
"Debug Level" => " ",
"Debug" => " ",
"Info" => " ",
"Success" => " ",
"Warn" => " ",
"Error" => " ",
"Fatal Error" => " ",
),
)
if !append_log
remove_log_file_path_on_logger_creation(log_file_path)
Expand All @@ -183,29 +208,31 @@ 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)
space_before_msg = treat_empty_tag(level_to_print, close_bracket)
open_bracket, close_bracket = get_tag_brackets(args.level, bracket_dict)
separator = get_separator(args.level, 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)

# 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)
space_before_msg = treat_empty_tag(level_to_print, close_bracket)
open_bracket, close_bracket = get_tag_brackets(args.level, bracket_dict)
separator = get_separator(args.level, close_bracket, separator_dict)

println(
io,
now(),
" ",
open_bracket,
level_to_print,
close_bracket,
space_before_msg,
separator,
args.message,
)
end
Expand All @@ -225,12 +252,13 @@ function print_colored(
color_dict::Dict{String, Symbol},
reverse_dict::Dict{String, Bool},
)
level_str = get_level_string(level)
level_string = get_level_string(level)

color = color_dict[level_str]
reverse = reverse_dict[level_str]
color = color_dict[level_string]
reverse = reverse_dict[level_string]

print_colored(io, str; color = color, reverse = reverse)

return nothing
end

Expand Down
2 changes: 1 addition & 1 deletion test/test_logs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down

2 comments on commit 90e3a49

@raphasampaio
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/96956

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.0 -m "<description of version>" 90e3a49aaa412946e3d02e66420e2f91bf6ce833
git push origin v0.3.0

Please sign in to comment.