Commons logging to use java.util.logging

2019-05-26 22:44发布

I am trying to use commons logging and want to use java.util.logging as underlying mechanism.

LogTest.java

import org.apache.commons.logging.*;

public class LogTest { 

        public static void main(String args[]) {
            System.setProperty("java.util.logging.config.file","log.properties");
            Log logger = LogFactory.getLog(LogTest.class);
            logger.trace("trace msg");
        }
}

I have src/main/resources/log.properties ( I am using maven project )

handlers=java.util.logging.ConsoleHandler

# Default global logging level.
# Loggers and Handlers may override this level
.level=ALL

I am not able to see any output. Please tell me how to programatically set the log.properties to leverage java logging.

3条回答
beautiful°
2楼-- · 2019-05-26 23:07
# Loggers and Handlers may override this level

Try adding java.util.logging.ConsoleHandler.level=ALL in your log.properties.

查看更多
放荡不羁爱自由
3楼-- · 2019-05-26 23:10

You need to specify a logger that implements the Log interface. In this case, the Jdk14Logger.

Check out How to use 'Apache Commons Logging' with 'Java SE Logging'? for more details.

Except from link:

Put "commons-logging.properties" file in your application's classpath. The contents of this file should look like: org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger Setting this property instructs Commons-Logging to use JDK Logger (i.e. Java SE Logger) as the Log implementation.

Then put log-config.properties in your classpath and configure accordingly.

If you decide to change impls in the future, more loggers are available out of the box.

查看更多
相关推荐>>
4楼-- · 2019-05-26 23:11

create commons-logging.properties in your classpath:

org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger

set java.util.logging.config.file before you instance of Logger:

System.setProperty("java.util.logging.config.file","log.properties");

then the JDK logging will be appender all logs which used by org.apache.commons.logging.*;

example: spring, hibernate, struts etc.

查看更多
登录 后发表回答