NutzCN Logo
精华 nutz-1.b.57/58与log4j2集成的问题
发布于 2993天前 作者 wukonggg 3309 次浏览 复制 上一个帖子 下一个帖子
标签: log4j

实现方式为在pom中配置log4j-1.2-api。
同样的代码和配置,1.b.57可以,但是1.b.58就不行了,报错如下

log4j:WARN No appenders could be found for logger (com.....AK47).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
21 回复

依然存在log4j 1.2的jar呢, 要移除掉

尝试1: 移除,仍然不行。没找到问题在哪。
日志如下

log4j:WARN No appenders could be found for logger (com.hoperun.epu.nwims.ak47.AK47).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
***************************************************
Application: AK47
Version:     0.1.0
Description: A date generator and loader for nwims
***************************************************

代码如下

public class AK47 {

    private static Log log = null;
    private static Ioc ioc = null;

    static {
        System.setProperty("log4j.configurationFile", "etc/log4j2.xml");
//        Log4j2LogAdapter adapter = new Log4j2LogAdapter();
//        if (adapter.canWork()) {
//            Logs.setAdapter(adapter);
//        } else {
//            System.out.println("日志组件没有正常启动!");
//        }
        log = Logs.get();
    }

    public static void main(String[] args) {
        ThreadContext.put("threadName", Thread.currentThread().getName());
        Thread.currentThread().setName("ak47-main");
        welcome();
        ioc = IocMaster.getInstance();
//        loadIoc();
//        now();
    }


    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 for nwims");
        System.out.println("***************************************************");
        log.info("AK47 has started. Please enjoy it!");
//
    }
//    private static void now() {
//        Dao dao = ioc.get(Dao.class);
//        Sql sql = Sqls.fetchString("select sysdate from dual");
//        sql.setCallback(new FetchTimestampCallback());
//        dao.execute(sql);
//        log.info("Server time: " + sql.getString());
//    }
}

POM如下:

 <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>

尝试2: 自己写Adapter。 可以,但是日志中打印的类名仍然不对,没查到原因。。
日志如下
***************************************************
Application: AK47
Version: 0.1.0
Description: A date generator and loader for nwims
***************************************************
2016-10-11 17:14:54,831 INFO [ak47-main] Log4j2LogAdapter$Log4j2Logger - AK47 has started. Please enjoy it!

代码如下

public class AK47 {

    private static Log log = null;
    private static Ioc ioc = null;

    static {
        System.setProperty("log4j.configurationFile", "etc/log4j2.xml");
        Log4j2LogAdapter adapter = new Log4j2LogAdapter();
        if (adapter.canWork()) {
            Logs.setAdapter(adapter);
        } else {
            System.out.println("日志组件没有正常启动!");
        }
        log = Logs.get();
    }

    public static void main(String[] args) {
        ThreadContext.put("threadName", Thread.currentThread().getName());
        Thread.currentThread().setName("ak47-main");
        welcome();
        ioc = IocMaster.getInstance();
//        loadIoc();
//        now();
    }


    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 for nwims");
        System.out.println("***************************************************");
        log.info("AK47 has started. Please enjoy it!");
//
    }
//    private static void now() {
//        Dao dao = ioc.get(Dao.class);
//        Sql sql = Sqls.fetchString("select sysdate from dual");
//        sql.setCallback(new FetchTimestampCallback());
//        dao.execute(sql);
//        log.info("Server time: " + sql.getString());
//    }
}
public class Log4j2LogAdapter implements LogAdapter {

    public Log4j2LogAdapter() {
    }

    public boolean canWork() {
        try {
            Class.forName("org.apache.logging.log4j.Logger", false, Log4j2LogAdapter.class.getClassLoader());
            return true;
        } catch (Throwable t) {
            return false;
        }
    }

    public Log getLogger(String className) {
        return new Log4j2LogAdapter.Log4j2Logger(className);
    }

    static class Log4j2Logger extends AbstractLog {
        private Logger logger;
        private static boolean hasTrace;

        Log4j2Logger(String className) {
            this.logger = LogManager.getLogger(className);
            this.isFatalEnabled = this.logger.isEnabled(Level.FATAL);
            this.isErrorEnabled = this.logger.isEnabled(Level.ERROR);
            this.isWarnEnabled = this.logger.isEnabled(Level.WARN);
            this.isInfoEnabled = this.logger.isEnabled(Level.INFO);
            this.isDebugEnabled = this.logger.isEnabled(Level.DEBUG);
            if (hasTrace) {
                this.isTraceEnabled = this.logger.isEnabled(Level.TRACE);
            }

        }

        public void debug(Object message, Throwable t) {
            if (this.isDebugEnabled()) {
                this.logger.log(Level.DEBUG, message, t);
            }
        }

        public void error(Object message, Throwable t) {
            if (this.isErrorEnabled()) {
                this.logger.log(Level.ERROR, message, t);
            }
        }

        public void fatal(Object message, Throwable t) {
            if (this.isFatalEnabled()) {
                this.logger.log(Level.FATAL, message, t);
            }
        }

        public void info(Object message, Throwable t) {
            if (this.isInfoEnabled()) {
                this.logger.log(Level.INFO, message, t);
            }
        }

        public void trace(Object message, Throwable t) {
            if (this.isTraceEnabled()) {
                this.logger.log(Level.TRACE, message, t);
            } else if (!hasTrace && this.isDebugEnabled()) {
                this.logger.log(Level.DEBUG, message, t);
            }
        }

        public void warn(Object message, Throwable t) {
            if (this.isWarnEnabled()) {
                this.logger.log(Level.WARN, message, t);
            }
        }

        protected void log(int level, Object message, Throwable t) {
            switch (level) {
                case 0:
                    if (hasTrace) {
                        this.logger.log(Level.TRACE, message, t);
                    } else {
                        this.logger.log(Level.DEBUG, message, t);
                    }
                    break;
                case 10:
                    this.logger.log(Level.DEBUG, message, t);
                    break;
                case 20:
                    this.logger.log(Level.INFO, message, t);
                    break;
                case 30:
                    this.logger.log(Level.WARN, message, t);
                    break;
                case 40:
                    this.logger.log(Level.ERROR, message, t);
                    break;
                case 50:
                    this.logger.log(Level.FATAL, message, t);
            }

        }

        public boolean isDebugEnabled() {
            return this.logger.isDebugEnabled();
        }

        public boolean isErrorEnabled() {
            return this.logger.isEnabled(Level.ERROR);
        }

        public boolean isFatalEnabled() {
            return this.logger.isEnabled(Level.FATAL);
        }

        public boolean isInfoEnabled() {
            return this.logger.isInfoEnabled();
        }

        public boolean isTraceEnabled() {
            return !hasTrace ? this.logger.isDebugEnabled() : this.logger.isTraceEnabled();
        }

        public boolean isWarnEnabled() {
            return this.logger.isEnabled(Level.WARN);
        }

        static {
            try {
                Level.class.getDeclaredField("TRACE");
                hasTrace = true;
            } catch (Throwable var1) {
                System.out.println(var1.getMessage());;
                var1.printStackTrace();
            }

        }
    }
}

是eclipse不? 打开pom.xml的依赖树,看看哪个jar依赖的log4j 1.2, 然后排除掉

使用Log4j2的LogManager,输出日志正常,但是设置了Adapter(3楼)就不正常了

Logger log = LogManager.getLogger();

@wendal 有依赖树的, IDEA。显示依赖的就是nutz。但是不知道怎么改。。。

代码中使用API
Log4j2的LogManager,输出日志正常,但是设置了Adapter(3楼)就不正常了
NUTZ的log api,输出日志异常(3楼)

这样改一下

		<dependency>
			<groupId>org.nutz</groupId>
			<artifactId>nutz</artifactId>
			<version>1.r.58</version>
			<exclusions>
				<exclusion>
					<artifactId>log4j</artifactId>
					<groupId>log4j</groupId>
				</exclusion>
			</exclusions>
		</dependency>

话说还真是个bug,是打包pom的bug, 因为改成gradle打包的,生成的pom中的log4j没有设置为provided

exclude也不行。呵呵

exclusion能排除的啊

排出是可以。但是排除》rebuild》run。输入仍然类名不对

***************************************************
Application: AK47
Version:     0.1.0
Description: A date generator and loader for nwims
***************************************************
2016-10-11 17:50:38,372 INFO  [ak47-main] Log4j2LogAdapter$Log4j2Logger - AK47 has started. Please enjoy it!

应该是AK47,而不是Log4j2LogAdapter$Log4j2Logger

正常结果如下

***************************************************
Application: AK47
Version:     0.1.0
Description: A date generator and loader for nwims
***************************************************
2016-10-11 17:52:05,844 INFO  [ak47-main] AK47 - AK47 has started. Please enjoy it!

不应该是打印log变量的class吗?

没有自定义log适配器,然后log4j-api又存在,所以返回log4j 1.2适配器,这个行为没错呢

最后总结一下:

nutz-1.r.58貌似是有个小bug。。
总结如下:

日志现象

在nutz-1.b-53验证

ALL Nutz Log via Log4jLogAdapter
2016-10-12 09:39:39,400 INFO  [main] Logs - Nutz is licensed under the Apache License, Version 2.0 .
Report bugs : https://github.com/nutzam/nutz/issues
2016-10-12 09:39:40,852 INFO  [ak47-main] AnnotationIocLoader - Scan complete ! Found 5 classes in 1 base-packages!
beans = ["samplePerfKpiAnalyzer", "littleBlueBird", "samplePerfDao", "skGenerator", "generalStatDao"]
2016-10-12 09:39:40,863 INFO  [ak47-main] NutIoc - NutIoc init begin ...
2016-10-12 09:39:40,866 INFO  [ak47-main] NutIoc - ... NutIoc init complete
2016-10-12 09:39:40,867 INFO  [ak47-main] AK47 - AK47 has started. Please enjoy it!

在nutz-1.r.57验证

2016-10-12 09:40:09,801 INFO  [ak47-main] AnnotationIocLoader - Found 5 classes in 1 base-packages!
beans = ["samplePerfKpiAnalyzer", "littleBlueBird", "samplePerfDao", "skGenerator", "generalStatDao"]
2016-10-12 09:40:09,827 INFO  [ak47-main] NutIoc - NutIoc init begin ...
2016-10-12 09:40:09,829 INFO  [ak47-main] NutIoc - ... NutIoc init complete
2016-10-12 09:40:09,830 INFO  [ak47-main] AK47 - AK47 has started. Please enjoy it!

与1.b.53相比INFO日志有所变化,少了如下内容

ALL Nutz Log via Log4jLogAdapter
2016-10-12 09:39:39,400 INFO  [main] Logs - Nutz is licensed under the Apache License, Version 2.0 .
Report bugs : https://github.com/nutzam/nutz/issues

在nutz-1.r.58验证

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.

源码

pom.xml

<dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-1.2-api</artifactId>
            <version>2.3</version>
        </dependency>

使用log4j-1.2-api做桥接

Log4j2.xml

Level:info

AK47.java

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() {
        log.info("AK47 has started. Please enjoy it!");
    }
}

@wendal 还不赶紧加精!!!

log4j 1.2的jar依然没有排除

针对18楼的补充

POM文件调整

关键配置

<dependency>
            <groupId>org.nutz</groupId>
            <artifactId>nutz</artifactId>
            <!--<version>1.b.53</version>-->
            <!--<version>1.r.57</version>-->
            <version>1.r.58</version>
            <exclusions>
                <exclusion>
                    <artifactId>log4j</artifactId>
                    <groupId>log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
		<dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-1.2-api</artifactId>
            <version>2.3</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>

运行日志

2016-10-12 11:25:09,372 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 11:25:11,171 INFO  [ak47-main] AnnotationIocLoader - Found 5 classes in 1 base-packages!
beans = ["samplePerfKpiAnalyzer", "littleBlueBird", "samplePerfDao", "skGenerator", "generalStatDao"]
2016-10-12 11:25:11,198 INFO  [ak47-main] NutIoc - NutIoc init begin ...
2016-10-12 11:25:11,201 INFO  [ak47-main] NutIoc - ... NutIoc init complete
***************************************************
Application: AK47
Version:     0.1.0
Description: A date generator and loader for nwims
***************************************************
2016-10-12 11:25:11,201 INFO  [ak47-main] AK47 - AK47 has started. Please enjoy it!

这样就可以正常运行了

总结帖:https://my.oschina.net/allman90/blog/757716

添加回复
请先登陆
回到顶部