Java 配置 log4j 以在运行时登录到自定义文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1324053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:16:08  来源:igfitidea点击:

configure log4j to log to custom file at runtime

javalog4j

提问by Fell

Can anyone please guide me as to how I can configure log4j to log to a specific file that I specify at run-time.The name and path of the log file are generated at run-time and the application must log to that particular file.

任何人都可以指导我如何配置 log4j 以登录到我在运行时指定的特定文件。日志文件的名称和路径是在运行时生成的,应用程序必须登录到该特定文件。

Generally file appender entries in the log4j.properties file points to the log file that will be used by the application.However in this case I would like to read the log file path from the command line and log to that particular file.

通常 log4j.properties 文件中的文件附加条目指向应用程序将使用的日志文件。但是在这种情况下,我想从命令行读取日志文件路径并记录到该特定文件。

How can I achieve this ?

我怎样才能做到这一点?

采纳答案by Vinay Sajip

Adapted from the log4j documentation:

改编自 log4j 文档:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.FileAppender;

public class SimpandFile {
   static Logger logger = Logger.getLogger(SimpandFile.class);
   public static void main(String args[]) {

      // setting up a FileAppender dynamically...
      SimpleLayout layout = new SimpleLayout();    
      FileAppender appender = new FileAppender(layout,"your filename",false);    
      logger.addAppender(appender);

      logger.setLevel((Level) Level.DEBUG);

      logger.debug("Here is some DEBUG");
      logger.info("Here is some INFO");
      logger.warn("Here is some WARN");
      logger.error("Here is some ERROR");
      logger.fatal("Here is some FATAL");
   }
}

回答by jmq

You can also do this from the log4j.properties file. Using the sample file below I have added the system property ${logfile.name}:

您也可以从 log4j.properties 文件执行此操作。使用下面的示例文件,我添加了系统属性${logfile.name}

# logfile is set to be a RollingFileAppender
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${logfile.name}
log4j.appender.logfile.MaxFileSize=10MB
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=[%-5p]%d{yyyyMMdd@HH\:mm\:ss,SSS}\:%c - %m%n

The log file name can then be set two different ways:

然后可以通过两种不同的方式设置日志文件名:

  1. As a command line, system property passed to java "-Dlogfile.name={logfile}"
  2. In the java program directly by setting a system property (BEFORE you make any calls to log4j).

    System.setProperty("logfile.name","some path/logfile name string");

  1. 作为命令行,系统属性传递给 java "-Dlogfile.name={logfile}"
  2. 在 java 程序中直接通过设置系统属性(在对 log4j 进行任何调用之前)。

    System.setProperty("logfile.name","一些路径/日志文件名字符串");

回答by Sumit

Can be also done by this properties define in log4j.properties file

也可以通过在 log4j.properties 文件中定义的这个属性来完成

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.maxFileSize=5000KB
log4j.appender.logfile.maxBackupIndex=5
log4j.appender.logfile.File=/WebSphere/AppServer/profiles/Custom01/error.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p [%t] %C %M %c{1}:%L - %m%n

回答by Manimaran Samuthirapandi

Working and same has been tested

工作和相同已经过测试

// setting up a FileAppender dynamically...
SimpleLayout layout = new SimpleLayout(); 
RollingFileAppender appender = new RollingFileAppender(layout,"file-name_with_location",true);
                    appender.setMaxFileSize("20MB");
                    logger.addAppender(appender);