Skip to content

Commit 32a026a

Browse files
authored
Test: document and export TestLogger and LogRecord. (#44080)
1 parent 85c83a7 commit 32a026a

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ Standard library changes
206206
function is provided to reproduce the mapping used in identifier normalization
207207
by the Julia parser ([#42561]).
208208

209+
#### Test
210+
* `TestLogger` and `LogRecord` are now exported from the Test stdlib. ([#44080])
211+
209212

210213
Deprecated or removed
211214
---------------------

stdlib/Test/docs/src/index.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,16 @@ Foo Tests | 3 1 4 0.0s
200200
ERROR: Some tests did not pass: 3 passed, 1 failed, 0 errored, 0 broken.
201201
```
202202

203+
## Testing Log Statements
204+
205+
One can use the [`@test_logs`](@ref) macro to test log statements, or use a [`TestLogger`](@ref).
206+
207+
```@docs
208+
Test.@test_logs
209+
Test.TestLogger
210+
Test.LogRecord
211+
```
212+
203213
## Other Test Macros
204214

205215
As calculations on floating-point values can be imprecise, you can perform approximate equality
@@ -226,7 +236,6 @@ Note that this is not a specific feature of the `≈` but rather a general featu
226236

227237
```@docs
228238
Test.@inferred
229-
Test.@test_logs
230239
Test.@test_deprecated
231240
Test.@test_warn
232241
Test.@test_nowarn

stdlib/Test/src/Test.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export @inferred
2222
export detect_ambiguities, detect_unbound_args
2323
export GenericString, GenericSet, GenericDict, GenericArray, GenericOrder
2424
export TestSetException
25+
export TestLogger, LogRecord
2526

2627
using Random
2728
using Random: AbstractRNG, default_rng

stdlib/Test/src/logging.jl

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@ using Logging: Logging, AbstractLogger, LogLevel, Info, with_logger
44
import Base: occursin
55

66
#-------------------------------------------------------------------------------
7-
# Log records
7+
"""
8+
LogRecord
9+
10+
Stores the results of a single log event. Fields:
11+
12+
* `level`: the [`LogLevel`](@ref) of the log message
13+
* `message`: the textual content of the log message
14+
* `_module`: the module of the log event
15+
* `group`: the logging group (by default, the name of the file containing the log event)
16+
* `id`: the ID of the log event
17+
* `file`: the file containing the log event
18+
* `line`: the line within the file of the log event
19+
* `kwargs`: any keyword arguments passed to the log event
20+
"""
821
struct LogRecord
922
level
1023
message
@@ -30,6 +43,42 @@ mutable struct TestLogger <: AbstractLogger
3043
respect_maxlog::Bool
3144
end
3245

46+
"""
47+
TestLogger(; min_level=Info, catch_exceptions=false)
48+
49+
Create a `TestLogger` which captures logged messages in its `logs::Vector{LogRecord}` field.
50+
51+
Set `min_level` to control the `LogLevel`, `catch_exceptions` for whether or not exceptions
52+
thrown as part of log event generation should be caught, and `respect_maxlog` for whether
53+
or not to follow the convention of logging messages with `maxlog=n` for some integer `n` at
54+
most `n` times.
55+
56+
See also: [`LogRecord`](@ref).
57+
58+
## Example
59+
60+
```jldoctest
61+
julia> using Test, Logging
62+
63+
julia> f() = @info "Hi" number=5;
64+
65+
julia> test_logger = TestLogger();
66+
67+
julia> with_logger(test_logger) do
68+
f()
69+
@info "Bye!"
70+
end
71+
72+
julia> @test test_logger.logs[1].message == "Hi"
73+
Test Passed
74+
75+
julia> @test test_logger.logs[1].kwargs[:number] == 5
76+
Test Passed
77+
78+
julia> @test test_logger.logs[2].message == "Bye!"
79+
Test Passed
80+
```
81+
"""
3382
TestLogger(; min_level=Info, catch_exceptions=false, respect_maxlog=true) =
3483
TestLogger(LogRecord[], min_level, catch_exceptions, nothing, Dict{Any, Int}(), respect_maxlog)
3584
Logging.min_enabled_level(logger::TestLogger) = logger.min_level

0 commit comments

Comments
 (0)