How to enable log.debug for tests in Release* mode?

Enable - means I’d like to see log in console output

Keywords here are

  • tests
  • release

I asked this question in different forums, also read similar items in this forum.
It did not help, I don’t see any log.
Looks I don’t understand something :joy:

Link to working code will be good enough.

Lot of thanks in advance

(zig version - 0.14.1)

In your test, do std.testing.log_level = .level.

It gets reset to .warn before each test.

1 Like

In the test .debug level already set:

test "exchanger exchange" {
    std.testing.log_level = .debug;

    std.log.debug("Exchanger log (mode={s})\r\n", .{@tagName(builtin.mode)});

Run:

     $ zig build test -freference-trace --summary all -Doptimize=ReleaseFast
Build Summary: 3/3 steps succeeded; 15/15 tests passed
test success
└─ run test 15 passed 1s MaxRSS:1M
   └─ zig test ReleaseFast native success 15s MaxRSS:288M

Nothing

It was the reason I asked link :joy:

1 Like

Looks like the test runner doesn’t set the std.options.log_level, which only it can do when testing, as the test runner is the root module.

It does its own test filtering based on std.testing.log_level but that happens after the log passed the std.options.log_level.

I would say it’s a bug, make an issue for it, even a pr. It would be an easy fix, just set the std.options.log_level to .debug and rely on its custom log fn to do the filtering, which it already does.

Until it’s fixed, and the next version is release, you can use a custom test runner, just copy the current one and apply the above fix.

1 Like

I copied test_runner.zig (0.14.1) to the root folder of the repo.

Change in build.zig:

...
const exe_unit_tests = b.addTest(.{
        .root_source_file = b.path("src/all_tests.zig"),
        .target = target,
        .optimize = optimize,
        .error_tracing = true,
        .test_runner = .{ .path = b.path("test_runner.zig"), .mode = .simple },
    });
...

And changed test_runner.zig:

...
testing.log_level = .debug; //  was .warn;
...
zig build test -freference-trace --summary all -Doptimize=ReleaseSafe

Nothing :joy:

Only after change of the test code itself:

...
std.log.warn("Exchanger log (mode={s})\r\n", .{@tagName(builtin.mode)});
...

I got:

test
└─ run test stderr
[default] (warn): Exchanger log (mode=ReleaseSafe)

Looks that I did not set custom test runner properly and my next post will be

How to set custom test runner?

No, the std_option.log_level that needs to be set because it takes priority over testing.log_level.

Forgot additional change done in test_runner.zig:

...
pub const std_options: std.Options = .{
    .logFn = log,
    .log_level = .debug,
};
...

Are you talking about this one?

Opened #25126