Reputation: 3287
I have the following log4j.properties files:
log.path=/var/log/MyApp
log4j.rootLogger=INFO, file
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File = MyApp.log
log4j.appender.file.Append = true
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=1000
log4j.appender.file.DatePattern = '.'yyy-MM-dd
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern =%d{yyyy-MM-dd HH:mm:ss} %t/%c{1} [%p] %m%n
When I run MyApp, I obtain MyApp.log file. I wonder if it is possible to set in the code two files for two main class? something like:
object MyApp1 {
def main(args: Array[String]) = {
log4j.setName("File" -> "MyApp1.log")
println("HelloWorld")
}
}
object MyApp2 {
def main(args: Array[String]) = {
log4j.setName("File" -> "MyApp2.log")
println("HelloWorld")
}
}
and then, when I will run Myapp1 it will create Myapp1.log and when I will run Myapp2 it will create Myapp2.log
Upvotes: 2
Views: 1030
Reputation: 1237
You can configure the log files for individual class like this -
log4j.logger.com.logger.test.logger_test.MyApp1=DEBUG, appender1
log4j.additivity.com.logger.test.logger_test.MyApp1=false
log4j.appender.appender1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.appender1.File=logs/MyApp1.log
log4j.appender.appender1.layout=org.apache.log4j.PatternLayout
log4j.logger.com.logger.test.logger_test.MyApp2=DEBUG, appender2
log4j.additivity.com.logger.test.logger_test.MyApp2=false
log4j.appender.appender2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.appender2.File=logs/MyApp2.log
log4j.appender.appender2.layout=org.apache.log4j.PatternLayout
Example :
public class Myapp1 {
Logger logger = Logger.getLogger(Myapp1.class);
public Myapp1() {
logger.debug("In Myapp1");
}
}
public class Myapp2 {
Logger logger = Logger.getLogger(Myapp2.class);
public Myapp2() {
logger.debug("In Myapp2");
}
}
public class App {
public static void main( String[] args ){
Myapp1 myapp1 = new Myapp1();
Myapp2 myapp2 = new Myapp2();
}
}
Upvotes: 1
Reputation: 182
The file name can be parameterized in the log4j.properties file and passed as a JVM argument,
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File = ${ApplicationName}.log
Can pass the ApplicationName as a JVM argument with actual file name,
-DApplicationName=Myapp1
Same log4j.properties file can be shared by both applications and you can pass the application name in the JVM parameter for each application.
Upvotes: 2