[英]How to log to a file with org.apache.commons.logging?
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 (ie /path/to/my-log.txt
) 这可能很明显,但是我一直在寻找一个使用
org.apache.commons.logging
登录到特定文件(例如/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")
}
}
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.
如果在类路径上没有log4j,则commons-logging默认为java.util.logging。 In this case you need to configure java.util.logging.FileHander.
在这种情况下,您需要配置java.util.logging.FileHander。 You can do it thru logging.properties file or programmatically
您可以通过logging.properties文件或以编程方式进行操作
Logger.getGlobal().addHandler(new FileHandler("log"));
LogFactory.getLog(MyClass.class).info("/path/to/mylog.log");
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. Commons-Logging只是一个日志外观,您应该使用Log4j或JDK-Logging进行真正的日志记录任务,您可以设置日志文件。
http://logging.apache.org/log4j/1.2/ or http://logging.apache.org/log4j/2.x/ http://logging.apache.org/log4j/1.2/或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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.