Java project packaged into executable jar with log4j writes the log in the directory where the JAR resides

Source: Internet
Author: User
Tags log log log4j

Develop a demo when you want to output the log to the final packaged jar in the directory, from the online learning experiment after finishing the configuration as follows,

Log4j.properties

Log4j.rootlogger =Info,console,logfilelog4j.appender.console=Org.apache.log4j.ConsoleAppenderlog4j.appender.console.Threshold=INFOlog4j.appender.console.ImmediateFlush=trueLog4j.appender.console.Target=System.outlog4j.appender.console.layout=Org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern=%d{yyyy-mm-dd HH:MM:SS} [%p]%m%Nlog4j.appender.logFile=Org.apache.log4j.FileAppenderlog4j.appender.logFile.ImmediateFlush=trueLog4j.appender.logFile.Append=falselog4j.appender.logFile.Encoding=utf-8Log4j.appender.logFile.File=JarDemo.loglog4j.appender.logFile.layout=Org.apache.log4j.PatternLayoutlog4j.appender.logFile.layout.ConversionPattern=%d{yyyy-mm-dd HH:MM:SS} [%p]%m%n

Main.java

Private Static Logger Logger = Loggerfactory.getlogger (Main.  Class);     Static {        new File (""). GetAbsolutePath ();         = (Fileappender) Org.apache.log4j.Logger.getRootLogger (). Getappender ("LogFile");         + File.separator + "JarDemo.log");    }

When finally packaged as a jar execution, a jarDemo.log log file is produced in the same directory as the jar file, and if run with idea, the log file will be at the root of the project where the source code is located.

Java project packaged into executable jar with log4j writes the log in the directory where the JAR resides

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.