Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make custom log macros work #52359

Merged
merged 3 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions base/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -370,23 +370,23 @@ function logmsg_code(_module, file, line, level, message, exs...)
let
level = $level
# simplify std_level code emitted, if we know it is one of our global constants
std_level = $(level isa Symbol ? :level : :(level isa LogLevel ? level : convert(LogLevel, level)::LogLevel))
if std_level >= _min_enabled_level[]
std_level = $(level isa Symbol ? :level : :(level isa Base.CoreLogging.LogLevel ? level : convert(Base.CoreLogging.LogLevel, level)::Base.CoreLogging.LogLevel))
IanButterworth marked this conversation as resolved.
Show resolved Hide resolved
if std_level >= Base.CoreLogging._min_enabled_level[]
group = $(log_data._group)
_module = $(log_data._module)
logger = current_logger_for_env(std_level, group, _module)
logger = Base.CoreLogging.current_logger_for_env(std_level, group, _module)
if !(logger === nothing)
id = $(log_data._id)
# Second chance at an early bail-out (before computing the message),
# based on arbitrary logger-specific logic.
if invokelatest(shouldlog, logger, level, _module, group, id)
if invokelatest(Base.CoreLogging.shouldlog, logger, level, _module, group, id)
file = $(log_data._file)
if file isa String
file = Base.fixup_stdlib_path(file)
end
line = $(log_data._line)
local msg, kwargs
$(logrecord) && invokelatest(handle_message,
$(logrecord) && invokelatest(Base.CoreLogging.handle_message,
logger, level, msg, _module, group, id, file, line;
kwargs...)
end
Expand Down
17 changes: 17 additions & 0 deletions stdlib/Logging/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import Logging: min_enabled_level, shouldlog, handle_message

@noinline func1() = backtrace()

# see "custom log macro" testset
CustomLog = LogLevel(-500)
macro customlog(exs...) Base.CoreLogging.logmsg_code((Base.CoreLogging.@_sourceinfo)..., esc(CustomLog), exs...) end

@testset "Logging" begin

@testset "Core" begin
Expand Down Expand Up @@ -275,4 +279,17 @@ end
@test m.run()
end

@testset "custom log macro" begin
@test_logs (CustomLog, "a") min_level=CustomLog @customlog "a"

buf = IOBuffer()
io = IOContext(buf, :displaysize=>(30,80), :color=>false)
logger = ConsoleLogger(io, CustomLog)

with_logger(logger) do
@customlog "a"
end
@test occursin("LogLevel(-500): a", String(take!(buf)))
end

end