使用org.apache.commons.logging写日志文件 [英] Write log file using org.apache.commons.logging

查看:466
本文介绍了使用org.apache.commons.logging写日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,我需要使用 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.

任何人都可以帮助我吗?

Can anyone help me?

谢谢&最好的问候。

Thanks & best regards.

推荐答案

试试这个样本,首先你需要两个这样的属性文件;

Try this sample, first you need two properties files likes this;

commons-logging.properties 放在应用程序的类路径中。此文件的内容应如下所示:

commons-logging.properties that put in your application's classpath. The contents of this file should look like:

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

除了Jdk14Logger之外,您还可以使用Log4j记录器。需要第二个自定义属性文件。示例 log-config.properties 如下所示:

You can also use Log4j logger besides Jdk14Logger.And need second custom properties file.For example log-config.properties looks like this:

    # 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

这是示例测试类

     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.");
          }
        }
     }

这是使用的示例包结构eclipse;

This is sample package structure using eclipse;

并在VM参数下添加TestLog类的编辑配置,如下所示;

And add TestLog class's Edit Configuration under VM arguments likes this;

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

然后运行,你可以在D下找到你的日志文件:/temp/log/test.log

And run then you can find your log file under D:/temp/log/test.log

这篇关于使用org.apache.commons.logging写日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆