log4j和log4j2动态修改日志文件名
log4j动态修改日志文件名方法:
log4j.properties
log4j.rootLogger=INFO, stdout, filelog4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%p]-[Thread: %t]-[%C.%M()]: %m%n
# Output to the File
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/app.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%p]-[Thread: %t]-[%C.%M()]: %m%n
log4j.appender.file.maxFileSize=2MB
log4j.appender.file.maxBackupIndex=5
import org.apache.log4j.Logger;import org.apache.log4j.RollingFileAppender;
import java.util.Enumeration;
import java.util.List;
public class ToolUtil {
public static void setLogFile(String name){
Logger rootLogger = Logger.getRootLogger();
Enumeration en = rootLogger.getAllAppenders();
while (en.hasMoreElements()){
Object obj = en.nextElement();
if(obj instanceof RollingFileAppender){
RollingFileAppender file = (RollingFileAppender)obj;
file.setFile(name);
file.activateOptions();
}
}
}
public static void main(String[] args){
ToolUtil.setLogFile("log/" + ToolUtil.class.getSimpleName()+ ".log");
}
}
log4j2的接口方法与log4j不同,不能直接用log4j的方法。
log4j2动态修改日志文件名方法:
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?><configuration status="INFO">
<appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<RollingFile name="appFile" fileName="logs/app.log"
filePattern="logs/app.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} %L - %msg%xEx%n"/>
<SizeBasedTriggeringPolicy size="10 MB" />
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</appenders>
<loggers>
<root level="INFO">
<appender-ref ref="console"/>
<appender-ref ref="appFile"/>
</root>
</loggers>
</configuration>
import org.apache.commons.codec.binary.Hex;import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.RollingFileAppender;
public class ToolUtil {
public static void setLogFile(String name){
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
org.apache.logging.log4j.core.config.Configuration config = ctx.getConfiguration();
if (config.getAppender("appFile") != null) {
RollingFileAppender rollingFileAppender = (RollingFileAppender)config.getAppender("appFile");
config.getLoggerConfig("").removeAppender("appFile");
rollingFileAppender.stop();
Appender appender = RollingFileAppender.newBuilder()
.setName("appFile2")
.withFileName(name)
.withFilePattern(rollingFileAppender.getFilePattern())
.setLayout(rollingFileAppender.getLayout())
.withPolicy(rollingFileAppender.getTriggeringPolicy())
.build();
appender.start();
config.getLoggerConfig("").addAppender(appender, config.getRootLogger().getLevel(), null);
ctx.updateLoggers(config);
}
}
public static void main(String[] args){
ToolUtil.setLogFile("log/" + ToolUtil.class.getSimpleName()+ ".log");
}
}
以上是 log4j和log4j2动态修改日志文件名 的全部内容, 来源链接: utcz.com/z/516430.html