[slf4j-dev] [JIRA] (SLF4J-461) Unit testing support

QOS.CH (JIRA) noreply-jira at qos.ch
Mon May 13 13:26:00 CEST 2019


    [ https://jira.qos.ch/browse/SLF4J-461?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=19554#comment-19554 ] 

Aaron Digulla commented on SLF4J-461:
-------------------------------------

I created a fork with a sample implementation: [https://github.com/digulla/slf4j/commit/ecf2fb7943888879ce26b2842268d943ff4d1651]

Things that I don't like about it:
 * You have to create one helper per supported log implementation (logback, log4j, ...).
 * I have to use reflection to get "aai" from logback Logger (in `RedirectLogbackLogger`).

A much better API would be an "override logger" API in LoggerFactory but I didn't want to change such a central place for now. Also, the LoggerFactory would have to return wrappers in this case. Security could be an issue, too, since that would allow malicious code to "steal" loggers at runtime to prevent logging of vital information or to fish for sensitive data.

> Unit testing support
> --------------------
>
>                 Key: SLF4J-461
>                 URL: https://jira.qos.ch/browse/SLF4J-461
>             Project: SLF4J
>          Issue Type: New Feature
>          Components: Unspecified
>    Affects Versions: 1.7.25, 1.8.0-beta4
>            Reporter: Aaron Digulla
>            Assignee: SLF4J developers list
>              Labels: test
>
> I would like to submit some code to help write unit tests that verify log behavior. Examples:
>  * Make sure that some code correctly logs errors and warnings
>  * The log messages contain the correct information (instead of, say, "[byte").
>  * Files are logged with absolute path
>  * Remove (expected) stack traces from test output
> My code contains a class RedirectLogger which takes lists of classes or logger names and adds appenders to those which collect log events in a list.
> At the end of the test, you can call a dump() method (optionally with a filter) to get everything that was logged.
> Example code before Java 8:
> {code:java}
> RedirectLogger rl = new RedirectLogger(Tool.class, Util.class);
> try {
>     rl.install();
>     tool.foo();
> } finally  {
>     rl.deinstall();
> }
> assertEquals("Expected log lines", rl.dump(Level.WARN));
> {code}
> Example > Java 8 with method that returns value:
> {code:java}
> RedirectLogger rl = new RedirectLogger(Tool.class, Util.class);
> Type result = rl.with(() -> {
>     return tool.bar();
> });
> assertEquals(..., result);
> assertEquals("Expected log lines", rl.dump(Level.WARN));{code}
>  



--
This message was sent by Atlassian JIRA
(v7.3.1#73012)


More information about the slf4j-dev mailing list