Log4j2 How to read environment variables (used when configuring the log relative path)

Source: Internet
Author: User
LOG4J2 configuration of log relative paths

The path configuration for the logs in the previous project is configured with the following:

<file name= "Log" filename= "Log/test.log" append= "false" >
        <patternlayout pattern= "%d{hh:mm:ss. SSS}%-5level%class{36}%l%M-%msg%xex%n "/>
  </File>

The log document for this configuration is archived when you run the program's top-level path (for example: C: or D:), I need to configure the relative path to the logs file under Run program path. Check the online two solutions, we give the method are: http://blog.csdn.net/whoopee/article/details/851416 the same, but I use a log4j2 non-Web project, try to read the environment variable several times, Incorrect strings or assignments are all configured incorrectly. Finally, there is no way, only to view official documents, the following is the official document of the Variable value table:

Discover that LOG4J2 does have its own rules for reading variables, a brief introduction to several commonly used date--can be inserted into the current date/time in a specific format env--can read the system's environment variable Jvmrunargs--can read the JVM when the runtime parameters set at startup, But the parameters of the main function cannot be read, and the SYS is not used on the Android system-the properties of the system properties are read, basically these parameters are sufficient.

Now let's talk about the configuration workaround for resolving log relative paths, set myapp_home =%~dp0 (set the current directory as the root) in the Option.bat batch file of the startup options, and then reference the variable in the Log4j2.xml file:

  <file name= "Log" filename= "${env:myapp_home}/log/test.log" append= "false" >
        <patternlayout pattern= "%d {HH:mm:ss. SSS}%-5level%class{36}%l%M-%msg%xex%n "/>
  </File>


OK to solve.

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.