Skip to content

Commit 6f7c328

Browse files
committed
Deprecate DemuxLogger to TeeLogger
1 parent cb19b17 commit 6f7c328

File tree

7 files changed

+78
-59
lines changed

7 files changed

+78
-59
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
```
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Base.CoreLogging:
99
handle_message, shouldlog, min_enabled_level, catch_exceptions
1010

1111
export demux_global_logger,
12-
DemuxLogger, TransformerLogger, FileLogger,
12+
TeeLogger, TransformerLogger, FileLogger,
1313
ActiveFilteredLogger, EarlyFilteredLogger, MinLevelLogger
1414

1515

@@ -32,11 +32,12 @@ function comp_handle_message_check(logger, args...; kwargs...)
3232
end
3333
###############################
3434

35-
include("demux.jl")
35+
include("tee.jl")
3636
include("transformer.jl")
3737
include("activefiltered.jl")
3838
include("earlyfiltered.jl")
3939
include("minlevelfiltered.jl")
4040
include("filelogger.jl")
41+
include("deprecated.jl")
4142

4243
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
struct TeeLogger <: AbstractLogger
2+
loggers::Vector{AbstractLogger}
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+
loggers = Vector{AbstractLogger}(collect(loggers))
19+
return TeeLogger(loggers)
20+
end
21+
22+
function handle_message(demux::TeeLogger, args...; kwargs...)
23+
for logger in demux.loggers
24+
if comp_handle_message_check(logger, args...; kwargs...)
25+
handle_message(logger, args...; kwargs...)
26+
end
27+
end
28+
end
29+
30+
function shouldlog(demux::TeeLogger, args...)
31+
any(comp_shouldlog(logger, args...) for logger in demux.loggers)
32+
end
33+
34+
function min_enabled_level(demux::TeeLogger)
35+
minimum(min_enabled_level(logger) for logger in demux.loggers)
36+
end
37+
38+
function catch_exceptions(demux::TeeLogger)
39+
any(catch_exceptions(logger) for logger in demux.loggers)
40+
end

test/runtests.jl

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
using LoggingExtras
22
using Test
33
using Base.CoreLogging
4-
using Base.CoreLogging: Debug, Info, Warn, Error
4+
using Base.CoreLogging: BelowMinLevel, Debug, Info, Warn, Error
55

66
using Test: collect_test_logs, TestLogger
77

8-
9-
@testset "Demux" begin
8+
@testset "Tee" begin
109
testlogger_info = TestLogger(min_level=Info)
1110
testlogger_warn = TestLogger(min_level=Warn)
1211

13-
with_logger(DemuxLogger(testlogger_warn, testlogger_info)) do
12+
with_logger(TeeLogger(testlogger_warn, testlogger_info)) do
1413
@info "info1"
1514
@warn "warn1"
1615
@info "info2"
@@ -104,3 +103,20 @@ end
104103
end
105104
@test length(testlogger.logs) == 2
106105
end
106+
107+
108+
@testset "Deprecations" begin
109+
testlogger = TestLogger(min_level=BelowMinLevel)
110+
111+
demux_logger = DemuxLogger(testlogger)
112+
@test demux_logger isa TeeLogger
113+
@test Set(demux_logger.loggers) == Set([testlogger, global_logger()])
114+
115+
demux_logger = DemuxLogger(testlogger; include_current_global=true)
116+
@test demux_logger isa TeeLogger
117+
@test Set(demux_logger.loggers) == Set([testlogger, global_logger()])
118+
119+
demux_logger = DemuxLogger(testlogger; include_current_global=false)
120+
@test demux_logger isa TeeLogger
121+
@test Set(demux_logger.loggers) == Set([testlogger])
122+
end

0 commit comments

Comments
 (0)