Skip to content

@test_logs doesn't work when using local loggers #48456

@veddox

Description

@veddox

(See the Discourse discussion here: https://discourse.julialang.org/t/test-logs-behavior-when-using-with-logger/80239)

@test_logs works by creating a TestLogger and wrapping its argument in a with_logger block. The problem with this is that if the argument itself already uses a with_logger block, this inner block receives all log statements without passing them on to the TestLogger. Therefore, in such situations @test_logs will fail, even if the underlying code works.

At minimum this should be mentioned in the documentation. However, it would be good to provide a work-around for this. Two suggestions that came to my mind are:

  1. Create a @test_output macro to intercept STDOUT. This would be directly equivalent to @test_warn. The advantage would be that this could also be used to test screen output other than log calls. The disadvantage is that having to revert to intercepting STDOUT hides some of the details of log calls.
  2. Alternately, one might be able to dig into Base.CoreLogging to provide a way to override local loggers when using @test_logs. This would be the cleanest solution from a user's perspective (@test_logs does what you expect even when using local loggers), but may be tricky/messy to implement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    loggingThe logging frameworktestThis change adds or pertains to unit tests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions