Skip to content

Commit

Permalink
Test that checks if something was written in the file logger
Browse files Browse the repository at this point in the history
Close the logger when done
  • Loading branch information
deltamarnix committed Dec 18, 2023
1 parent 9910205 commit 75f5430
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
24 changes: 20 additions & 4 deletions core/src/logging.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
function is_current_module(log)::Bool
const LogMessageType =
@NamedTuple{level::LogLevel, _module::Module, group::Symbol, id::Symbol}

function is_current_module(log::LogMessageType)::Bool
(log._module == @__MODULE__) ||
(parentmodule(log._module) == @__MODULE__) ||
log._module == OrdinaryDiffEq # for the progress bar
log._module == OrdinaryDiffEq# for the progress bar
end

function setup_logger(config::Config)::AbstractLogger
function setup_logger(
config::Config;
module_filter_function::Function = is_current_module,
)::AbstractLogger
file_logger = LoggingExtras.MinLevelLogger(
LoggingExtras.FileLogger(results_path(config, "ribasim.log")),
config.logging.verbosity,
Expand All @@ -14,7 +20,17 @@ function setup_logger(config::Config)::AbstractLogger
LogLevel(-1), # To include progress bar
)
return LoggingExtras.EarlyFilteredLogger(
is_current_module,
module_filter_function,
LoggingExtras.TeeLogger(file_logger, terminal_logger),
)
end

function close(logger::AbstractLogger)
if hasfield(typeof(logger), :logger)
close(logger.logger)
elseif hasfield(typeof(logger), :loggers)
foreach(close, logger.loggers)
elseif hasfield(typeof(logger), :stream) && !isa(logger, TerminalLogger)
Base.close(logger.stream)
end
end
1 change: 1 addition & 0 deletions core/src/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function main(ARGS)::Cint
model = with_logger(logger) do
Ribasim.run(config)
end
Ribasim.close(logger)
return if successful_retcode(model)
println("The model finished successfully")
0
Expand Down
40 changes: 40 additions & 0 deletions core/test/logging_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@test Logging.shouldlog(logger, Logging.Info, Ribasim, nothing, "message")
@test Logging.shouldlog(logger, Logging.Info - 1, Ribasim, nothing, "message") # progress bar
@test !Logging.shouldlog(logger, Logging.Debug, Ribasim, nothing, "message")

Ribasim.close(logger)
end
end

Expand All @@ -31,5 +33,43 @@ end
@test Logging.shouldlog(logger, Logging.Info, Ribasim, nothing, "message")
@test Logging.shouldlog(logger, Logging.Info - 1, Ribasim, nothing, "message") # progress bar
@test Logging.shouldlog(logger, Logging.Debug, Ribasim, nothing, "message")

Ribasim.close(logger)
end
end

@testitem "setup_logger creates TeeLogger with 2 sinks" begin
using Logging
using LoggingExtras
mktempdir() do dir
cp(
normpath(@__DIR__, "data", "logging_test_loglevel_debug.toml"),
normpath(dir, "ribasim.toml");
force = true,
)
config = Ribasim.Config(normpath(dir, "ribasim.toml"))
mkdir(Ribasim.results_path(config, "."))
logger = Ribasim.setup_logger(
config;
module_filter_function = log::Ribasim.LogMessageType ->
log._module == @__MODULE__,
)

with_logger(logger) do
@info "foo"
@warn "bar"
@debug "baz"
end

Ribasim.close(logger)

println(@__MODULE__)

open(normpath(dir, "results", "ribasim.log"), "r") do io
result = read(io, String)
@test occursin("Info: foo", result)
@test occursin("Warning: bar", result)
@test occursin("Debug: baz", result)
end
end
end

0 comments on commit 75f5430

Please sign in to comment.