@@ -4,7 +4,20 @@ using Logging: Logging, AbstractLogger, LogLevel, Info, with_logger
44import 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+ """
821struct LogRecord
922 level
1023 message
@@ -30,6 +43,42 @@ mutable struct TestLogger <: AbstractLogger
3043 respect_maxlog:: Bool
3144end
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+ """
3382TestLogger (; min_level= Info, catch_exceptions= false , respect_maxlog= true ) =
3483 TestLogger (LogRecord[], min_level, catch_exceptions, nothing , Dict {Any, Int} (), respect_maxlog)
3584Logging. min_enabled_level (logger:: TestLogger ) = logger. min_level
0 commit comments