Reputation: 17847
This may be obvious, but I have been looking for a simple example of using org.apache.commons.logging
to log to a specific file (i.e. /path/to/my-log.txt
)
public class MyClass {
static Log log = LogFactory.getLog(MyClass.class);
public static void main(String[] args) {
// What to do here to get log to output to a file???
log.info("I want to appear in a specific log file")
}
}
Upvotes: 1
Views: 16170
Reputation: 135992
It depends on logging implementation that you have in your project. If you do not have log4j on the classpath, commons-logging default to java.util.logging. In this case you need to configure java.util.logging.FileHander. You can do it thru logging.properties file or programmatically
Logger.getGlobal().addHandler(new FileHandler("log"));
LogFactory.getLog(MyClass.class).info("/path/to/mylog.log");
Upvotes: 4
Reputation: 1065
Commons-Logging is just a logging facade, you should use Log4j or JDK-Logging do the real logging tasks, which you can set the log file.
http://logging.apache.org/log4j/1.2/ or http://logging.apache.org/log4j/2.x/
it's configuration file will like:
log4j.rootLogger=info, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d{yy-MM-dd HH:mm:ss} %c:%L - %m%n
log4j.appender.R.File=/path/to/mylog.log
Upvotes: 2