Log file not getting generated with log4j2 - log4j2

I migrated from log4j 1.x to log4j2.
I removed log4j.properties file and created log4j2.xml file.
After migration I ran the application but not able to find the .log file.
Can anyone suggest me the mistakes

I would suggest you add status="debug" to your configuration element in the log4j2.xml like:
<Configuration status="debug">
This will log log4j configuring itself. If you see no output then it means Log4j 2 isn't finding your configuration or Log4j isn't being used as the logging framework.

Related

Log4j2 some logs are displayed on console

Recently I have upgraded my project from Log4j1 to Log4j2. I have included below dependencies jars in my project. Some of the logs are going to console. In case of Log4j1 these logs are going to the rolling file. I am not sure what is the reason. Can you please help?
log4j-api-2.13.0.jar
log4j-core-2.13.0.jar
It sounds like you have left the log4j 1.x jar in your project. You should remove it, along with the log4 1.x configuration, and replace it with log4j-1.2-api-2.13.0.jar.

How to set log4j.property to .jar location

I'm Setting up Log4j2 in a Spring-boot application. I now want to create a /log directory exactly where the .jar file is located.
This is needed as we start the java application from a startup script and the configuration should work on both windows and unix developer machines as well as a server.
I already tried with:
<RollingFile name="FileAppender" fileName="./logs/mylog.log"
filePattern="logs/mylog-%d{yyyy-MM-dd}-%i.log">
which just creates a log folder at the directory where the jar gets started.
then I read i should use .\log/mylog.log as .\ points to the directory of the jar file.
But then it just creates a folder called .\log.
I also tried with configuration with jvm arguments and calling them at the log4j2.xml with: ${logFile}. Now a directory gets created called '${logFile}.
The only ${} command working is the directory of the log4j configuration file. But as this is inside the jar it just gets me a pretty useless folder structure
Thanks in Advance
EDIT: In the End what I did was setting up two configuration files, log4j2.xml and log4j2-prod.xml
The log4j2.xml took the system property as Vikas Sachdeva mentioned, while the prod.xml got the location hard coded.
Not really the solution I was looking for but made it work.
One solution is to pass log directory location through system properties.
Configuration file will look like -
<RollingFile name="FileAppender" fileName="${sys:basePath}/mylog.log"
filePattern="${sys:basePath}/mylog-%d{yyyy-MM-dd}-%i.log">
Now, pass VM argument basePath with absolute path of directory containing JAR file -
java -jar myapp.jar -DbasePath=/home/ubuntu/app

Websphere Liberty: How to specify log4j2 configuration location?

I'm trying to tell Websphere Liberty where is located my log4j2.xmlfile, but it isn't working.
In my file jvm.options I configure:
-Dlog4j.configurationFile=file:///${server.config.dir}/log4j2.xml
but it looks like Liberty does not understand the variable ${server.config.dir} in the jvm.options file. The file is in the same directory of the server.xml file.
How would I specify the log4j2.xml location for Liberty?
I think it should be possible by creating a Library entry in your server.xml such as:
<library id="log4jLib">
<folder dir="/opt/log4j2/config"/>
</library>
where the directory specified contains the log4j2 properties or xml file.
Then specify a classloader for your application like this:
<application id="test" name="test" type="ear" location="test.ear">
<classloader commonLibraryRef="log4jLib" />
</application>
The ${server.config.dir} variable is one of the Liberty built-in server config variables, these only apply within the server.xml (and included configurations).
When you run a Liberty server, the user.dir gets set to the same thing as ${server.config.dir}, so you could just specify the relative path to your log4j2.xml file in jvm.options as:
-Dlog4j.configurationFile=log4j2.xml
For Liberty I do the following;
I have a jvm.options file containing:
-Dlog4j.configurationFile=log4j2.xml
I place both files (log4j2.xml and jvm.options) in the server config.
I.e where tour server.xml etc is placed:
usr/servers/<myserver>:
server.xml
jvm.options
log4j2.xml
That does the trick for me.
There are a few ways of configuring log4j in WebSphere. Aside from the library entries method mentioned by pseudonym, you can simply drop the log4j2 configuration file to the global library directory at
wlp/usr/servers//lib/global
If there are files present in above location at the time an application is started, and that application does not have a classloader element configured, the application uses these libraries. If a class loader configuration is present, these libraries are not used unless the global library is explicitly referenced.
You can find more details about the global libraries in this link
WebSphere Liberty Shared Libraries

how to set abc.properties as log4j2 configuartion file in classpath

I am migrating from log4j1 to log4j2 and i have a filename as abc.properties which i need to set as log4j configuration file in classpath. I tried with
-Dlog4j.configurationFile=abc.properties,but no use?Can anyone help with this?
If the file is on classpath, you can set the location in code -
org.apache.logging.log4j.core.config.Configurator.initialize(null, "abc.properties");
Set file path using above statement before using Logger.

Log4j2 not loading log4j2.xml

I have a problem concerning log4j2 which does not load the log4j2.xml configuration file in a project.
The project is bundled into an uber jar file. When running the application using java -jar jarfile.jar the application starts but log4j prints the following error to the console:
ERROR StatusLogger No log4j2 configuration file found. Using default
configuration: logging only errors to the console.
I checked the jar and it definitely contains a log4j2.xml file in the root location.
Because I could not figure out why this does not work I debugged to log4j2 bootstrap code. I found out that log4j never tries to read the log4j2.xml. This should happen in org.apache.logging.log4j.core.config.ConfigurationFactory.Factory#getConfiguration.
Unfortunately the list of factories used in this method is empty thus the method always returns null.
Any ideas on this?
If you want to check this clone https://github.com/cryptomator/cryptomator, cd to the main directory in the cloned repo and run mvn clean install -DskipTests -P uber-jar afterwards you will find the jar file in question under main/uber-jar/target.
I suspect this is the same issue as Log4j2 configuration not found when running standalone application builded by shade plugin since it sounds like you are building an uber jar.
I have a similar issue, but not using shade.
I have a jar file, with dependencies in /lib
I have a log4j2.xml file in the same location as the main jar file.
I can (obviously) run the jar file by calling:
java -Dlog4j2.configurationFile=.\log4j2.xml -jar myjar.jar
However, in Windows, it's possible simply to double-click the jar file to load it. Everything loads and works, except that it doesn't find the log4j2.xml file - so no logfile is written.
What I would like to be able to do is have a simple jar file I can hand to someone and have it run on their machine, with the ability to configure logging in the event they run into issues.
EDIT:
To do that, you need to amend your code thus:
public class MyClass
{
static
{
System.setProperty("log4j.configurationFile", "log4j2.xml");
}
private final static Logger LOG = LogManager.getLogger();
//OTHER STUFF HERE
}
Thanks to Load Log4j2 configuration file programmatically for the answer.

Resources