Skip to content

Commit

Permalink
change the name to LoggingPolyglot
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermebodin committed Sep 14, 2023
1 parent 56e2a84 commit 06540df
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 144 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/aqua.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ jobs:
using Pkg
Pkg.add(PackageSpec(name="Aqua"))
Pkg.develop(PackageSpec(path=pwd()))
using Polyglot, Aqua
Aqua.test_all(Polyglot; unbound_args = false)
using LoggingPolyglot, Aqua
Aqua.test_all(LoggingPolyglot; unbound_args = false)
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = "Polyglot"
name = "LoggingPolyglot"
uuid = "211639cc-9b11-4cfd-abc6-8f7477829344"
version = "0.1.0"

Expand Down
62 changes: 31 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Polyglot.jl
# LoggingPolyglot.jl

[build-img]: https://github.com/psrenergy/Polyglot.jl/workflows/CI/badge.svg?branch=master
[build-url]: https://github.com/psrenergy/Polyglot.jl/actions?query=workflow%3ACI
[build-img]: https://github.com/psrenergy/LoggingPolyglot.jl/workflows/CI/badge.svg?branch=master
[build-url]: https://github.com/psrenergy/LoggingPolyglot.jl/actions?query=workflow%3ACI

[codecov-img]: https://codecov.io/gh/psrenergy/Polyglot.jl/coverage.svg?branch=master
[codecov-url]: https://codecov.io/gh/psrenergy/Polyglot.jl?branch=master
[codecov-img]: https://codecov.io/gh/psrenergy/LoggingPolyglot.jl/coverage.svg?branch=master
[codecov-url]: https://codecov.io/gh/psrenergy/LoggingPolyglot.jl?branch=master

# StateSpaceModels.jl

Expand All @@ -18,29 +18,29 @@ The package also helps users to deal with logs in multiple languages.

## Basic usage

Polyglot will log messages to different locations.
LoggingPolyglot will log messages to different locations.

```julia
import Polyglot
import LoggingPolyglot

log_file = "my_application.log"
polyglot_logger = Polyglot.create_polyglot_logger(log_file)
polyglot_logger = LoggingPolyglot.create_polyglot_logger(log_file)

# Only goes to file
Polyglot.debug("debug message")
LoggingPolyglot.debug("debug message")

# Goes to file and console
Polyglot.info("info message")
Polyglot.warn("warn message")
Polyglot.non_fatal_error("error message")
LoggingPolyglot.info("info message")
LoggingPolyglot.warn("warn message")
LoggingPolyglot.non_fatal_error("error message")

# Goes to console and file and then runs exit(1) if the session is not iterative.
Polyglot.fatal_error("Application cannot continue")
LoggingPolyglot.fatal_error("Application cannot continue")
```

## Log in differnt languages

Polyglot stores some constants that help users deal with logs in different languages.
LoggingPolyglot stores some constants that help users deal with logs in different languages.

```julia
log_path = "langs.log"
Expand All @@ -54,16 +54,16 @@ langs_dict = Dict(
"pt" => "O arquivo @@@ não existe.",
)
)
Polyglot.set_dict(langs_dict)
Polyglot.set_language("pt")
polyglot_logger = Polyglot.create_polyglot_logger(log_path)
LoggingPolyglot.set_dict(langs_dict)
LoggingPolyglot.set_language("pt")
polyglot_logger = LoggingPolyglot.create_polyglot_logger(log_path)
# It will log the portuguese version "Olá!"
Polyglot.info(1)
LoggingPolyglot.info(1)
# It will display the message "O arquivo file.txt não existe"
Polyglot.info(2, "file.txt")
LoggingPolyglot.info(2, "file.txt")
```

One suggestion to store the codes ans messages for multiple languages is to store it on a TOML file. The function `Polyglot.set_dict` accepts the TOML path as input.
One suggestion to store the codes ans messages for multiple languages is to store it on a TOML file. The function `LoggingPolyglot.set_dict` accepts the TOML path as input.

Dictionary TOML:
```toml
Expand All @@ -80,18 +80,18 @@ Set dictionary from TOML:
```julia
log_path = "langs.log"
toml_dict_path = "example.toml"
Polyglot.set_dict(toml_dict_path)
Polyglot.set_language("pt")
polyglot_logger = Polyglot.create_polyglot_logger(log_path)
LoggingPolyglot.set_dict(toml_dict_path)
LoggingPolyglot.set_language("pt")
polyglot_logger = LoggingPolyglot.create_polyglot_logger(log_path)
# It will log the portuguese version "Olá!"
Polyglot.info(1)
LoggingPolyglot.info(1)
# It will display the message "O arquivo file.txt não existe"
Polyglot.info(2, "file.txt")
LoggingPolyglot.info(2, "file.txt")
```

## Create logger

The arguments that can be passed using `Polyglot.create_polyglot_logger`:
The arguments that can be passed using `LoggingPolyglot.create_polyglot_logger`:
* `log_file_path`: Log file path. This input must be passed
* `min_level_console`: Minimum level shown in console. Default: Logging.Info
* `min_level_file`: Minimum level shown in file. Default: Logging.Debug
Expand Down Expand Up @@ -169,7 +169,7 @@ background_reverse_dict = Dict(
)

log_file = "my_application.log"
Polyglot.create_polyglot_logger(log_file; level_dict, color_dict, background_reverse_dict)
LoggingPolyglot.create_polyglot_logger(log_file; level_dict, color_dict, background_reverse_dict)
```

The next example shows how to remove the `info` tag
Expand All @@ -191,8 +191,8 @@ level_dict = Dict(
"Error" => "Error",
"Fatal Error" => "Fatal Error",
)
Polyglot.create_polyglot_logger(log_file; brackets_dict, level_dict)
Polyglot.info("info msg")
Polyglot.warn("warn msg")
Polyglot.remove_log_file_path_on_logger_creation(log_file)
LoggingPolyglot.create_polyglot_logger(log_file; brackets_dict, level_dict)
LoggingPolyglot.info("info msg")
LoggingPolyglot.warn("warn msg")
LoggingPolyglot.remove_log_file_path_on_logger_creation(log_file)
```
4 changes: 2 additions & 2 deletions revise/revise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ dir_path = dirname(@__DIR__)
Pkg.activate(dir_path)
Pkg.instantiate()

using Polyglot
using LoggingPolyglot

@info("""
Polyglot
LoggingPolyglot
""")
2 changes: 1 addition & 1 deletion src/Polyglot.jl → src/LoggingPolyglot.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Polyglot
module LoggingPolyglot

using LoggingExtras
using Dates
Expand Down
2 changes: 1 addition & 1 deletion src/logger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function create_polyglot_logger(
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)
io = choose_terminal_io(args.level) # needed to use Polyglot in MD Studio. It takes stderr as error and stdout as non error log
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)
Expand Down
60 changes: 30 additions & 30 deletions test/test_log_levels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,92 @@ module TestLogLevels

using Test
using Logging
import Polyglot
import LoggingPolyglot

function test_debug_levels_debug_with_level_999()
log_path = "log_debug_levels_test.log"
level = -1000
polyglot_logger = Polyglot.create_polyglot_logger(
polyglot_logger = LoggingPolyglot.create_polyglot_logger(
log_path;
min_level_console = Logging.LogLevel(level),
min_level_file = Logging.LogLevel(level),
)
Polyglot.debug("debug")
Polyglot.debug("debug -999"; level = -999)
Polyglot.debug("debug -1"; level = -1)
LoggingPolyglot.debug("debug")
LoggingPolyglot.debug("debug -999"; level = -999)
LoggingPolyglot.debug("debug -1"; level = -1)
logs_on_file = readlines(log_path)
@test length(logs_on_file) == 3
@test occursin("debug", logs_on_file[1])
@test occursin("debug -999", logs_on_file[2])
@test occursin("debug -1", logs_on_file[3])
Polyglot.close_polyglot_logger(polyglot_logger)
LoggingPolyglot.close_polyglot_logger(polyglot_logger)
rm(log_path)
return nothing
end

function test_debug_levels_2()
log_path = "log_debug_levels_test.log"
level = -100
polyglot_logger = Polyglot.create_polyglot_logger(
polyglot_logger = LoggingPolyglot.create_polyglot_logger(
log_path;
min_level_console = Logging.LogLevel(level),
min_level_file = Logging.LogLevel(level),
)
Polyglot.debug("debug")
Polyglot.debug("debug -999"; level = -999)
Polyglot.debug("debug -1"; level = -1)
LoggingPolyglot.debug("debug")
LoggingPolyglot.debug("debug -999"; level = -999)
LoggingPolyglot.debug("debug -1"; level = -1)
logs_on_file = readlines(log_path)
@test length(logs_on_file) == 1
@test occursin("debug -1", logs_on_file[1])
Polyglot.close_polyglot_logger(polyglot_logger)
LoggingPolyglot.close_polyglot_logger(polyglot_logger)
rm(log_path)
return nothing
end

function test_debug_levels_assertion_error_1()
log_path = "test_debug_levels_assertion_error.log"
level = -100
polyglot_logger = Polyglot.create_polyglot_logger(
polyglot_logger = LoggingPolyglot.create_polyglot_logger(
log_path;
min_level_console = Logging.LogLevel(level),
min_level_file = Logging.LogLevel(level),
)
@test_throws AssertionError Polyglot.debug("debug"; level = 1)
Polyglot.close_polyglot_logger(polyglot_logger)
@test_throws AssertionError LoggingPolyglot.debug("debug"; level = 1)
LoggingPolyglot.close_polyglot_logger(polyglot_logger)
rm(log_path)
return nothing
end

function test_debug_levels_assertion_error_2()
log_path = "test_debug_levels_assertion_error.log"
level = -10000
polyglot_logger = Polyglot.create_polyglot_logger(
polyglot_logger = LoggingPolyglot.create_polyglot_logger(
log_path;
min_level_console = Logging.LogLevel(level),
min_level_file = Logging.LogLevel(level),
)
@test_throws AssertionError Polyglot.debug("debug"; level = -1001)
Polyglot.close_polyglot_logger(polyglot_logger)
@test_throws AssertionError LoggingPolyglot.debug("debug"; level = -1001)
LoggingPolyglot.close_polyglot_logger(polyglot_logger)
rm(log_path)
return nothing
end

function test_log_levels_on_file()
log_path = "log_file_levels_test.log"
polyglot_logger = Polyglot.create_polyglot_logger(
polyglot_logger = LoggingPolyglot.create_polyglot_logger(
log_path;
min_level_console = Logging.Info,
min_level_file = Logging.Warn,
)
Polyglot.debug("debug")
Polyglot.info("info")
Polyglot.warn("warn")
Polyglot.non_fatal_error("non_fatal_error")
LoggingPolyglot.debug("debug")
LoggingPolyglot.info("info")
LoggingPolyglot.warn("warn")
LoggingPolyglot.non_fatal_error("non_fatal_error")
logs_on_file = readlines(log_path)
@test length(logs_on_file) == 2
@test occursin("warn", logs_on_file[1])
@test occursin("non_fatal_error", logs_on_file[2])
Polyglot.close_polyglot_logger(polyglot_logger)
LoggingPolyglot.close_polyglot_logger(polyglot_logger)
rm(log_path)
return nothing
end
Expand All @@ -101,19 +101,19 @@ function test_log_names_with_dict()
"Error" => "error",
)
log_path = "log_names_test.log"
polyglot_logger = Polyglot.create_polyglot_logger(log_path; level_dict = level_dict)
Polyglot.debug("test")
Polyglot.debug("test"; level = -100)
Polyglot.info("test")
Polyglot.warn("test")
Polyglot.non_fatal_error("test")
polyglot_logger = LoggingPolyglot.create_polyglot_logger(log_path; level_dict = level_dict)
LoggingPolyglot.debug("test")
LoggingPolyglot.debug("test"; level = -100)
LoggingPolyglot.info("test")
LoggingPolyglot.warn("test")
LoggingPolyglot.non_fatal_error("test")
logs_on_file = readlines(log_path)
@test occursin("[debug]", logs_on_file[1])
@test occursin("[debug level -100]", logs_on_file[2])
@test occursin("[info]", logs_on_file[3])
@test occursin("[warn]", logs_on_file[4])
@test occursin("[error]", logs_on_file[5])
Polyglot.close_polyglot_logger(polyglot_logger)
LoggingPolyglot.close_polyglot_logger(polyglot_logger)
rm(log_path)
return nothing
end
Expand Down
Loading

0 comments on commit 06540df

Please sign in to comment.