BazerUtils.jl

Assorted Julia utilities including custom logging
Log | Files | Refs | README | LICENSE

commit a8d5028269689d58ff551c20332f06d14e38b2cc
parent 168092ed6a4ce3b95dcf3b7abba942fa8ca91b3b
Author: Erik Loualiche <[email protected]>
Date:   Sat, 14 Mar 2026 02:40:37 -0400

Merge pull request #11 from LouLouLibs/fix/issue-10-nothing-module-crash

Fix format_log4j crash when _module=nothing
Diffstat:
Msrc/CustomLogger.jl | 2+-
Mtest/UnitTests/customlogger.jl | 17++++++++++++++++-
2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/src/CustomLogger.jl b/src/CustomLogger.jl @@ -396,7 +396,7 @@ function format_log4j(log_record::NamedTuple; timestamp = format(now(), "yyyy-mm-dd HH:MM:SS") log_level = rpad(uppercase(string(log_record.level)), 5) - module_name = nameof(log_record._module) + module_name = isnothing(log_record._module) ? :unknown : nameof(log_record._module) file = shorten_path_str(log_record.file; strategy=shorten_path) prefix = shorten_path == :relative_path ? "[$(pwd())] " : "" line = log_record.line diff --git a/test/UnitTests/customlogger.jl b/test/UnitTests/customlogger.jl @@ -4,6 +4,7 @@ log_paths = map(l -> l.logger.logger.stream, logger_in.loggers) |> (s -> filter(x -> x isa IOStream, s)) |> (s -> map(x -> x.name, s)) |> + (s -> filter(x -> contains(x, "<file "), s)) |> (s -> map(x -> match(r"<file (.+)>", x)[1], s)) return unique(string.(log_paths)) end @@ -152,7 +153,21 @@ @test any(map(contains("DEBUG"), filter(contains("<15>"), log_lines))) close_logger(logger_single, remove_files=true) - # -- logger to only one file sink + # -- logger with _module=nothing (issue #10) + logger_single = custom_logger( + log_path; + log_format=:log4j, + overwrite=true) + log_record = (level=Base.CoreLogging.Info, message="test nothing module", + _module=nothing, file="test.jl", line=1, group=:test, id=:test) + buf = IOBuffer() + BazerUtils.custom_format(buf, log_record; log_format=:log4j) + output = String(take!(buf)) + @test contains(output, "unknown") + @test contains(output, "test nothing module") + close_logger(logger_single, remove_files=true) + + # -- logger to only one file sink log_path = joinpath.(tempdir(), "log") logger_single = custom_logger( log_path;