Use of log4j2.x (log4j1.x upgrade to log4j2.x)

Source: Internet
Author: User

Log4j2.x has changed a lot relative to log4j1.x, which is a bit unnecessary and impersonal for a widely used open source software.


Preparatory work

Official download page: http://logging.apache.org/log4j/2.x/download.html select Apache log4j 2 binary download, do not select the source file to download, if you are interested in studying and repackaging, It is also a must. (GZ and zip are just different packaging formats, generally Linux under a lot more than GZ, using WinRAR software regardless of which format can be done)


Start configuration

1. Unzip the package, select Log4j-api-2.1.jar and Log4j-core-2.1.jar to import project projects


2. Refer to the LOG4J2 documentation, prepare a log4j configuration file, and then name it,

Simple description

Appenders inside the main configuration output mode

Loggers main configuration log how to play

This log configuration, on the one hand in the control too output, but also hit the App.log, a file can only 6MB, full 6MB start Backup, up to 10 files.

<?xml version= "1.0" encoding= "UTF-8"? ><configuration status= "OFF" ><properties><property name= "FileName" >app.log</property><property name= "Backupfilepattern" >logs/backup-%i.log</property ></properties><appenders><console name= "Console" target= "System_out" ><patternlayout Pattern= "[%d{iso8601}]%c{1} (%m:%l):%m%n"/></console><rollingfile name= "Rollingfile" FileName= "${ FileName} "filepattern=" ${backupfilepattern} "><patternlayout><pattern>[%d{iso8601}"%C{1} (%M:%L): %m%n</pattern></patternlayout><policies><sizebasedtriggeringpolicy size= "6MB"/></ Policies><defaultrolloverstrategy max= "Ten"/></rollingfile></appenders><loggers>< Logger name= "Com.rsi.remote.deploy.tool.gui.MainUI" level= "Trace" ><appenderref ref= "Rollingfile"/></ Logger><root level= "error" ><appenderref ref= "Console"/></root></loggers></ ConfiguratiOn> 

3. Configure the environment variable, specify the location of the log configuration-dlog4j.configurationfile=file:///c:\logtext.xml (the path must have protocol file:///, otherwise it will be an error)


4. Use of the time, using Logmanager, the previous version of the logger, which is also a relatively tangled place, you say you upgrade version on the upgrade version, you also modify the call method, like before you can configure the file path in the source, and now only support environment variables, If not found it will default to Classpath below to find several files called Log4j2, there are yml,xml, etc., The biggest trouble with modifying calls is to upgrade log4j1.x to log4j2.x, and if your previous code call is called directly, it would be miserable, if you encapsulate it relatively better, so to say, advise you, if the normal design project when invoking open source projects, it is best to use their own code to encapsulate it again, so that later If you have a problem with this calling method, you can modify the file directly.

Static Logmanagerlogger = Logmanager.getlogger (TestLogger.class.getName ());


If you have any other questions, please leave me a message.

Use of log4j2.x (log4j1.x upgrade to log4j2.x)

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.