Log4j config: Use Tomcat context params

I wanted to use parameters defined in context.xml on Tomcat 8 in configuration of Log4j2. My context was defined like:

<?xml version="1.0" encoding="UTF-8"?> 
    <Parameter name="instanceName" value="testInstance" override="false"/> 
    <Resource .../> 

Now I would like to use parameter instanceName in log filename. I spent many hours trying various approaches and googling. I wonder why is this not better documented - Using Log4j 2 in Web Applications does not really help. In the end it is easy - just use web: prefix like this:

<?xml version="1.0" encoding="UTF-8"?> 
<Configuration status="debug"> 
        <Property name="log-path">${sys:catalina.base}/logs/${web:instanceName}</Property> 
        <RollingFile name="file" fileName="${log-path}/test.log" filePattern="${log-path}/test-%d{yyyy-MM-dd}.log"> ... ... 

That’s it folks!

Tags:  Log4j  Tomcat  Java 
Last blog articles:
Leaving LinkedIn
LinkedIn: Odcházení
git gui: Add pull and stash commands
Bosch Silence Plus + code E24
Using LXC virtualization for testing server installation

Java Ubuntu Gradle česky personal Software-architecture DIY children React Eclipse DisplayLink AI Spring JavaScript AspectJ neuron SSH LXC JavaFX Immutables Gnome3 GIT FatJar Edison technologies science procrastination pokusy lambda kvas ellipsis destilace chemie alkohol VisualVM Upstart Tomcat Selenium REST PrimeFaces Log4j Jooq Jackson JSF Debug DI CSS

Last tweets: