We use log4j 1.2.x to enter our product and want to upgrade to log4j 2.x in the near future. One of the functionality that we have implemented is to register system information and other important parameters on each new minimized log file that is generated. The way we implemented in log4j 1.2.x is that we extended the RollingFileAppender class and redefined the rollOver() method, below is a snippet of the implementation fragment
@Override public void rollOver() { super.rollOver();
Now that we want to switch to log4j2, we are considering a new solution to achieve the same functionality. But since I see the source code for log4j2, it is very different from the old source code. The RollingFileAppender class RollingFileAppender not contain the rollOver() method, since it was moved to the RollingManagerhelper , and it was set to private as-well.
Developing a complete new package and extending / implementing some abstract / helper classes from log4j2 is one of the possible solutions for us, but this will require a lot of coding / copying, since we are not modifying what RollingFileAppender does most likely only require a small extension. Is there a simple solution?
UPDATE
I created a custom search according to the sentence in the answers and below how I created it;
@Plugin(name = "property", category = StrLookup.CATEGORY) public class CustomLookup extends AbstractLookup { private static AtomicLong aLong = new AtomicLong(0); @Override public String lookup(LogEvent event, String key) { if (aLong.getAndIncrement() == 0) { return "this was first call"; } if (key.equalsIgnoreCase("customKey")) { return getCustomHeader(); } else { return "non existing key"; } } private static String getCustomHeader() {
But this did not work, as mentioned; it always prints this was first call in the header. I also tried to put a breakoint in the first if state, and I noticed that it gets only once. Therefore, I am afraid that the customLookup class is initialized only at startup, when log4j2 initializes its properties from xml config. I do not know how else I could implement this custom search class.
UPDATE 2
After the implementation described above, I tried it differently, as shown below:
private static AtomicLong aLong = new AtomicLong(0); @Override public String lookup(LogEvent event, String key) { return getCustomHeader(key); } private static String getCustomHeader(final String key) { if (aLong.getAndIncrement() == 0) { return "this was first call"; } if (key.equalsIgnoreCase("customKey")) {
But it does the same as ... well. log4j2 creates headers on initialization from its xml configuration file and then uses headers from memory. The return value of the overridden lookup() method cannot be changed dynamically, since it is only called during initialization. Any help would be greatly appreciated.