Log4j-subcontract output log

Source: Internet
Author: User

Recently, due to some inexplicable accidental data deletion in the business, we have studied the log4j packet output and want to use log4j to record which data has been deleted by mistake.

The specific method is to create a new package, then create a new class, record the deleted data in the class, and configure the output file pointing to the new package in log4j. xml.


Experiment in the test project:

Log4j. xml:

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE log4j: configuration SYSTEM "log4j. dtd">
<Log4j: configuration xmlns: log4j = "http://jakarta.apache.org/log4j/" debug = "false">
 
<Appender name = "Console" class = "org. apache. log4j. leleappender">
<Layout class = "org. apache. log4j. PatternLayout">
<Param name = "ConversionPattern" value = "% d {yyyy-MM-dd HH: mm: ss. SSS} %-5 p [% t] % c {1 }:% L-% m % n "/>
</Layout>
</Appender>

<Appender name = "File" class = "org. apache. log4j. RollingFileAppender">
<Param name = "File" value = "D:/log4j/test. log"/>
<Param name = "MaxFileSize" value = "1 MB"/>
<Param name = "MaxBackupIndex" value = "10"/>
<Param name = "Append" value = "true"/>
<Layout class = "org. apache. log4j. PatternLayout">
<Param name = "ConversionPattern" value = "% d {yyyy-MM-dd HH: mm: ss. SSS} %-5 p [% t] % c {1}-% m % n "/>
</Layout>
</Appender>

<Appender name = "File1" class = "org. apache. log4j. RollingFileAppender">
<Param name = "File" value = "D:/log4j/test1.log"/>
<Param name = "MaxFileSize" value = "1 MB"/>
<Param name = "MaxBackupIndex" value = "10"/>
<Param name = "Append" value = "true"/>
<Layout class = "org. apache. log4j. PatternLayout">
<Param name = "ConversionPattern" value = "% d {yyyy-MM-dd HH: mm: ss. SSS} %-5 p [% t] % c {1}-% m % n "/>
</Layout>
</Appender>
 
<Logger name = "org">
<Level value = "WARN"/>
</Logger>
<Logger name = "org. apache">
<Level value = "WARN"/>
</Logger>
<Logger name = "com. zznode. log">
<Level value = "DEBUG"/>
</Logger>
<Logger name = "com. zznode. test">
<Level value = "DEBUG"/>
<Appender-ref = "File1"/>
</Logger>
<Logger name = "rule">
<Level value = "INFO"/>
</Logger>
<Logger name = "org. springframework. jdbc. core">
<Level value = "info"/>
</Logger>

<Root>
<Level value = "DEBUG"/>
<Appender-ref = "Console"/>
<Appender-ref = "File"/>
</Root>
 
</Log4j: configuration>

Main method:

[Java]
Package com. zznode. log;
 
Import org. apache. log4j. Logger;
Import org. apache. log4j. PropertyConfigurator;
Import org. apache. log4j. xml. DOMConfigurator;
 
Import com. zznode. test. Test;
 
/**
* @ Desc:
* @ Since Nov 8, 2012
* @ Author chaisson
*
* <P>
*/
Public class Log4jApp {

Public void printLog (){
Logger log = Logger. getLogger (Log4jApp. class. getClass ());
Log.info ("test info ");
Log. debug ("test debug ");
Log. error ("test error ");
}

Public static void main (String [] args ){
 
DOMConfigurator. configure ("D:/workspace/Test/log4j. xml ");
// PropertyConfigurator. configure ("D:/workspace/Test/log4j. properties ");
 
Log4jApp app = new Log4jApp ();
App. printLog ();

Test test = new Test ();
Test. printLog ();
}
}

Project Structure:

Output result:


Eclipse Console:

[Plain]
14:41:43, 858 INFO Class: 27-test info
14:41:43, 860 DEBUG Class: 28-test debug
14:41:43, 860 ERROR Class: 29-Test error
14:41:43, 861 DEBUG Test: 17-Test. java debug
14:41:43, 861 INFO Test: 18-Test. java info
Test. log:

[Plain]
14:41:43. 858 INFO [main] Class-test info
14:41:43. 860 DEBUG [main] Class-test debug
14:41:43. 860 ERROR [main] Class-Test error
14:41:43. 861 DEBUG [main] Test-Test. java debug
14:41:43. 861 INFO [main] Test-Test. java info
Test1.log:
[Plain]
14:41:43. 861 DEBUG [main] Test-Test. java debug
14:41:43. 861 INFO [main] Test-Test. java info

In this way, change log4j. xml to log4j. properties. The effect is the same and the configuration is as follows:
[Plain]
### Set log levels ###
Log4j. rootLogger = debug, stdout, File
Log4j.logger.com. zznode. test = debug, File1

Log4j. appender. stdout = org. apache. log4j. leleappender
Log4j. appender. stdout. Target = System. out
Log4j. appender. stdout. layout = org. apache. log4j. PatternLayout
Log4j. appender. stdout. layout. ConversionPattern = % d {ABSOLUTE} % 5 p % c {1}: % L-% m % n

Log4j. appender. File = org. apache. log4j. DailyRollingFileAppender
Log4j. appender. File. File = D:/log4j/test. log
Log4j. appender. File. Append = true
Log4j. appender. File. Threshold = DEBUG
Log4j. appender. File. layout = org. apache. log4j. PatternLayout
Log4j. appender. file. layout. conversionPattern = % d {yyyy-MM-dd HH: mm: ss. SSS} %-5 p [% t] % c {1}-% m % n

Log4j. appender. File1 = org. apache. log4j. DailyRollingFileAppender
Log4j. appender. File1.File = D:/log4j/test1.log
Log4j. appender. File1.Append = true
Log4j. appender. File1.Threshold = DEBUG
Log4j. appender. File1.layout = org. apache. log4j. PatternLayout
Log4j. appender. file1.layout. conversionPattern = % d {yyyy-MM-dd HH: mm: ss. SSS} %-5 p [% t] % c {1}-% m % n

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.