Production Settings File for Log4J

How to make Log4j2 configurable by environment using Spring Boot 1.3.6.RELEASE

The properties lookup element allows to refer properties from an external properties file in the log4j configuration.
For your example it should be something like this:

  1. A file env.properties contains the following properties:

    log.file.path=/opt/tomcat/logs
    log.file.name=dummydummy
    log.file.size=100 MB
    log.level=DEBUG

The properties lookup should be defined as properties of the log4j2.xml:

<Configuration>  
<Properties>
<property name="log.file.path">${bundle:env:log.file.path}</property>
<property name="log.file.name">${bundle:env:log.file.name}</property>
<property name="log.file.size">${bundle:env:log.file.size}</property>
<property name="log.level">${bundle:env:log.level}</property>
</Properties>

Now the properties may be referred in appenders with ${property_name} notation. Each property reference will be interpolated with the real value from the env.properties.

You can find another example of properties lookup here.

Logging for application in different environment Log4j

Firstly you'll need a different copy of your log4j.xml for each environment.

Lets call it log4j-dev.xml, log4j-test.xml, log4j-stage.xml and log4j-prod.xml each having their own settings like log file name and log levels.

You then pass in the corresponding file at the the server startup as a system property like below -

-Dlog4j.configuration=log4j-dev.xml

This URL has the example on how to pass this for Tomcat. The concept is the same for whichever server you are deploying on.

How to give environmental variable path for file appender in configuration file in log4j

When parsing its configuration file, the expression ${MY_HOME} will be expanded to the value of the system property named MY_HOME, not the system environment variable. There's a difference between the two.

To achieve this in a clean way, you'll have to add something like this to the JVM invocation line:

-DMY_HOME=$MY_HOME

That would define the Java system property MY_HOME to contain the value of the environment variable MY_HOME.

Change location of log4j.properties

Yes, define log4j.configuration property

java -Dlog4j.configuration=file:/path/to/log4j.properties myApp

Note, that property value must be a URL.

For more read section 'Default Initialization Procedure' in Log4j manual.

No log4j2 configuration file found. Using default configuration: logging only errors to the console

I have been dealing with this problem for a while. I have changed everything as described in this post and even thought error occured. In that case make sure that you clean the project when changing settings in .xml or .properties file.
In eclipse environment. Choose Project -> Clean

What does threshold mean in Log4J?

You have two things here : a logger, and an appender. Unfortunately, you chose the same name for both, which doesn't make it very clear.

The logger's minimum level is set to warn, which means everything you log with this logger which doesn't have at least the warn level will be ignored.

Once a message is accepted by the logger, it's sent to one or several appenders (to a file, to the console, to a mail server, etc.). Each of these appenders may define a threshold. You could for example limit the messages in the console to errors, but accept warn messages in the log file.



Related Topics



Leave a reply



Submit