随笔记录程序员成长之路
在Java程序中设置Log4j的方法有很多种,这里我只尝试过一种并且成功了。
首先,需要在main方法中加入一个参数来接收Log4j的properties文件:
//Log4j properties file
String log4jProperties = args[5];
然后创建file
File log4jFile =new File(log4jProperties);
如果你想把log的名字传进去,可以设置system的property
System.setProperty("LOGGINGNAME","hdfsputService");
最后用PropertyConfigurator来配置就行了
PropertyConfigurator.configure(log4jFile.getAbsolutePath());
以下是配置文件
# Spark Streaming Logging Configuration
# See also: http://spark.apache.org/docs/2.0.2/running-on-yarn.html#debugging-your-application
log4j.rootLogger=INFO, logRollingFile
log4j.appender.logRollingFile =org.apache.log4j.RollingFileAppender
log4j.appender.logRollingFile.layout =org.apache.log4j.PatternLayout
log4j.appender.logRollingFile.layout.ConversionPattern =%d %p %c %m %n
log4j.appender.logRollingFile.Threshold =DEBUG
log4j.appender.logRollingFile.ImmediateFlush =TRUE
log4j.appender.logRollingFile.Append =TRUE
log4j.appender.logRollingFile.File =/tmp/delevin/log/${LOGGINGNAME}.log
log4j.appender.logRollingFile.MaxFileSize =50MB
log4j.appender.logRollingFile.MaxBackupIndex =10
log4j.appender.logRollingFile.Encoding =UTF-8
网友评论