log4j–动态打印日志文件到指定文件夹
1、添加Maven依赖
<!-- log4j日志相关坐标 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<!-- 这里注意:如果是springBoot项目,不必再引入下边这个jar了,
因为spring-boot-starter本身就已经引入这个jar了。以免造成jar包冲突 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
2、配置文件 log4j.properties
log4j.rootLogger = debug,stdout,file
### 输出到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
### 输出到日志文件 ###
log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
###设置DatePattern,当天日志保存到log.log文件,前一天的日志文件名将被修改为log.log + _yyyy-MM-dd.log
log4j.appender.file.File =D/logs/log.log
log4j.appender.file.DatePattern = '_'yyyy-MM-dd'.log'
log4j.appender.file.Append = true
## 输出DEBUG级别以上的日志
log4j.appender.file.Threshold = DEBUG
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
3、编写日志打印工具类 LogUtil
package com.example.loginfo;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import java.io.File;
public class LogUtil {
private static Logger logger;
private static String filepath;
public static void setLog(String path) { //path:日志保存路径
filepath = path;
new File(filepath).mkdir(); //创建文件夹
}
public static void infoMsg(String filename, String msg) { //filename:生成日志的文件名 msg:日志信息
logger = Logger.getLogger("_" + filename);
DailyRollingFileAppender appender = new DailyRollingFileAppender();
appender.setFile(filepath + "/" + filename + ".log");
appender.setDatePattern("'.'yyyy-MM-dd");
PatternLayout layout = new PatternLayout("%-4r %-5p %d{yyyy-MM-dd HH:mm:ssS}%c %m%n");
appender.setLayout(layout);
appender.setAppend(true);
appender.activateOptions();
logger.addAppender(appender);
logger.setAdditivity(false);
logger.setLevel(Level.INFO);
logger.info(msg);
appender.close();
}
public Logger getLogger() {
return logger;
}
}
4、工具类调用
package com.example.loginfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class LogController {
Logger logger = LoggerFactory.getLogger(LogController.class);
@GetMapping(value = "/log")
public void testLog() {
LogUtil.setLog("E:\\home\\cuiyudong");
List<Object> objects = new ArrayList<>();
objects.add("FHIUFWIEU");
objects.add("DASD");
objects.add("FHIUCASFFGFWIEU");
LogUtil.infoMsg("LogController日至信息:",objects.toString());
}
}
测试结果:文章来源:https://www.toymoban.com/news/detail-600199.html
文章来源地址https://www.toymoban.com/news/detail-600199.html
到了这里,关于log4j--动态打印日志文件到指定文件夹的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!