How to Use log files for JAVA projects and java project log files

Source: Internet
Author: User

How to Use log files for JAVA projects and java project log files

Recently I made a java Desktop program, which is drawn using SWT/jface on the interface. During this process, I want to use log4j to record the log information of the program running, including the error log.

The following describes how to use log4j and the configuration method.


1. configuration method:

Import jar packages, slf4j-log-4j12.jar, slf4j-api-1.7.2.jar, log4j-1.2.17.jar

Copy the log4j. xml configuration file of log4j to the src directory of the project.

The content of log4j. xml is as follows: $ {helloHome} is the path of the Project configured for the virtual machine.

<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE log4j: configuration SYSTEM "log4j. dtd">
<Log4j: configuration xmlns: log4j = "http://jakarta.apache.org/log4j/">

<Appender name = "debug-out" class = "org. apache. log4j. RollingFileAppender">
<Param name = "File" value = "$ {helloHome}/logs/debug. log"/>
<Param name = "MaxFileSize" value = "1024KB"/>
<Param name = "MaxBackupIndex" value = "5"/>
<Layout class = "org. apache. log4j. PatternLayout">
<Param name = "ConversionPattern" value = "% d {yyyy. MM. dd HH: mm: ss} % c-% m % n"/>
</Layout>
<Filter class = "org. apache. log4j. varia. LevelRangeFilter">
<Param name = "LevelMax" value = "debug"/>
<Param name = "AcceptOnMatch" value = "true"/>
</Filter>
</Appender>

<Appender name = "info-out" class = "org. apache. log4j. RollingFileAppender">
<Param name = "File" value = "$ {helloHome}/info. log"/>
<Param name = "MaxFileSize" value = "1024KB"/>
<Param name = "MaxBackupIndex" value = "5"/>
<Layout class = "org. apache. log4j. PatternLayout">
<Param name = "ConversionPattern" value = "% d {yyyy. MM. dd HH: mm: ss} % c-% m % n"/>
</Layout>
<Filter class = "org. apache. log4j. varia. LevelRangeFilter">
<Param name = "LevelMax" value = "info"/>
<Param name = "LevelMin" value = "info"/>
<Param name = "AcceptOnMatch" value = "true"/>
</Filter>
</Appender>

<Appender name = "warn-out" class = "org. apache. log4j. RollingFileAppender">
<Param name = "File" value = "$ {helloHome}/warn. log"/>
<Param name = "MaxFileSize" value = "1024KB"/>
<Param name = "MaxBackupIndex" value = "5"/>
<Layout class = "org. apache. log4j. PatternLayout">
<Param name = "ConversionPattern" value = "% d {yyyy. MM. dd HH: mm: ss} % c-% m % n"/>
</Layout>
<Filter class = "org. apache. log4j. varia. LevelRangeFilter">
<Param name = "LevelMax" value = "warn"/>
<Param name = "LevelMin" value = "warn"/>
<Param name = "AcceptOnMatch" value = "true"/>
</Filter>
</Appender>

<Appender name = "error-out" class = "org. apache. log4j. RollingFileAppender">
<Param name = "File" value = "$ {helloHome}/logs/error. log"/>
<Param name = "MaxFileSize" value = "1024KB"/>
<Param name = "MaxBackupIndex" value = "5"/>
<Layout class = "org. apache. log4j. PatternLayout">
<Param name = "ConversionPattern" value = "% d {yyyy. MM. dd HH: mm: ss} % c-% m % n"/>
</Layout>
<Filter class = "org. apache. log4j. varia. LevelRangeFilter">
<Param name = "LevelMin" value = "error"/>
<Param name = "AcceptOnMatch" value = "true"/>
</Filter>
</Appender>


<! -- OF-506: Jetty INFO messages are generally not useful. Ignore them by default. -->
<Logger name = "org. eclipse. jetty">
<Level value = "warn"/>
</Logger>

<Root>
<Level value = "info"/>
<Appender-ref = "debug-out"/>
<Appender-ref = "info-out"/>
<Appender-ref = "warn-out"/>
<Appender-ref = "error-out"/>
</Root>

</Log4j: configuration>


The configuration is complete.


2. Usage

Public class HelloiServer {
Private static final Logger Log = LoggerFactory. getLogger (HelloiServer. class );

Public void aa (){
Log. error ("erroreeeeeeeeeeeeeeee ");
}

}

In this way, the log will be recorded in the "$ {helloHome}/logs/error. log" path configured in the above log4j. xml file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.