You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "taylorThunderbolt (via GitHub)" <gi...@apache.org> on 2024/06/13 14:43:17 UTC
[I] The `append` option in file appender doesn't work properly (logging-log4j2)
taylorThunderbolt opened a new issue, #2661:
URL: https://github.com/apache/logging-log4j2/issues/2661
## Description
Use new FileAppender(the `append` option is set to false, the 'fileName' option is equivalent to previous FileAppender) to log rest messages after logging some messages, the previous messages still exists in the log file.
This fact does not conform to the description in official documents: `When true - the default, records will be appended to the end of the file. When set to false, the file will be cleared before new records are written.`
## Configuration
**Version:** 2.23.1
**Operating system:** all
**JDK:** JDK17
## Logs
```
xxxxxxxx
xxxxxxxx
11111
11111
```
## Reproduction
log4j2.xml
```xml
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="file" fileName="logs/run.log" append="true"/>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="file"/>
</Root>
</Loggers>
</Configuration>
```
a demo to reproduct the issue:
```java
public class Main {
private static final Logger LOG = LogManager.getLogger("logger");
public static void main(String[] args) {
LOG.info("xxxxxxxx");
LOG.info("xxxxxxxx");
LoggerContext context = (LoggerContext) LogManager.getContext(false);
LoggerConfig logger = context.getConfiguration().getLoggerConfig("logger");
logger.removeAppender("file");
FileAppender appender = FileAppender.newBuilder().withAppend(false).setName("file")
.withFileName("logs/run.log").build();
appender.start();
logger.addAppender(appender, Level.INFO, null);
context.updateLoggers();
// after using a new appender, i expect the log file to only contain the following message.
LOG.info("11111");
LOG.info("11111");
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] The `append` option in file appender doesn't work properly (logging-log4j2)
Posted by "rgoers (via GitHub)" <gi...@apache.org>.
rgoers commented on issue #2661:
URL: https://github.com/apache/logging-log4j2/issues/2661#issuecomment-2166934448
Note that using append="false" on a FileAppender where the same file is already open on another FileAppender would likely cause serious problems on Windows if each FileAppender had its own FileManager. Even on Unix I would imagine a user might file a bug if append="false" actually cleared the already open file being written to by another FileAppender as it would result in data loss.
Having multiple FileAppenders point to the same file on the same file system is just a bad, bad idea, even if they are in separate JVMs or are running on separate servers.
Note that in the options Piotr recommends the second option may not be possible if there are multiple LoggerContexts each trying to log with the same configuration or multiple applications running on the same server with each using the same configuration.
I should not that ALL Appender Managers exhibit similar behavior. Every Manager keys on the options that are necessary for it to be unique and will cause all other Appenders using those same values to share the same Manager.
Closing since this is by design and won't be changed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] The `append` option in file appender doesn't work properly (logging-log4j2)
Posted by "ppkarwasz (via GitHub)" <gi...@apache.org>.
ppkarwasz commented on issue #2661:
URL: https://github.com/apache/logging-log4j2/issues/2661#issuecomment-2166641459
@taylorThunderbolt,
This behavior is by design: there can be only **one** `FileManager` per file.
If two `FileAppender`s point to the same file, they both use the same `FileManager` and they will both use the same value of the `append` option.
Note that having more than one `FileAppender` per file is **not** a recommended configuration. This situation occurs naturally only during a reconfiguration process, when the "old" and the "new" appender coexist for a short moment to prevent log event loss.
If you really want to change the value of the `append` option you need to:
* either use a different file name,
* or explicitly stop the old "file" appender, before creating a new one. Warning: this will cause log event loss.
NOTE: even if the current implementation would allow you to create a second `FileManager`, by using `./logs/run.log`, I wouldn't recommend approach.
This is an implementation detail and we might change the implementation at some point (e.g. we could normalize the file names).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] The `append` option in file appender doesn't work properly (logging-log4j2)
Posted by "taylorThunderbolt (via GitHub)" <gi...@apache.org>.
taylorThunderbolt commented on issue #2661:
URL: https://github.com/apache/logging-log4j2/issues/2661#issuecomment-2168115153
@ppkarwasz @rgoers
got you guys point, thanks a lot ^_^
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] The `append` option in file appender doesn't work properly (logging-log4j2)
Posted by "rgoers (via GitHub)" <gi...@apache.org>.
rgoers closed issue #2661: The `append` option in file appender doesn't work properly
URL: https://github.com/apache/logging-log4j2/issues/2661
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] The `append` option in file appender doesn't work properly (logging-log4j2)
Posted by "taylorThunderbolt (via GitHub)" <gi...@apache.org>.
taylorThunderbolt commented on issue #2661:
URL: https://github.com/apache/logging-log4j2/issues/2661#issuecomment-2165926481
I have browsed through the source code to find where the `append` option is actually used. This option is used when opening a FileOutputStream:
https://github.com/apache/logging-log4j2/blob/fea2a7116160fb1555d578406444b4fc4f0ef2da/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/FileManager.java#L251
When different FileAppender use the same `fileName` option, they can use the same FileManager, which means they will share the same FileOutputStream:
https://github.com/apache/logging-log4j2/blob/fea2a7116160fb1555d578406444b4fc4f0ef2da/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/AbstractManager.java#L144
The outputstrem in same FileManager only instantiate once, so change the `append` option doesn't work properly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org