From 8afefa099658f1d2ca409779da7bab50995247df Mon Sep 17 00:00:00 2001 From: Elliot Saba Date: Thu, 12 Oct 2023 10:23:57 -0700 Subject: [PATCH] Merge `LogComposeExtras` in as a package extension This integrates the `LoggingExtrasCompose` package [0] as a package extension to `LogCompose`, to be loaded on-demand when `LoggingExtras` is also loaded. This requires the Julia compat bounds to be bumped up to v1.9. [0] https://github.com/tanmaykm/LoggingExtrasCompose.jl --- .github/workflows/ci.yml | 2 +- Project.toml | 13 ++++++++++--- ext/LoggingExtrasExt.jl | 19 +++++++++++++++++++ test/runtests.jl | 38 +++++++++++++++++++++++++++++++++++++- test/testapp.toml | 16 ++++++++++++++++ 5 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 ext/LoggingExtrasExt.jl diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7b44c54..7629aa2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ jobs: fail-fast: false matrix: version: - - '1.0' + - '1.9' - '1' # automatically expands to the latest stable 1.x release of Julia - nightly os: diff --git a/Project.toml b/Project.toml index bdf5ca6..466a63d 100644 --- a/Project.toml +++ b/Project.toml @@ -4,17 +4,24 @@ keywords = ["configure", "compose", "logging", "logger"] authors = ["Tanmay Mohapatra "] license = "MIT" desc = "Compose loggers and logger ensembles declaratively using configuration files" -version = "0.2.1" +version = "0.3" + +[weakdeps] +LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36" + +[extensions] +LoggingExtrasExt = "LoggingExtras" [deps] Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" [compat] -julia = "1" +julia = "1.9" [extras] +LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Test"] +test = ["Test", "LoggingExtras"] diff --git a/ext/LoggingExtrasExt.jl b/ext/LoggingExtrasExt.jl new file mode 100644 index 0000000..86d9c55 --- /dev/null +++ b/ext/LoggingExtrasExt.jl @@ -0,0 +1,19 @@ +module LoggingExtrasExt + +using LoggingExtras, LogCompose +import LogCompose: logcompose, log_min_level + +function logcompose(::Type{LoggingExtras.TeeLogger}, config::Dict{String,Any}, logger_config::Dict{String,Any}) + destinations = [LogCompose.logger(config, dest) for dest in logger_config["destinations"]] + return LoggingExtras.TeeLogger(destinations...) +end + +function logcompose(::Type{LoggingExtras.FileLogger}, config::Dict{String,Any}, logger_config::Dict{String,Any}) + filename = logger_config["filename"] + append = get(logger_config, "append", false) + flush = get(logger_config, "flush", true) + + return LoggingExtras.FileLogger(filename; append=append, always_flush=flush) +end + +end # module diff --git a/test/runtests.jl b/test/runtests.jl index 10532ef..928ec94 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,4 @@ -using LogCompose, Test, Logging +using LogCompose, Test, Logging, LoggingExtras function test() config = joinpath(@__DIR__, "testapp.toml") @@ -52,8 +52,44 @@ function test() @test_throws ErrorException LogCompose.logger(invalid_config, "invalid") @test_throws ErrorException LogCompose.logcompose(String, invalid_config, invalid_config) + file1 = "testapp1.log" + file2 = "testapp2.log" + rm(file1; force=true) + rm(file2; force=true) + + let logger = LogCompose.logger(config, "file1"; section="loggers") + with_logger(logger) do + @info("testfile1") + end + end + + let logger = LogCompose.logger(config, "file2"; section="loggers") + with_logger(logger) do + @info("testfile2") + end + end + + let logger = LogCompose.logger(config, "tee"; section="loggers") + with_logger(logger) do + @info("testtee") + end + end + + @test isfile(file1) + @test isfile(file2) + + log_file_contents = readlines(file1) + @test findfirst("testfile1", log_file_contents[1]) !== nothing + @test findfirst("testtee", log_file_contents[3]) !== nothing + + log_file_contents = readlines(file2) + @test findfirst("testfile2", log_file_contents[1]) !== nothing + @test findfirst("testtee", log_file_contents[3]) !== nothing + try rm(simple_logfile; force=true) + rm(file1; force=true) + rm(file2; force=true) catch ex # ignore (occasionally fails with resource busy exception on Windows, because logger has not been gc'd yet) end diff --git a/test/testapp.toml b/test/testapp.toml index 6d01936..ce32f5c 100644 --- a/test/testapp.toml +++ b/test/testapp.toml @@ -13,3 +13,19 @@ displaysize = [3, 50] # Set display size for formatting [loggers.null] type = "Logging.NullLogger" + +[loggers.file1] +type = "LoggingExtras.FileLogger" +filename = "testapp1.log" +append = true # file open mode (default: false) +flush = true # flush after logging (default: true) + +[loggers.file2] +type = "LoggingExtras.FileLogger" +filename = "testapp2.log" +append = true +flush = true + +[loggers.tee] +type = "LoggingExtras.TeeLogger" +destinations = ["file1", "file2"]