2

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

Yuvaraj G
  • 1,157
  • 9
  • 17
a.moussa
  • 2,977
  • 7
  • 34
  • 56
  • Does this answer your question? [log4j: Log output of a specific class to a specific appender](https://stackoverflow.com/questions/2763740/log4j-log-output-of-a-specific-class-to-a-specific-appender) – Izhar Ahmed Sep 14 '20 at 12:11

2 Answers2

2

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.

Seralahthan
  • 182
  • 11
1

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();
    }
}
Yuvaraj G
  • 1,157
  • 9
  • 17
  • It create two logs files but I have nothing inside logs files. I am not able to add logs into files – a.moussa Sep 14 '20 at 13:48
  • Did you change the package names accordingly `com.logger.test.logger_test` should be your package name. I tested this code and it works. – Yuvaraj G Sep 14 '20 at 14:21