简体   繁体   English

使用org.apache.commons.logging写日志文件

[英]Write log file using org.apache.commons.logging

I'm writing an application where I need to write log to a file using org.apache.commons.logging library, but i don't know how to start. 我正在编写一个应用程序,我需要使用org.apache.commons.logging库将日志写入文件,但我不知道如何启动。

Can anyone help me? 谁能帮我?

Thanks & best regards. 谢谢和最好的问候。

Try this sample, first you need two properties files likes this; 试试这个样本,首先你需要两个像这样的属性文件;

commons-logging.properties that put in your application's classpath. commons-logging.properties放在应用程序的类路径中。 The contents of this file should look like: 该文件的内容应如下所示:

    org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger

You can also use Log4j logger besides Jdk14Logger.And need second custom properties file.For example log-config.properties looks like this: 除了Jdk14Logger之外,您还可以使用Log4j记录器。需要第二个自定义属性文件。例如, log-config.properties如下所示:

    # The following creates two handlers
    handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler
    # Set the default logging level for the root logger
    .level=SEVERE
    # log level for the "com.example" package
    sample.logging.level=FINE
    # Set the default logging level
    java.util.logging.ConsoleHandler.level=ALL
    java.util.logging.FileHandler.level=FINE
    # Set the default formatter
    java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
    java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
    # Specify the location and name of the log file
    java.util.logging.FileHandler.pattern=D:/temp/log/test.log

This is sample test class 这是示例测试类

     public class TestLog {

     private static Log log = LogFactory.getLog(TestLog.class);
     public static void main(String[] args) {
          log.info("Testing Info Message.");
              if (log.isDebugEnabled()) {
                  log.debug("Testing Debug Message.");
          }
        }
     }

This is sample package structure using eclipse; 这是使用eclipse的示例包结构;

在此输入图像描述

And add TestLog class's Edit Configuration under VM arguments likes this; 并在VM参数下添加TestLog类的Edit Configuration就像这样;

  -Djava.util.logging.config.file=/D:/dev/workspace/LoggingTest/bin/log-config.properties(your properties file path)

在此输入图像描述

And run then you can find your log file under D:/temp/log/test.log 然后运行,您可以在D:/temp/log/test.log下找到您的日志文件

I hope this helps... this is how we have done in our project... 我希望这有帮助......这就是我们在项目中的表现......

A. Include the jar in your project. A.在项目中包含jar。

B. Define log4j.xml for loggers definition something like this... B.为loggers定义log4j.xml这样的定义......

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

    <appender name="FILE" class="org.jboss.logging.appender.RollingFileAppender">
    <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
    <param name="File" value="${jboss.server.log.dir}/server.log"/>
    <param name="Append" value="false"/>
    <param name="MaxFileSize" value="1048576KB"/>
    <param name="MaxBackupIndex" value="3"/>

 <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
 </layout>      
</appender>

<root>
   <appender-ref ref="CONSOLE"/>
   <appender-ref ref="FILE"/>
</root>

</log4j:configuration>

C. Use the logger in the class: C.在课堂上使用记录器:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Class YourClass{
    private static Log log = LogFactory.getLog(YourClass.class);

    public void yourMethod(){
        log.info("Your Message");
    }
}

EDIT: D. Since we have a JBoss AS environment so the application is configured to read log4j.xml like following (You would need an equivalent config): 编辑:D。因为我们有一个JBoss AS环境所以应用程序配置为读取log4j.xml如下(你需要一个等效的配置):

<mbean code="org.jboss.logging.Log4jService"
  name="jboss.system:type=Log4jService,service=Logging"
  xmbean-dd="resource:xmdesc/Log4jService-xmbean.xml">
  <attribute name="ConfigurationURL">resource:jboss-log4j.xml</attribute>
  <!-- Set the org.apache.log4j.helpers.LogLog.setQuiteMode. As of log4j1.2.8
  this needs to be set to avoid a possible deadlock on exception at the
  appender level. See bug#696819.
  -->
  <attribute name="Log4jQuietMode">true</attribute>
  <!-- How frequently in seconds the ConfigurationURL is checked for changes -->
  <attribute name="RefreshPeriod">60</attribute>
</mbean>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用org.apache.commons.logging登录到文件? - How to log to a file with 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 使用org.apache.commons.logging.LogFactory.getLog的最佳实践 - Best Practice of Using org.apache.commons.logging.LogFactory.getLog Wildfly 17:“找不到或无法使用用户指定的日志类&#39;org.apache.commons.logging.impl.Log4JLogger”。”使用commons-configuration2 - Wildfly 17: “User-specified log class 'org.apache.commons.logging.impl.Log4JLogger' cannot be found or is not useable.” using commons-configuration2 NoClassDefFoundError:org/apache/commons/logging/LogFactory - NoClassDefFoundError: org/apache/commons/logging/LogFactory 错误:NoClassDefFoundError:org / apache / commons / logging / LogFactory - Error: NoClassDefFoundError: org/apache/commons/logging/LogFactory 使用Commons-logging的JDK日志无法创建日志文件 - log file is not getting created using JDK logging with Commons-logging 春季项目部署向org.apache.commons.logging.Log抛出ClassNotFoundException - Spring project deployment throws ClassNotFoundException for org.apache.commons.logging.Log
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM