Skip to content

Commit 48d8de8

Browse files
committed
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
1 parent eca2c39 commit 48d8de8

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

Project.toml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@ keywords = ["configure", "compose", "logging", "logger"]
44
authors = ["Tanmay Mohapatra <[email protected]>"]
55
license = "MIT"
66
desc = "Compose loggers and logger ensembles declaratively using configuration files"
7-
version = "0.2.1"
7+
version = "0.3"
8+
9+
[weakdeps]
10+
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
11+
12+
[extensions]
13+
LoggingExtrasExt = "LoggingExtras"
814

915
[deps]
1016
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1117
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
1218

1319
[compat]
14-
julia = "1"
20+
julia = "1.9"
1521

1622
[extras]
23+
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
1724
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1825

1926
[targets]
20-
test = ["Test"]
27+
test = ["Test", "LoggingExtras"]

ext/LoggingExtrasExt.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module LoggingExtrasExt
2+
3+
using LoggingExtras, LogCompose
4+
import LogCompose: logcompose, log_min_level
5+
6+
function logcompose(::Type{LoggingExtras.TeeLogger}, config::Dict{String,Any}, logger_config::Dict{String,Any})
7+
destinations = [LogCompose.logger(config, dest) for dest in logger_config["destinations"]]
8+
return LoggingExtras.TeeLogger(destinations...)
9+
end
10+
11+
function logcompose(::Type{LoggingExtras.FileLogger}, config::Dict{String,Any}, logger_config::Dict{String,Any})
12+
filename = logger_config["filename"]
13+
append = get(logger_config, "append", false)
14+
flush = get(logger_config, "flush", true)
15+
16+
return LoggingExtras.FileLogger(filename; append=append, always_flush=flush)
17+
end
18+
19+
end # module

test/runtests.jl

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using LogCompose, Test, Logging
1+
using LogCompose, Test, Logging, LoggingExtras
22

33
function test()
44
config = joinpath(@__DIR__, "testapp.toml")
@@ -52,8 +52,44 @@ function test()
5252
@test_throws ErrorException LogCompose.logger(invalid_config, "invalid")
5353
@test_throws ErrorException LogCompose.logcompose(String, invalid_config, invalid_config)
5454

55+
file1 = "testapp1.log"
56+
file2 = "testapp2.log"
57+
rm(file1; force=true)
58+
rm(file2; force=true)
59+
60+
let logger = LogCompose.logger(config, "file1"; section="loggers")
61+
with_logger(logger) do
62+
@info("testfile1")
63+
end
64+
end
65+
66+
let logger = LogCompose.logger(config, "file2"; section="loggers")
67+
with_logger(logger) do
68+
@info("testfile2")
69+
end
70+
end
71+
72+
let logger = LogCompose.logger(config, "tee"; section="loggers")
73+
with_logger(logger) do
74+
@info("testtee")
75+
end
76+
end
77+
78+
@test isfile(file1)
79+
@test isfile(file2)
80+
81+
log_file_contents = readlines(file1)
82+
@test findfirst("testfile1", log_file_contents[1]) !== nothing
83+
@test findfirst("testtee", log_file_contents[3]) !== nothing
84+
85+
log_file_contents = readlines(file2)
86+
@test findfirst("testfile2", log_file_contents[1]) !== nothing
87+
@test findfirst("testtee", log_file_contents[3]) !== nothing
88+
5589
try
5690
rm(simple_logfile; force=true)
91+
rm(file1; force=true)
92+
rm(file2; force=true)
5793
catch ex
5894
# ignore (occasionally fails with resource busy exception on Windows, because logger has not been gc'd yet)
5995
end

test/testapp.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,19 @@ displaysize = [3, 50] # Set display size for formatting
1313

1414
[loggers.null]
1515
type = "Logging.NullLogger"
16+
17+
[loggers.file1]
18+
type = "LoggingExtras.FileLogger"
19+
filename = "testapp1.log"
20+
append = true # file open mode (default: false)
21+
flush = true # flush after logging (default: true)
22+
23+
[loggers.file2]
24+
type = "LoggingExtras.FileLogger"
25+
filename = "testapp2.log"
26+
append = true
27+
flush = true
28+
29+
[loggers.tee]
30+
type = "LoggingExtras.TeeLogger"
31+
destinations = ["file1", "file2"]

0 commit comments

Comments
 (0)