How to lookup property parameter in web app for log4j2? - java

I want to configure log4j2 to lookup the logging path dynamically from web startup (tomcat).
From the docs (http://logging.apache.org/log4j/2.x/manual/lookups.html) there is a web: lookup with different parameter possibilities.
At first I'm trying the provided example:
<Appenders>
<File name="ApplicationLog" fileName="${web:rootDir}/app.log"/>
</Appenders>
Result: ERROR Unable to create file ${web:rootDir}/app.log java.io.IOException
I also tried the other buildin properties like servletContextName and contextPath with the same error message.
So I'm probably still missing something important. But what?

Add log4j-web JAR
Your usual log4j-core JAR file on its own is not enough.
There is an additional component called log4j-web.
For automatic replacement of the ${web:...} placeholders in your logging configuration, you need the additional log4j-web JAR file. If you don't have this log4j-web JAR file, then Log4j2 itself will still work, but it will not replace these placeholders with anything. So for example ${web:rootDir} will just end up as the literal text ${web:rootDir}.
Perhaps you are missing that dependency in your project?
Log4j-web on Maven Central

Is ${web:rootDir} a value? The placeholder is not replaced by its real value.
In the page you can red:
Information on how to use Lookups in configuration files can be found in the Property Substitution: http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution

Related

Not able to print logs to Tomcat's CATALINA_BASE by using log4j2

I have update our code from log4j to log4j 2.17.1 And I want to stored the log file to servers under the Apache tomcat. I am using the log4j2.properties mentioned below.
When I run the code, then the logs file is printed in under code structure(see in below attached screenshot)but I want to print the logs file in QA-Servers under apache tomcat.
Please help me to solve the issue.
TL;DR: use ${sys:catalina.base}.
The property substitution in Log4j 2.x differs from Log4j 1.x (cf. documentation). The most prominent change is that:
in Log4j 1.x ${catalina.base} is looked up in Java system properties and, if the system property does not exist, in the configuration file,
in Log4j 2.x ${catalina.base} is looked up only in the configuration file.
In both cases if the property can not be resolved the placeholder is left unchanged.
In Log4j 2.x all external property lookups must be prefixed using an appropriate prefix. The exact equivalent of Log4j 1.x behavior is ${sys:catalina.base}. Therefore you can use:
# Fallback
property.catalina.base=.
appender.rolling.fileName=${sys:catalina.base}/logs/aseq_wiptmobile_qa-1.applog

How to use multiple configuration files for Log4j2?

I am writing Java code that tests a Java library. The library includes its own Log4j2 configuration as part of the distribution.
I would like to use Log4j2 in my test code without modifying the library's configuration.
Is there a way to have a separate Log4j2 configuration for my test code?
This is all running as command-line Java, no servers or web involvement at all.
EDIT
What I want is to be able to configure loggers, appenders, etc for the test code to use, and at the same time have the library code use its own separate configuration file for its logging.
The idea is to use Log4j2 in my test code, but without having to change the library's configuration file. Since the library configuration file is part of the library's distribution, I don't want to change it for testing.
This may be helpful:
Log4j2 will first look for log4j2-test.xml in the classpath
if that file is not found, it will look for log4j2.xml in the classpath
So one option is to copy the library's configuration (log4j2.xml) to log4j2-test.xml and add your own configuration to log4j2-test.xml.
Furthermore, Log4j2 supports XInclude in XML configuration, so you could use that feature to avoid duplicating the library's configuration in your log4j2-test.xml.
Log4j2 supports "Composite Configuration" which exactly matches your requirement. All you need to do is provide path to multiple files in log4j.configurationFile property. This can be passed from command line or added to log4j2.component.properties file in your application.
References
https://logging.apache.org/log4j/2.x/manual/configuration.html#CompositeConfiguration
https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties
There are two step you can try to solve for your issue
Create your own configuration file with your custom name(eg: xyz.properties/.xml)
You must add the following line to your java runtime command
cmd> java -Dlog4j.configuration=location/xyz.properties
If you use diffent name for configuration rather log4j.properties/.xml file you need to configure that file at runtime by above command for more info have a look here..
Correct format for using an alternate XML file to log4j2.xml:
java -Dlog4j.configurationFile=./location/log4j2-custom.xml
Assuming ./location/log4j2-custom.xml exists and is the new XML to replace log4j2.xml in this run
See:
https://github.com/kamalcph/Log4j2Examples/blob/master/src/main/java/in/co/nmsworks/log4j2/examples/CompositeConfigurationExample.java
Referencing https://logging.apache.org/log4j/2.x/manual/configuration.html states that you can add multiple comma separated files under log4j2.configurationFile property.
To use multiple configuration files, depending on the environment you must set the.
for example:
if (env.equals("DEV")) {
setConfigFile("log4j2-dev.xml");
}
public static void setConfigFile(String logConfigFile) {
File file = new File(logConfigFile);
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
context.setConfigLocation(file.toURI());
}
first configure application.yaml file
spring:
profiles:
active: dev
---
spring:
message: running in the dev profile //just to output the message in the log
profiles: dev
logging.config: classpath:log4j2-dev.xml
---
spring:
profiles: prod
logging.config: classpath:log4j2-prod.xml
and create these similar files in your classpath
* log4j2-dev.xml
* log4j2-prod.xml

How to disable jackrabbit.log_IS_UNDEFINED in jackrabbit-standalone-2.4.2.jar

I have created a code using jackrabbit-standalone-2.4.2.jar and i am getting jackrabbit.log_IS_UNDEFINED.log file in the project directory. This log file is very huge(close to 1 GB). I want to disable it but i dont know how. Does anybody have any idea to deal with situation?
To remove this file you have to (as stated in the file name) to define the logger that you want to use.
Since jackrabbit is relying on slf4j, you should configure properly a logger for it.
For instance you can add to you dependencies the lf4j-log4j12 implementation, plus a log4j.properties in your classpath.
You can also configure any other slf4j implementation.
I have solved this issue by removing logback.xml and slf4j.jar from jackrabbit-standalone.jar and adding slf4j.jar and slf4j-lo4j.jar in the classpath of the project. Now jackrabbit.log_IS_UNDEFINED.log is gone and i am getting desired logs :)

Output classpath content from within Spring context configuration

Does Spring provide any way to output the actual content of the classpath environment variable when it is loading a resource in a context configuration file?
<!-- Import the special context -->
<import resource="classpath:mySpecialApplicationContext.xml"/>
I set the Log4J logging level to ALL for Springframework classes but this value does not appear to be logged by the framework. I am trying to figure out if Spring is loading this from a dependency, and I want to see the classpath setting during application runtime. The application is built by Maven with many dependencies.
If there are two or more mySpecialApplicationContext.xml's in the classpath, which one does Spring use?
Thank you.
You can see the relevant (I think) source code at http://goo.gl/9dK2c
In short:
No, the classpath is not logged
The details of what would be loaded when there is more than one matching resource in the classpath are ClassLoader dependent - the DefaultResourceLoader uses the thread's classloader, but typically, the first one found would be used.

Log4j configuration via JVM argument(s)?

What variables do I have to set/pass as arguments to the JVM to get Log4j to run properly? And by properly I mean not complain and print to the console. Can I see a typical example?
Note: I need to avoid creating a log4j.properties file in the application.
Do you have a log4j configuration file ? Just reference it using
-Dlog4j.configuration={path to file}
where {path to file} should be prefixed with file:
Edit: If you are working with log4j2, you need to use
-Dlog4j.configurationFile={path to file}
Taken from answer https://stackoverflow.com/a/34001970/552525
The solution is using of the following JVM argument:
-Dlog4j.configuration={path to file}
If the file is NOT in the classpath (in WEB-INF/classes in case of Tomcat) but somewhere on you disk, use file:, like
-Dlog4j.configuration=file:C:\Users\me\log4j.xml
More information and examples here: http://logging.apache.org/log4j/1.2/manual.html
This configuration seems to have changed in Log4j 2:
-Dlog4j.configurationFile=file:C:\Users\me\log4j.xml
See: https://logging.apache.org/log4j/2.x/manual/configuration.html
I know this is already answered, but because you said, this isn't exactly what you are looking for, I would like to point out the following alternative:
You can also use a configuration class instead of the properties or xml file.
-Dlog4j.configuratorClass=com.foo.BarConfigurator
See http://logging.apache.org/log4j/1.2/manual.html for details.
Late to the party as since 2015, Log4J 1.x has reached EOL.
Log4J 2.x onwards the JVM option should be -Dlog4j.configurationFile=<filename>
P.S. <filename> could be a file relative to the class path without the file: as suggested in the other answers.
Generally, as long as your log4j.properties file is on the classpath, Log4j should just automatically pick it up at JVM startup.
Relative Path is also ok:
java -Dlog4j.configuration=file:".\log4j.properties" -jar com.your-1.0-SNAPSHOT.jar
or
java -Dlog4j.configuration=file:".\log4j.xml" -jar com.your-1.0-SNAPSHOT.jar
If you are using gradle. You can apply 'aplication' plugin and use the following command
applicationDefaultJvmArgs = [
"-Dlog4j.configurationFile=your.xml",
]
According to the page (Log4j2 - Automatic Configuration) Log4j2 needs:
-Dlog4j2.configurationFile=<path to file>
Log4j will inspect the "log4j2.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension. Note that this is not restricted to a location on the local file system and may contain a URL.
Taken from: Log4j2 - Automatic Configuration
I used this two commands below with weblogic 12 and it works for log4j2.xml
-Dlog4j.configurationFile=<path to file>
-Dlog4j2.configurationFile=<path to file>

Categories