Commons logging to use java.util.logging
Asked Answered
K

4

3

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.

Krissykrista answered 26/2, 2011 at 3:37 Comment(0)
G
4

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.

Glenine answered 24/1, 2013 at 21:12 Comment(2)
This works (a simple Java project). But trying the same with a Maven project in Eclipse/STS does not. Tried putting the log configuration file (I put all the configuration inside commons-logging.properties itself) inside "src/main/resources" and "src/main/java" and root of the project, it is not being picked up. What is the classpath in this case, while using maven?Sterilize
src/main/resources should be correct. If it's not working you need to verify your assumptions about what's being included in your classpath. You might also want to verify that everything is packaged correctly by cracking open you jar/war to ensure the file is where you expect it.Glenine
P
0
# Loggers and Handlers may override this level

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

Porphyroid answered 26/2, 2011 at 7:47 Comment(1)
Sorry. This did not work. And my contents of src/main/resources/commons-logging.properties : org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14LoggerKrissykrista
O
0

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.

Oatmeal answered 31/7, 2014 at 8:57 Comment(0)
P
0

While Snekse's answer is basically correct, there is one point to correct/clarify:

  1. Correct: commons-logging searches its property file from classpath. Thus, commons-logging.properties must be found in the classpath
  2. Not correct: the JDK logger (JUL-logger) loads its configuration through a FileInputStream (which works on file system objects and not on classpath). See java.util.logging.LogManager.readConfiguration(). Thus, the System-Property value of "java.util.logging.config.file" must either be an absolute path or relative to the actual execution directory.
Printing answered 17/12, 2019 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.