转自:
下文笔者讲述SpringBoot配置log4j的方法分享,如下所示
SpringBoot日志输出
springboot框架中默认使用logback进行日志输出
当然它也可以配置其它的日志框架
SpringBoot配置log4j
Springboot配置log4j的方法:
1.添加log4j.properties配置文件
2.初始化配置文件
配置log4j文件
添加log4j.properties配置文件
配置文件添加的路径
在工程src/main/resources下
文件内容
#log4j.rootLogger=CONSOLE,info,error,DEBUG log4j.rootLogger=info,error,CONSOLE,DEBUG log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n log4j.logger.info=info log4j.appender.info=org.apache.log4j.DailyRollingFileAppender log4j.appender.info.layout=org.apache.log4j.PatternLayout log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n log4j.appender.info.datePattern='.'yyyy-MM-dd log4j.appender.info.Threshold = info log4j.appender.info.append=true #log4j.appender.info.File=/home/java265project/logs/info/api_services_info log4j.appender.info.File=/Users/java265project/logs/info/api_services_info log4j.logger.error=error log4j.appender.error=org.apache.log4j.DailyRollingFileAppender log4j.appender.error.layout=org.apache.log4j.PatternLayout log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n log4j.appender.error.datePattern='.'yyyy-MM-dd log4j.appender.error.Threshold = error log4j.appender.error.append=true #log4j.appender.error.File=/home/java265project/logs/error/api_services_error log4j.appender.error.File=/Users/java265project/logs/error/api_services_error log4j.logger.DEBUG=DEBUG log4j.appender.DEBUG=org.apache.log4j.DailyRollingFileAppender log4j.appender.DEBUG.layout=org.apache.log4j.PatternLayout log4j.appender.DEBUG.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n log4j.appender.DEBUG.datePattern='.'yyyy-MM-dd log4j.appender.DEBUG.Threshold = DEBUG log4j.appender.DEBUG.append=true #log4j.appender.DEBUG.File=/home/java265project/logs/debug/api_services_debug log4j.appender.DEBUG.File=/Users/java265project/logs/debug/api_services_debug ### Debug log4j.logger.com.ibatis=DEBUG log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
注意事项: 1.log4j.properties文件中 当部署线上环境,需要释放掉注释语句,并注释线下配置语句。 #log4j.appender.info.File=/home/java265project/logs/info/api_services_info log4j.appender.info.File=/Users/java265project/logs/info/api_services_info 2.日志配置涉及三个级别:info、error、debug
例:
初始化log4j配置
springboot启动是通过main入口函数
需在main入口函数中设置log4j初始化
public class App { private static final Logger logger = LoggerFactory.getLogger(App.class); @Value("${server.port}") private int port; @Value("${server.sessionTimeout}") private int sessionTimeout; static { try{ // 初始化log4j String log4jPath = ""; if(Constant.LOG_MODE == 1){ // 配置线上地址 log4jPath = App.class.getClassLoader().getResource("").getPath()+"java265project/config/log4j.properties"; logger.info("Log4j线上生产模式初始化。。。"); }else{ // 配置本地地址 log4jPath = App.class.getClassLoader().getResource("").getPath()+"log4j.properties"; logger.info("Log4j线下开发模式初始化。。。"); } logger.info("初始化Log4j。。。。"); logger.info("path is "+ log4jPath); PropertyConfigurator.configure(log4jPath); }catch (Exception e){ logger.error(e.toString()); } } public static void main(String[] args) { final String[] temp = args; logger.info("oops: main入口函数编码-" +System.getProperty("file.encoding")); if(Constant.LOG_MODE == 0){ SpringApplication.run(App.class, args); logger.info("oops: 线下开发测试"); }else{ logger.info("oops:" + args[0]); } if(ArrayUtils.isNotEmpty(args)) { // 如果你的应用程序,运行后不自动退出,那么处理start时不要执行正常的代码,否则在部署测试 appctl.sh 的时候,会一直等待进程退出 if(args[0].equals("startup")) { new Thread( new Runnable(){ public void run(){ System.out.println("启动Mythread子线程"); logger.info("启动Mythread子线程"); SpringApplication.run(App.class, temp); } }).start(); System.out.println("program startup"); logger.info("program startup"); }else if(args[0].equals("stop")) { System.out.println("program stop"); logger.info("program stop"); }else if(args[0].equals("restart")) { System.out.println("program restart"); logger.info("program restart"); }else if(args[0].equals("status")) { System.out.println("program status"); logger.info("program status"); } } }
优化配置
可对log4j.properties文件进行调整
使其配置更简单
log4j.rootLogger=${log.root} log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n log4j.logger.info=info log4j.appender.info=org.apache.log4j.DailyRollingFileAppender log4j.appender.info.layout=org.apache.log4j.PatternLayout log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n log4j.appender.info.datePattern='.'yyyy-MM-dd log4j.appender.info.Threshold = info log4j.appender.info.append=true log4j.appender.info.File=${log.base}/java265project/logs/info/api_services_info log4j.logger.error=error log4j.appender.error=org.apache.log4j.DailyRollingFileAppender log4j.appender.error.layout=org.apache.log4j.PatternLayout log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n log4j.appender.error.datePattern='.'yyyy-MM-dd log4j.appender.error.Threshold = error log4j.appender.error.append=true log4j.appender.error.File=${log.base}/java265project/logs/error/api_services_error log4j.logger.DEBUG=DEBUG log4j.appender.DEBUG=org.apache.log4j.DailyRollingFileAppender log4j.appender.DEBUG.layout=org.apache.log4j.PatternLayout log4j.appender.DEBUG.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n log4j.appender.DEBUG.datePattern='.'yyyy-MM-dd log4j.appender.DEBUG.Threshold = DEBUG log4j.appender.DEBUG.append=true log4j.appender.DEBUG.File=${log.base}/java265project/logs/debug/api_services_debug ### Debug log4j.logger.com.ibatis=DEBUG log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
与之相应的main函数新增如下代码,同时去掉static代码块:
System.setProperty("log.root",Constant.LOG_ROOT_DAILY); System.setProperty("log.base",Constant.LOG_PATH_DAILY);
转载:https://blog.csdn.net/qq_25073223/article/details/127957911
查看评论