Log4j:如何配置最简单的文件日志记录? [英] Log4j: How to configure simplest possible file logging?
问题描述
我的故事:
我想做一个简单的东西,就像一个最简单的 log4j 记录器,它可以将行记录到文件中.我找到了几个具有某些功能的示例,但不是真正有效的基本、通用的示例,也没有解释每一行如何工作的示例.
I want to make a thing which is as simple as a simplest possible log4j logger that logs rows to a file. I have found several examples with some functionality, but not a basic, general one that really works, and not one with an explanation how the each row work.
问题:
谁能提供一个?
先决条件:
- 我已经知道将文件放在哪里,并且我已经配置了 log4j 并用于控制台日志记录.
- 现在我想登录一个文件,并在程序运行后从文件系统中找到该文件.
- 需要添加到现有
log4j.properties
文件的行是所需的输出.
- I already know where to put the file and I have the log4j configured and working for console logging.
- Now I want to log to a file and also find the file from file system once the program has run.
- Rows needed to be added to the existing
log4j.properties
file are the desired output.
推荐答案
我有一个通用的 log4j.xml 文件给你:
I have one generic log4j.xml file for you:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration debug="false">
<appender name="default.console" class="org.apache.log4j.ConsoleAppender">
<param name="target" value="System.out" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
<appender name="default.file" class="org.apache.log4j.FileAppender">
<param name="file" value="/log/mylogfile.log" />
<param name="append" value="false" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
<appender name="another.file" class="org.apache.log4j.FileAppender">
<param name="file" value="/log/anotherlogfile.log" />
<param name="append" value="false" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
</layout>
</appender>
<logger name="com.yourcompany.SomeClass" additivity="false">
<level value="debug" />
<appender-ref ref="another.file" />
</logger>
<root>
<priority value="info" />
<appender-ref ref="default.console" />
<appender-ref ref="default.file" />
</root>
</log4j:configuration>
有一个控制台、两个文件附加器和一个指向第二个文件附加器而不是第一个的记录器.
with one console, two file appender and one logger poiting to the second file appender instead of the first.
编辑
在一个较旧的项目中,我发现了一个简单的 log4j.properties 文件:
In one of the older projects I have found a simple log4j.properties file:
# For the general syntax of property based configuration files see
# the documentation of org.apache.log4j.PropertyConfigurator.
# The root category uses two appenders: default.out and default.file.
# The first one gathers all log output, the latter only starting with
# the priority INFO.
# The root priority is DEBUG, so that all classes can be logged unless
# defined otherwise in more specific properties.
log4j.rootLogger=DEBUG, default.out, default.file
# System.out.println appender for all classes
log4j.appender.default.out=org.apache.log4j.ConsoleAppender
log4j.appender.default.out.threshold=DEBUG
log4j.appender.default.out.layout=org.apache.log4j.PatternLayout
log4j.appender.default.out.layout.ConversionPattern=%-5p %c: %m%n
log4j.appender.default.file=org.apache.log4j.FileAppender
log4j.appender.default.file.append=true
log4j.appender.default.file.file=/log/mylogfile.log
log4j.appender.default.file.threshold=INFO
log4j.appender.default.file.layout=org.apache.log4j.PatternLayout
log4j.appender.default.file.layout.ConversionPattern=%-5p %c: %m%n
有关所有布局参数的说明,请参见此处:log4j PatternLayout 参数
For the description of all the layout arguments look here: log4j PatternLayout arguments
这篇关于Log4j:如何配置最简单的文件日志记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!