1. 来龙去脉
生成日志非常重要的程序,需要考虑未来切换日志方案的可能。在使用nutz时,nutz会提供一套日志的adaptor,方便以后的切换。具体使用时,可以在程序中使用nutz提供的log相关api,而不是直接使用log4j1,log4j或者其他的日志实现。
升级到nutz-1.r.58后,这种方法会带来nutz自身的日志会打印异常,log4j找不到appender了。具体日志(程序启动后)如下:
log4j:WARN No appenders could be found for logger (org.nutz.resource.Scans).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
2. 解决方案
2.1. 版本说明
- log4j2-2.3
- nutz-1.b.53, nutz-1.r.57, nutz-1.b.58尝试。
2.2. 核心通用代码及配置
2.2.1. 代码
import com.hoperun.epu.base.nutz.IocMaster;
import org.apache.logging.log4j.ThreadContext;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.impl.sql.callback.FetchTimestampCallback;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.Ioc;
import org.nutz.log.Log;
import org.nutz.log.Logs;
public class AK47 {
private static Log log = null;
private static Ioc ioc = null;
static {
System.setProperty("log4j.configurationFile", "etc/log4j2.xml");
log = Logs.get();
}
public static void main(String[] args) {
ThreadContext.put("threadName", Thread.currentThread().getName());
Thread.currentThread().setName("ak47-main");
ioc = IocMaster.getInstance();
welcome();
}
private static void welcome() {
System.out.println("***************************************************");
System.out.println("Application: AK47");
System.out.println("Version: 0.1.0");
System.out.println("Description: A date generator and loader");
System.out.println("***************************************************");
log.info("AK47 has started. Please enjoy it!");
}
}
2.2.2. Log4j2配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="info" packages="com.rhfdz.epu">
<properties>
<!-- 控制台输出日志格式 -->
<property name="patterns.console">%d %-5p [%t] %C{1} - %m%n</property>
<!-- 日志文件输出日志格式 -->
<Property name="patterns.file">%d %-5p [%t] %C - %m%n</Property>
<!-- 日志文件大小 -->
<Property name="file.size">50 MB</Property>
<!-- 日志文件最大个数 -->
<Property name="file.maxIndex">10</Property>
</properties>
<appenders>
<console name="console" target="SYSTEM_OUT">
<!--这个都知道是输出日志的格式-->
<PatternLayout pattern="${patterns.console}"/>
</console>
<routing name="routing">
<routes pattern="$${thread:threadName}">
<route>
<rollingFile name="logFile-${thread:threadName}"
fileName="log/${thread:threadName}.log"
filePattern="log/$${date:yyyy-MM}/${thread:threadName}-%d{MM-dd-yyyy}-%i.log.gz">
<patternLayout pattern="${patterns.file}" />
<policies>
<sizeBasedTriggeringPolicy size="${file.size}" />
</policies>
<defaultRolloverStrategy max="${file.maxIndex}" />
</rollingFile>
</route>
</routes>
</routing>
<!--<async name="async" bufferSize="1000" includeLocation="true">-->
<!--<appenderRef ref="routing" />-->
<!--<appenderRef ref="console" />-->
<!--</async>-->
</appenders>
<loggers>
<logger name="com.rhfdz.epu" level="debug"/>
<logger name="org.nutz" level="info"/>
<root level="info">
<!--<appenderRef ref="async" />-->
<appenderRef ref="console" />
<appenderRef ref="routing" />
</root>
</loggers>
</configuration>
2.3. 不同nutz版本的处理方法
2.3.1. nutz-1.b.53
2.3.1.1. 日志效果
ALL Nutz Log via Log4jLogAdapter
2016-10-12 22:14:16,301 INFO [main] Logs - Nutz is licensed under the Apache License, Version 2.0 .
Report bugs : https://github.com/nutzam/nutz/issues
2016-10-12 22:14:17,711 INFO [ak47-main] AnnotationIocLoader - Scan complete ! Found 5 classes in 1 base-packages!
beans = ["samplePerfKpiAnalyzer", "littleBlueBird", "samplePerfDao", "skGenerator", "generalStatDao"]
2016-10-12 22:14:17,720 INFO [ak47-main] NutIoc - NutIoc init begin ...
2016-10-12 22:14:17,722 INFO [ak47-main] NutIoc - ... NutIoc init complete
***************************************************
Application: AK47
Version: 0.1.0
Description: A date generator and loader
***************************************************
2016-10-12 22:14:17,723 INFO [ak47-main] AK47 - AK47 has started. Please enjoy it!
2.3.1.2. maven依赖
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz</artifactId>
<version>1.b.53</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.3</version>
</dependency>
2.3.2. nutz-1.r.57
2.3.2.1. 日志效果
2016-10-12 22:21:01,417 INFO [ak47-main] AnnotationIocLoader - Found 5 classes in 1 base-packages!
beans = ["samplePerfKpiAnalyzer", "littleBlueBird", "samplePerfDao", "skGenerator", "generalStatDao"]
2016-10-12 22:21:01,443 INFO [ak47-main] NutIoc - NutIoc init begin ...
2016-10-12 22:21:01,446 INFO [ak47-main] NutIoc - ... NutIoc init complete
***************************************************
Application: AK47
Version: 0.1.0
Description: A date generator and loader
***************************************************
2016-10-12 22:21:01,446 INFO [ak47-main] AK47 - AK47 has started. Please enjoy it!
与1.b.53相比INFO日志有所变化,少了如下内容
ALL Nutz Log via Log4jLogAdapter
2016-10-12 22:14:16,301 INFO [main] Logs - Nutz is licensed under the Apache License, Version 2.0 .
Report bugs : https://github.com/nutzam/nutz/issues
2.3.2.2. maven依赖
与nutz-1.b.53的依赖配置基本相同,仅对nutz依赖做调整如下
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz</artifactId>
<version>1.r.57</version>
</dependency>
2.3.3. nutz-1.r.58
2.3.3.1. 日志效果
2016-10-12 22:26:03,466 INFO Log4j appears to be running in a Servlet environment, but there's no log4j-web module available. If you want better web container support, please add the log4j-web JAR to your web archive or server lib directory.
2016-10-12 22:26:05,094 INFO [ak47-main] AnnotationIocLoader - Found 5 classes in 1 base-packages!
beans = ["samplePerfKpiAnalyzer", "littleBlueBird", "samplePerfDao", "skGenerator", "generalStatDao"]
2016-10-12 22:26:05,111 INFO [ak47-main] NutIoc - NutIoc init begin ...
2016-10-12 22:26:05,114 INFO [ak47-main] NutIoc - ... NutIoc init complete
***************************************************
Application: AK47
Version: 0.1.0
Description: A date generator and loader for nwims
***************************************************
2016-10-12 22:26:05,114 INFO [ak47-main] AK47 - AK47 has started. Please enjoy it!
2.3.3.2. maven依赖
与nutz-1.b.53的依赖配置基本相同,仅对nutz依赖做调整如下
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz</artifactId>
<version>1.r.58</version>
<!-- 1.r.58的bug,需要excludelog4j,不然nutz会找不到appender -->
<!-- 参考:https://nutz.cn/yvr/t/1uj0eaetdei4kqr9qsipb53ics -->
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency>
如果不增加对log4j的exclusion,则会出现文章开头的情况。
3. 原始贴地址
本文内容的原始的讨论过程在nutz社区:https://nutz.cn/yvr/t/1uj0eaetdei4kqr9qsipb53ics