apache / logging-log4j2

Apache Log4j 2 is a versatile, feature-rich, efficient logging API and backend for Java.
https://logging.apache.org/log4j/2.x/
Apache License 2.0
3.4k stars 1.62k forks source link

SimpleLayout in log4j-1.2-api does not log anything #2722

Closed kojinoguchi closed 4 months ago

kojinoguchi commented 4 months ago

Description

When using SimpleLayout provided in log4j-1.2-api, it doesn't log anything. Is this by design? In log4j1, it used to log with “%level - %m%n” pattern ?

Reproduction

        Logger logger = Logger.getLogger(clazz);
        logger.removeAllAppenders();
        logger.setLevel(Level.DEBUG);
        SimpleLayout layout = new SimpleLayout();
        logFile = File.createTempFile("log", "");
        FileAppender appender = new FileAppender(layout, logFile.toString(),
                        false, false, 0);
        logger.addAppender(appender);

From one of the unit-test we have. This produces empty logs. Works when replacing SimpleLayout with PatternLayout.

vy commented 4 months ago

@kojinoguchi, SimpleLayout is by design a no-op. When a Log4j 1 configuration is read, SimpleLayout component will be replaced with a PatternLayout – see SimpleLayoutBuilder responsible for this transformation. But since you access the element programmatically, which is discouraged!1, you get the no-op component. In short, your Log4j 1 configurations using SimpleLayout will work.

1 Log4j Core treats the configuration file as the public API. Even though we do our best to keep the binary backward compatibility for publicly visible classes, fields, etc. in the code, we don't give the same guarantee that we give for the configuration file syntax.

kojinoguchi commented 4 months ago

When a Log4j 1 configuration is read, SimpleLayout component will be replaced with a PatternLayout

Aaah, I see. In that case, I would have preferred if this empty SimpleLayout didn't exist or failed out with exception instead of silently swallowing the logs (no-op). Thanks anyways for the infoi!