java 如何使用 org.apache.commons.logging 登录到文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15285049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to log to a file with org.apache.commons.logging?
提问by chaimp
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
)
这可能很明显,但我一直在寻找一个简单的例子,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")
}
}
回答by Evgeniy Dorofeev
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
这取决于您在项目中的日志记录实现。如果类路径上没有 log4j,则 commons-logging 默认为 java.util.logging。在这种情况下,您需要配置 java.util.logging.FileHander。您可以通过 logging.properties 文件或以编程方式完成
Logger.getGlobal().addHandler(new FileHandler("log"));
LogFactory.getLog(MyClass.class).info("/path/to/mylog.log");
回答by donnior
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