简体   繁体   中英

apache log4j implementation through log4j.xml, log4j.dtd

我已经通过log4j.properties实现了log4j,但是我已经阅读了使用log4j xml的一些好处,所以现在我正在尝试但没有得到它...

I always use the xml. To me it's easier to read and work with and also there are several log4j features that can only be implemented with XML. As JoseK said, if you have more specific questions about what you've tried, let us know.

For some basics, the properties file will be read automatically if it's in a file named log4j.xml located anywhere in the classpath. The easiest way to accomplish this is to put in the root src direcory. In our project, I make a logging folder under the resources directory and add that to the classpath, this allows me to also put the commons-logging.properties file there.


There's no shortage of descriptions online of the structure of log4j xml files. One random google example is here . You can find plenty more on the web.

Hopefully, to help you more, I'll provide my exact logging file from an active project. As well as the accompanying commons-logging.properties file:

  <!-- ================ APPENDERS ================ --> <appender name="ProjectAppender" class="org.apache.log4j.FileAppender"> <param name="File" value="/blazeds/tomcat/logs/all.out" /> <param name="Append" value="true" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%m%n" /> </layout> </appender> <appender name="AspectAppender" class="org.apache.log4j.FileAppender"> <param name="File" value="/blazeds/tomcat/logs/aspects.out" /> <param name="Append" value="true" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%d{ABSOLUTE}]\\t\\t%m%n" /> </layout> </appender> <appender name="DebugFileAppender" class="org.apache.log4j.FileAppender"> <param name="File" value="/blazeds/tomcat/logs/debug.out" /> <param name="Append" value="true" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%d{ABSOLUTE}] %-5p\\t%m%n" /> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="DEBUG" /> <param name="LevelMax" value="DEBUG" /> <param name="AcceptOnMatch" value="true" /> </filter> <filter class="org.apache.log4j.varia.DenyAllFilter" /> </appender> <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.SimpleLayout" /> </appender> <appender name="ErrorConsoleAppender" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%d{ABSOLUTE}] %-5p > %m%n" /> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="ERROR" /> <param name="LevelMax" value="ERROR" /> <param name="AcceptOnMatch" value="true" /> </filter> <filter class="org.apache.log4j.varia.DenyAllFilter" /> </appender> <!-- ========= LOGGERS - additive ==== --> <logger name="com.companyname.projectname" additivity="true"> <level value="ALL" /> <appender-ref ref="ProjectAppender" /> <appender-ref ref="DebugFileAppender" /> </logger> <logger name="com.companyname.projectname.aspects" additivity="true"> <level value="ALL" /> <appender-ref ref="AspectAppender" /> </logger> <root> <priority value="ALL" /> <appender-ref ref="ErrorConsoleAppender" /> </root> 

We're not logging heavily here so it's a basic enough example to give you the idea. One cool feature of this logging setup is any call to log.error(string) automatically bubbles all the way up to the console.

Place these two files in your src directory, modify the output file paths, and you should see logging show up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM