Skip to content

Merge LogComposeExtras in as a package extension #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 10 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ keywords = ["configure", "compose", "logging", "logger"]
authors = ["Tanmay Mohapatra <[email protected]>"]
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"]
19 changes: 19 additions & 0 deletions ext/LoggingExtrasExt.jl
Original file line number Diff line number Diff line change
@@ -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
38 changes: 37 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using LogCompose, Test, Logging
using LogCompose, Test, Logging, LoggingExtras

function test()
config = joinpath(@__DIR__, "testapp.toml")
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test/testapp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]