简体   繁体   English

如何使用org.apache.commons.logging登录到文件?

[英]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.

相关问题 使用org.apache.commons.logging写日志文件 - Write log file using org.apache.commons.logging 使用 Springboot org.apache.commons.logging 禁用 PDFBox 日志记录 - Disable PDFBox logging with Springboot org.apache.commons.logging 如何更改org.apache.commons.logging.Log.info(“ massage”)将写入日志文件 - How to change the org.apache.commons.logging.Log.info(“massage”) will write to log file org.apache.commons.logging.Log 无法解析 - org.apache.commons.logging.Log cannot be resolved 如何修复ClassNotFoundException:org.apache.commons.logging.LogFactory? - How to fix ClassNotFoundException: org.apache.commons.logging.LogFactory? 错误:NoClassDefFoundError:org / apache / commons / logging / LogFactory - Error: NoClassDefFoundError: org/apache/commons/logging/LogFactory NoClassDefFoundError:org/apache/commons/logging/LogFactory - NoClassDefFoundError: org/apache/commons/logging/LogFactory 春季项目部署向org.apache.commons.logging.Log抛出ClassNotFoundException - Spring project deployment throws ClassNotFoundException for org.apache.commons.logging.Log 获取 org.apache.commons.logging.LogConfigurationException - Getting org.apache.commons.logging.LogConfigurationException Weblogic11:用户指定的日志类'org.apache.commons.logging.impl.Log4JLogger'找不到或无法使用 - Weblogic11: User-specified log class 'org.apache.commons.logging.impl.Log4JLogger' cannot be found or is not useable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM