Skip to content

Commit 19d4071

Browse files
authored
Merge pull request #22 from oxinabox/ox/noglobal
Deprecate DemuxLogger for TeeLogger, that does not support `include_current_global` kwarg
2 parents 91dc007 + fd2328c commit 19d4071

File tree

7 files changed

+97
-68
lines changed

7 files changed

+97
-68
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "LoggingExtras"
22
uuid = "e6f89c97-d47a-5376-807f-9c37f3926c36"
33
authors = ["Lyndon White <[email protected]>"]
4-
version = "0.3.0"
4+
version = "0.4.0"
55

66
[compat]
77
julia = "0.7, 1"

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,25 @@ logger = global_logger()
7676

7777
# Loggers introduced by this package:
7878
This package introduces 6 new loggers.
79-
The `DemuxLogger`, the `TransformerLogger`, 3 types of filtered logger, and the `FileLogger`.
79+
The `TeeLogger`, the `TransformerLogger`, 3 types of filtered logger, and the `FileLogger`.
8080
All of them just wrap existing loggers.
81-
- The `DemuxLogger` sends the logs to multiple different loggers.
81+
- The `TeeLogger` sends the logs to multiple different loggers.
8282
- The `TransformerLogger` applies a function to modify log messages before passing them on.
8383
- The 3 filter loggers are used to control if a message is written or not
8484
- The `MinLevelLogger` only allowes messages to pass that are above a given level of severity
8585
- The `EarlyFilteredLogger` lets you write filter rules based on the `level`, `module`, `group` and `id` of the log message
8686
- The `ActiveFilteredLogger` lets you filter based on the full content
8787
- The `FileLogger` is a simple logger sink that writes to file.
8888

89-
By combining `DemuxLogger` with filter loggers you can arbitrarily route log messages, wherever you want.
89+
By combining `TeeLogger` with filter loggers you can arbitrarily route log messages, wherever you want.
9090

9191

92-
## `DemuxLogger`
92+
## `TeeLogger`
9393

94-
The `DemuxLogger` sends the log messages to multiple places.
94+
The `TeeLogger` sends the log messages to multiple places.
9595
It takes a list of loggers.
96-
It also has the keyword argument `include_current_global`,
97-
to determine if you also want to log to the global logger.
96+
You often want to pass the `current_logger()` or `global_logger()`
97+
as one of those inputs so it keeps going to that one as well.
9898

9999
It is up to those loggers to determine if they will accept it.
100100
Which they do using their methods for `shouldlog` and `min_enabled_level`.
@@ -111,17 +111,16 @@ It is really simple.
111111
The resulting file format is similar to that which is shown in the REPL.
112112
(Not identical, but similar)
113113

114-
### Demo: `DemuxLogger` and `FileLogger`
114+
### Demo: `TeeLogger` and `FileLogger`
115115
We are going to log info and above to one file,
116116
and warnings and above to another.
117117

118118
```julia
119119
julia> using Logging; using LoggingExtras;
120120

121-
julia> demux_logger = DemuxLogger(
121+
julia> demux_logger = TeeLogger(
122122
MinLevelLogger(FileLogger("info.log"), Logging.Info),
123123
MinLevelLogger(FileLogger("warn.log"), Logging.Warn),
124-
include_current_global=false
125124
);
126125

127126

src/LoggingExtras.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import Base.CoreLogging:
88
AbstractLogger, SimpleLogger,
99
handle_message, shouldlog, min_enabled_level, catch_exceptions
1010

11-
export demux_global_logger,
12-
DemuxLogger, TransformerLogger, FileLogger,
11+
export TeeLogger, TransformerLogger, FileLogger,
1312
ActiveFilteredLogger, EarlyFilteredLogger, MinLevelLogger
1413

1514

@@ -32,11 +31,12 @@ function comp_handle_message_check(logger, args...; kwargs...)
3231
end
3332
###############################
3433

35-
include("demux.jl")
34+
include("tee.jl")
3635
include("transformer.jl")
3736
include("activefiltered.jl")
3837
include("earlyfiltered.jl")
3938
include("minlevelfiltered.jl")
4039
include("filelogger.jl")
40+
include("deprecated.jl")
4141

4242
end # module

src/demux.jl

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/deprecated.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
Base.@deprecate(
3+
DemuxLogger(loggers::Vararg{AbstractLogger}; include_current_global=true),
4+
include_current_global ? TeeLogger(global_logger(), loggers...) : TeeLogger(loggers...)
5+
)

src/tee.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
struct TeeLogger{T<:NTuple{<:Any, AbstractLogger}} <: AbstractLogger
2+
loggers::T
3+
end
4+
5+
6+
7+
"""
8+
TeeLogger(loggers...)
9+
10+
Send the same log message to all the loggers.
11+
12+
To include the current logger do:
13+
`TeeLogger(current_logger(), loggers...)`
14+
to include the global logger, do:
15+
`TeeLogger(global_logger(), loggers...)`
16+
"""
17+
function TeeLogger(loggers::Vararg{AbstractLogger})
18+
return TeeLogger(loggers)
19+
end
20+
21+
function handle_message(demux::TeeLogger, args...; kwargs...)
22+
for logger in demux.loggers
23+
if comp_handle_message_check(logger, args...; kwargs...)
24+
handle_message(logger, args...; kwargs...)
25+
end
26+
end
27+
end
28+
29+
function shouldlog(demux::TeeLogger, args...)
30+
any(comp_shouldlog(logger, args...) for logger in demux.loggers)
31+
end
32+
33+
function min_enabled_level(demux::TeeLogger)
34+
minimum(min_enabled_level(logger) for logger in demux.loggers)
35+
end
36+
37+
function catch_exceptions(demux::TeeLogger)
38+
any(catch_exceptions(logger) for logger in demux.loggers)
39+
end

test/runtests.jl

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
using LoggingExtras
22
using Test
3-
using Base.CoreLogging
4-
using Base.CoreLogging: Debug, Info, Warn, Error
5-
63
using Test: collect_test_logs, TestLogger
74

5+
using Base.CoreLogging
6+
using Base.CoreLogging: BelowMinLevel, Debug, Info, Warn, Error
87

9-
@testset "Demux" begin
10-
testlogger_info = TestLogger(min_level=Info)
11-
testlogger_warn = TestLogger(min_level=Warn)
128

13-
with_logger(DemuxLogger(testlogger_warn, testlogger_info)) do
14-
@info "info1"
15-
@warn "warn1"
16-
@info "info2"
9+
@testset "Tee" begin
10+
@testset "contructor" begin
11+
@testset "mixed types" begin
12+
@test TeeLogger(TestLogger(), NullLogger()) isa TeeLogger
13+
end
14+
15+
@testset "errors if given nonloggers" begin
16+
@test_throws Exception TeeLogger(stdout, stderr)
17+
end
18+
end
19+
@testset "basic use with compositional levels" begin
20+
testlogger_info = TestLogger(min_level=Info)
21+
testlogger_warn = TestLogger(min_level=Warn)
22+
23+
with_logger(TeeLogger(testlogger_warn, testlogger_info)) do
24+
@info "info1"
25+
@warn "warn1"
26+
@info "info2"
27+
end
28+
@test length(testlogger_info.logs) == 3
29+
@test length(testlogger_warn.logs) == 1
1730
end
18-
@test length(testlogger_info.logs) == 3
19-
@test length(testlogger_warn.logs) == 1
2031
end
2132

2233

@@ -104,3 +115,20 @@ end
104115
end
105116
@test length(testlogger.logs) == 2
106117
end
118+
119+
120+
@testset "Deprecations" begin
121+
testlogger = TestLogger(min_level=BelowMinLevel)
122+
123+
demux_logger = DemuxLogger(testlogger)
124+
@test demux_logger isa TeeLogger
125+
@test Set(demux_logger.loggers) == Set([testlogger, global_logger()])
126+
127+
demux_logger = DemuxLogger(testlogger; include_current_global=true)
128+
@test demux_logger isa TeeLogger
129+
@test Set(demux_logger.loggers) == Set([testlogger, global_logger()])
130+
131+
demux_logger = DemuxLogger(testlogger; include_current_global=false)
132+
@test demux_logger isa TeeLogger
133+
@test Set(demux_logger.loggers) == Set([testlogger])
134+
end

0 commit comments

Comments
 (0)