如何创建两个日志文件

huangapple go评论60阅读模式
英文:

How to create two log files

问题

我有以下的log4j.properties文件:

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 = '.yyyy-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

当我运行MyApp时,我获得MyApp.log文件。我想知道是否有可能在代码中为两个主类设置两个文件?类似这样:

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")
  }
}

然后,当我运行MyApp1时,它会创建MyApp1.log,当我运行MyApp2时,它会创建MyApp2.log。

英文:

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

答案1

得分: 2

文件名可以在log4j.properties文件中参数化,然后作为JVM参数传递,

log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File = ${ApplicationName}.log

可以通过JVM参数传递ApplicationName作为实际文件名,

-DApplicationName=Myapp1

相同的log4j.properties文件可以被两个应用程序共享,并且您可以为每个应用程序在JVM参数中传递应用程序名称。

英文:

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.

答案2

得分: 1

你可以像这样为单个类配置日志文件 -

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

示例:

public class Myapp1 {
    Logger logger = Logger.getLogger(Myapp1.class);
    
    public Myapp1() {
        logger.debug("在 Myapp1 中");
    }
}

public class Myapp2 {
    Logger logger = Logger.getLogger(Myapp2.class);
    
    public Myapp2() {
        logger.debug("在 Myapp2 中");
    }
}

public class App {
    public static void main(String[] args) {
        Myapp1 myapp1 = new Myapp1();
        Myapp2 myapp2 = new Myapp2();
    }
}
英文:

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();
    }
}

huangapple
  • 本文由 发表于 2020年9月14日 19:18:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63883231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定