What does “status” mean in Log4j2 configuration? - java

I just have finished adjustment of log4j2.xml configuration file and spotted something I don't really understand. So what is <Configuration status="SOME_STATUS_HERE">?
Almost in all examples here :
http://logging.apache.org/log4j/2.x/manual/configuration.html
folks from Apache added status to configuration.
For example here is the first one:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN"> <!--status="WARN" - what is this???-->
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

The status logger is used internally by log4j2 components. Setting status="debug" (or "trace") in the configuration will cause this internal logging to be output to the command line.
It will print debug information about which log4j2 plugin components are loaded (all configuration elements map to log4j2 plugins), and more details like for example what appenders and loggers were found, what parameters they have and how they are combined.
This is useful for troubleshooting configuration issues.
From Log4j 2.9, you can use system property log4j2.debug (no value required) to turn on internal Log4j2 status logging even before the configuration file is loaded. Prior to version 2.9, the same can be achieved with system property -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACE.

Related

Sentry log4j2 appender cannot add environment

I am using the Sentry log4j appender(version: 5.7.1) to send logged exceptions to Sentry. bellow is the log4j2.xml configuration.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" packages="org.apache.logging.log4j.core,io.sentry.log4j2">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<Sentry name="Sentry"
dsn="https://dsn" />
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Sentry"/>
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Now I need to add a specific environment in order to differentiate records based on the QA and Production environment. when I add environment="qa", it gives following error.
ERROR Sentry contains an invalid element or attribute "environment"
In Log4j2 integration, properties other than "dsn" can be configured either using:
environment variable: "SENTRY_ENVIRONMENT=qa"
system property "sentry.environment=qa"
sentry.properties file with content: environment=qa
Read more in docs: https://docs.sentry.io/platforms/java/configuration/

log4j2.xml config file is not being loaded

I have upgraded my log4j from 1.2.17 to 2.16.0. Therefore I had to rewrite my log4j.xml as well as to rename it to log4j2.xml. I have made the necessary changes. However it seems that my new config is not loaded. I have tried to configure log4j through both web.xml :
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>classpath:log4j2.xml</param-value>
</context-param>
and -Dlog4j2.configurationFile. Both approaches failed and I am not getting any logs in mylog.log file. However when running the application locally the Console display the logs.
This is my log4j2.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="trace">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%-5p %c{1} - %m%n"/>
</Console>
<RollingFile name="DAILY_LOG" fileName="${catalina.base}/logs/mylog.log"
filePattern="${catalina.base}/logs/mylog.log.%d{yyyy-MM-dd}">
<PatternLayout pattern="%d %-5p [%c] [%X{sid}:%X{uid}] %m%n "/>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- Limit the org.springframework category to INFO as its DEBUG is verbose -->
<Logger name="org.springframework" level="INFO"/>
<!-- Limit the org.hibernate category to INFO as its DEBUG is verbose -->
<Logger name="org.hibernate" level="INFO"/>
<!-- Limit the org.apache category to INFO as its DEBUG is verbose -->
<Logger name="org.apache" level="INFO"/>
<Logger name="org.myproject" level="INFO"/>
<!-- Root Logger -->
<Root level="INFO">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="DAILY_LOG"/>
</Root>
</Loggers>
</Configuration>
I have tried to debug log4j configuration by using : -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=trace. I have noticed that the LoggerContext instance is well created and all listed loggers are as well built.
I wonder if my config is wrongly written. Can someone please confirm or deny this to me.
I found the solution. This took me a while to figure out. The problem was due to the new syntax used by log4j 2.x to resolve system properties. Unlike log4j 1.x which used ${some.property} syntax for property substitution , log4j 2.x uses ${sys:some.property}. I had to add that sys prefix to make it work.
Source : https://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution

How to set log4j config to project's dependencies?

I am using log4j2 in my project with -Dlog4j.configurationFile=src/main/resources/log4j2.xml.
My application loggers write logs in the format specified by the configuration file.
I have other dependencies (sparkjava, hazelcast, mongodb, sql2o etc..) that also write logs, but in different format. I would like those loggers to use my log4j configuration file.
How can I do it?
EDIT:
My log4j2.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
As I understand it, every log message should be relate to the root logger, and thus be formatted according to my log4j2.xml configuration file.
Add the log4j 2 jars and the adapters for other logging APIs:
log4j-api (always)
log4j-core (always)
log4j-1.2-api
log4j-jcl
log4j-slf4j-impl (do NOT add log4j-to-slf4j)
Remove other logging implementations:
log4j-1.2.x
logback-core-*
logback-classic-*
Keep the facades:
commons-logging-*
slf4j-api-*

print format in apache commons logging

Using log4j underneath.
How can I do, format or wildcard stuff like:
System.out.printf('%s', 5)
With
log.info('%s', 5) //org.apache.commons.logging.Log instance
Version:commons-logging-1.1.3.jar.
The bad thingy about this mentioned API is:
It has methods info(object) and info(object, throwable) only.
Or logging did not evolve there??
This is probably the easiest way to do it
log.info(String.format("%s", 5));
The feature you are looking for is included in log4j2:
http://www.apache.org/dyn/closer.cgi/logging/log4j/2.1/apache-log4j-2.1-bin.zip
You have to include at least two jar files:
log4j-api-2.1.jar
log4j-core-2.1.jar
Finally put a configuration file named "log4j2.xml" in your classpath which can look like this:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
After the configuration you should be able to achieve what you want with the following code:
Logger log = LogManager.getLogger(YOURCLASSNAME.class);
log.printf(Level.INFO,"%s",5);
Yes, it is achievable in "slf4j". Try using the below snapshots-
log.info("My name is %s %s",firstName,secondName)
or
LOG.debug("WidgetName:{}\n curWidgetVOArray=\n",curWidgetName, curWidgetVOArray);
Refer - http://javarevisited.blogspot.in/2013/08/why-use-sl4j-over-log4j-for-logging-in.html

log4j2.xml is not loading in eclipse

I'm fairly new to log4j. Trying to use log4j2.xml to configure. I added the file to the build path and it worked once, but is no longer working.
This is my log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</appenders>
<loggers>
<root level="TRACE">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
I stripped it down, just to test it to get it working. I checked debug, and the logger config is using Default.
I also tried naming the file log4j2-test.xml.
Any ideas?
Thanks
Since you uses "servlets" as the tag, I assume you are working in a web application. You need to place your log4j.xml into a location that your web application classloader can find. For example, the WEB-INF/classes directory.

Categories