java common log_java – 使用org.apache.commons.logging编写日志文件

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

commons-logging.properties放入应用程序的类路径.该文件的内容应如下所示:

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

您还可以使用Log4j logger除了Jdk14Logger.And需要第二个自定义属性文件.例如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

这是样本测试类

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的示例包结构;

并添加TestLog类的编辑配置在VM参数下这样;

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值