I implemented Log4J logging as part of my project for my Java Desktop application and it works fine .
Now i have another project and want that both projects share the same log file . Both these projects are bundled together in my application so it makes sense for them to share the log files . How is this possible ?
Currently this is what i have :
LoggerClass:
package com.application.logging;
import org.apache.log4j.Logger;
public class LoggingSample {
private static Logger logger = Logger.getLogger("Visualx");
public static Logger getLogger() {
return logger;
}
public static void setLogger(Logger logger) {
LoggingSample.logger = logger;
}
}
Log4j.properties file :
# Log levels
log4j.rootLogger=INFO,CONSOLE,R
# Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file
log4j.appender.R.File=${user.home}/log/testlog.log
log4j.appender.R.MaxFileSize=200KB
# Number of backup files
log4j.appender.R.MaxBackupIndex=2
# Layout for Rolling File Appender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n
Even if it was technical possible (I doubt it, because two processes would need to open one file at the same time), this is generally not considered best practise.
Better write into separate files and make sure that the actual log statements make sense and e.g. use references that allow to trace an event through all log files.
If you really need to merge / bundle things together I'd recommend to implement that on OS level (cronjob, ...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With