GitHub user ppkarwasz added a comment to the discussion: Java CRaC support on 
static defined Loggers

Log4j Core does define a 
[`LifeCycle`](https://logging.apache.org/log4j/2.x/javadoc/log4j-core/org/apache/logging/log4j/core/LifeCycle.html)
 interface, and many Core plugins implement it. In principle this means that 
calling `AbstractConfiguration#stop()` and then `AbstractConfiguration#start()` 
could be used to restart the logging system.

However, `LifeCycle` support in Log4j Core is currently inconsistent and 
largely untested, especially for appenders. For example:

* Most file-based appenders open files in their constructors rather than in 
`start()`.
* As a result, calling `stop()` followed by `start()` will not correctly 
re-acquire file resources.
* Similar issues exist in other components which manage I/O resources.

Improving this is feasible but non-trivial. It would require refactoring 
components to follow a consistent LifeCycle contract:

* Resource acquisition should happen in `start()`, not in the constructor.
* Resource release should happen in `stop()`.
* Components should be prepared to operate through multiple start/stop cycles.

We could make progress incrementally by addressing the most commonly used 
components first:

1. `ConsoleAppender`
2. `FileAppender`, `RandomAccessFileAppender` and `MemoryMappedFileAppender`,
3. `RollingFileAppender` and `RollingRandomcAccessFileAppender`
4. Others (e.g. socket appenders, JDBC appender, etc.)

This is an area where contributions are welcome.

### On Managers (`FileManager`, etc.)

Managers like `FileManager` are not good candidates for restart logic. They are 
designed as thin wrappers around resources and are cached globally via the 
`AbstractManager` registry. Once a `FileManager` is closed, it unregisters 
itself from the `AbstractManager` registry and should not be reused. Restarting 
a configuration should create new managers rather than trying to restart old 
ones.

A better approach would be to:

* Move creation of managers (like `FileManager`) out of the appender 
constructor/builder.
* Instantiate managers inside the appender's `start()` method instead.
* Ensure `stop()` closes and unregisters managers properly.


GitHub link: 
https://github.com/apache/logging-log4j2/discussions/3954#discussioncomment-14813872

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]

Reply via email to