I migrated our spring boot application from the web.xml configuration file to java based configuration. In this application we use log4j2 for logging however we also have log4j because of some surrounding system which uses it, so we can not remove it.
After finishing with all the migration the logs were not working. I figured out that I have to add the following code into my application.properties file
logging.level.project.base.package=TRACE
logging.level.org.springframework=INFO
logging.level.org.hibernate=INFO
Adding all these made logs to appear, however only the property file log4j.properties is recognized and the logs which are using log4j2 have the default logging pattern.
If I remove the log4j.properties file the log4j2 still doesn't work, moreover I get the following warning message
log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment).
log4j:WARN Please initialize the log4j system properly.
I also tried to specify the config file path in application.properties in bought of the following ways
logging.config=application.properties
logging.config=classpath:application.properties
Our pom.xml dependencies are the following
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
We are not running using an embedded tomcat, so we have a config class that extends SpringBootServletInitializer. We use the 2.0.1.RELEASE Spring Boot Version.
Bellow you can find my 2 log4j config files
log4j.properties
conversionPatternShort=OLDLOGGGGER %5p %d %-30C{1}:%4L [%X{username}] - %m%n
### APPENDER
log4j.appender.crt=org.apache.log4j.ConsoleAppender
log4j.appender.crt.layout=org.apache.log4j.PatternLayout
log4j.appender.crt.layout.ConversionPattern=${conversionPatternShort}
### Define Levels per package
log4j.rootLogger=ERROR,crt
log4j.logger.some.package1=DEBUG,crt
log4j.logger.some.package2=DEBUG,crt
log4j2.properties
status=ERROR
dest=err
name=LocalConfig
### PATTERN
property.conversionPatternShort=%5p %d %-30C{1}:%4L [%X{username}] - %m%n
### APPENDER
appenders=crt
appender.crt.type=Console
appender.crt.name=crt
appender.crt.layout.type=PatternLayout
appender.crt.layout.pattern=${conversionPatternShort1}
## LOGGER
## defining some loggers, foreach logger I have defined
## (name, level, additivity, appenderRef.crt.ref)
logger.xxxxx.name=namex
logger.xxxxx.level=DEBUG
logger.xxxxx.additivity=false
logger.xxxxx.appenderRef.crt.ref=crt
logger.yyyyy.name=namey
logger.yyyyy.level=DEBUG
logger.yyyyy.additivity=false
logger.yyyyy.appenderRef.crt.ref=crt
logger.hibernateUtil.name=org.hibernate.engine.jdbc.spi.SqlExceptionHelper
logger.hibernateUtil.level=ERROR
logger.hibernateUtil.additivity=false
logger.hibernateUtil.appenderRef.crt.ref=crt
rootLogger.level=WARN
rootLogger.appenderRef.crt.ref=crt
I really can not find what change made the log4j2 not work anymore. Could you please help?
I could use some help with my log4j2 configuration. When I debug the logger, it looks like it shuts down all of the appenders, and then I never receive any application logs. Luckily I know that log4j is at least finding the properties file and giving logger initialization debug output.
Here is my log4j2.properties file:
status = debug
name = PropertiesConfig
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = info
appenders = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yy-MM-dd HH:mm:ss:SSS} %-5p %c{1}:%L - %m%n
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
appender.file.type = File
appender.file.name = application
appender.file.fileName=${filename}/propertieslogs.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
Here is the logger debug init output:
DEBUG Starting LoggerContext[name=ROOT] from configuration at null
DEBUG Starting LoggerContext[name=ROOT, org.apache.logging.log4j.core.LoggerContext#448dc39b] with configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#70ffa0b4...
DEBUG Shutdown hook enabled. Registering a new one.
DEBUG Apache Log4j Core 2.11.1 initializing configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#70ffa0b4
DEBUG Installed 1 script engine
DEBUG Oracle Nashorn version: 1.8.0_162, language: ECMAScript, threading: Not Thread Safe, compile: true, names: [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript], factory class: jdk.nashorn.api.scripting.NashornScriptEngineFactory
DEBUG PluginManager 'Core' found 118 plugins
DEBUG PluginManager 'Level' found 0 plugins
DEBUG PluginManager 'Lookup' found 14 plugins
DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
DEBUG PluginManager 'TypeConverter' found 26 plugins
DEBUG createAppenderRef(ref="STDOUT", level="null", Filter=null)
DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
DEBUG createLogger(additivity="null", level="DEBUG", includeLocation="null", ={STDOUT}, ={}, Configuration(PropertiesConfig), Filter=null)
DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
DEBUG createLoggers(={root})
DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
DEBUG PatternLayout$Builder(pattern="%d{yy-MM-dd HH:mm:ss:SSS} %-5p %c{1}:%L - %m%n", PatternSelector=null, Configuration(PropertiesConfig), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
DEBUG PluginManager 'Converter' found 44 plugins
DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
DEBUG ConsoleAppender$Builder(target="null", follow="null", direct="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%d{yy-MM-dd HH:mm:ss:SSS} %-5p %c{1}:%L - %m%n), name="STDOUT", Configuration(PropertiesConfig), Filter=null)
DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false
DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
DEBUG createAppenders(={STDOUT})
DEBUG Building Plugin[name=filter, class=org.apache.logging.log4j.core.filter.ThresholdFilter].
DEBUG createFilter(level="INFO", onMatch="null", onMismatch="null")
DEBUG Configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#70ffa0b4 initialized
DEBUG Starting configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#70ffa0b4
DEBUG Started configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#70ffa0b4 OK.
DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false-1
DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false-1, all resources released: true
DEBUG Appender DefaultConsole-1 stopped with status true
DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration#605d8cc OK
DEBUG Registering MBean org.apache.logging.log4j2:type=ROOT
DEBUG Registering MBean org.apache.logging.log4j2:type=ROOT,component=StatusLogger
DEBUG Registering MBean org.apache.logging.log4j2:type=ROOT,component=ContextSelector
DEBUG Registering MBean org.apache.logging.log4j2:type=ROOT,component=Loggers,name=
DEBUG Registering MBean org.apache.logging.log4j2:type=ROOT,component=Appenders,name=STDOUT
DEBUG org.apache.logging.log4j.core.util.SystemClock does not support precise timestamps.
DEBUG LoggerContext[name=ROOT, org.apache.logging.log4j.core.LoggerContext#448dc39b] started OK with configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#70ffa0b4.
DEBUG Log4jServletContextListener ensuring that Log4j starts up properly.
DEBUG Log4jServletContextListener ensuring that Log4j shuts down properly.
DEBUG Removing LoggerContext for [ROOT].
DEBUG Stopping LoggerContext[name=ROOT, org.apache.logging.log4j.core.LoggerContext#448dc39b]...
DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false
DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false, all resources released: true
DEBUG Appender STDOUT stopped with status true
DEBUG Stopped org.apache.logging.log4j.core.config.properties.PropertiesConfiguration#70ffa0b4 OK
DEBUG Stopped LoggerContext[name=ROOT, org.apache.logging.log4j.core.LoggerContext#448dc39b] with status true
Thanks for any help! I need it.
I'd really like to get the async appenders working at some point, but its hard to find examples via the log4j2.properties syntax for log4j2
P.S. - here is my pom.xml:
<!-- log4j is used for logging within the application -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.11.1</version>
<scope>runtime</scope>
</dependency>
<!-- slf4j is used to tie Spring to log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
I just experienced what seems to be a similar problem. In my case, I was able to receive application logs, but with what appeared to be a default log4j2 config. The logs were INFO level. None of the levels set for loggers in the log4j2 config (XML) were being applied. I saw a very similar output in the startup logs as posted by the OP, including the message about "shutting down OutputStreamManager".
Mine is a spring-boot application and I was able to resolve the problem by disabling the spring-boot LoggingSystem. I added this system variable to my startup command:
-Dorg.springframework.boot.logging.LoggingSystem=none
Once I did that, the logging worked as expected, according to the levels set in my log4j2 XML config. It appears to be spring-boot hijacking the log system, rather than a conflict between SLF4J an LOG4J2.
Note that I still see the same logging startup messages -- the OutputStreamManager is still shutting down. Speculation: this is normal behavior, possibly just shutting down the bootstrap logger that prints the log4j2 initialization process.
Versions:
log4j: 2.13.3
slf4j: 1.7.30
spring-boot: 2.2.7-RElEASE
Below is my log4j configuration
#log4j.additivity.org.apache.qpid=false
log4j.rootLogger=DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.threshold=DEBUG
log4j.appender.console.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.logger.javax.jms=DEBUG
log4j.logger.org.apache.qpid=DEBUG
log4j.logger.org.apache.qpid.amqp_1_0=DEBUG
log4j.logger.org.apache.qpid.amqp_1_0.jms=DEBUG
and then in code
String log4jConfigFile = System.getProperty("user.dir") + File.separator + "log4j.properties";
PropertyConfigurator.configure(log4jConfigFile);
logger.debug("this is a debug log message");
my debug message this is a debug log message do get printed but the log messages from org.apache.qpid are not getting printed on console
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-amqp-1-0-client-jms</artifactId>
<version>0.22</version>
</dependency>
EDIT
I am a newbie in java... The logging dependencies I have added. Do I need add some setting somewhere to redirect sl4j logs to log4j??
<slf4j-version>1.6.6</slf4j-version>
<log4j-version>1.2.17</log4j-version>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j-version}</version>
</dependency>
The (deprecated) qpid-amqp-1-0-client-jms client used java.util.logging, and not log4j. To quote from a mail I sent back in 2014 to the users#qpid.apache.org mailing list:
you can turn it on by setting the Java system property
java.util.logging.config.file to point to a file that looks something
like this:
handlers=java.util.logging.FileHandler FRM.level=ALL
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=[%1$tc] %4$s: %5$s%n
java.util.logging.FileHandler.level=ALL`
# (The output file is placed in the directory
# defined by the "user.home" System property.)
java.util.logging.FileHandler.pattern=%h/qpid-jms-%u.log`
When you run the client it should then generate a file called
qpid-jms-0.log in your home directory, with output that looks
something like:
[Mon Feb 24 18:45:58 CET 2014] FINE: RECV[/127.0.0.1:5672|0] :
SaslMechanisms{saslServerMechanisms=[ANONYMOUS]}
Note that the logging in this old client is really very minimal and ideally you should instead migrate your code to the supported Qpid JMS client for AMQP 1.0 https://qpid.apache.org/components/jms/index.html which does use slf4j, but uses different configuration syntax for connections and queues.
I am in the process of migrating from log4j 1.2 to log4j 2.
I have my whole configuration into a log4j2.properties file.
I noticed that I got a new error message in my logs while starting my tomcat :
log4j:WARN No appenders could be found for logger (org.springframework.web.filter.CharacterEncodingFilter).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I have verified with my previous logs, and I did not have this warning previously.
Here is my previous configuration (in the log4j.properties file):
log4j.rootLogger=INFO, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{dd-MM HH:mm:ss} %p %t %c - %m%n
log4j.logger.com.example=DEBUG
log4j.logger.org.springframework=INFO
Here is the new one (in log4j2.properties) :
appender.console.type = Console
appender.console.name = ConsoleAppender
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d %p %t %c - %m%n
loggers = log1, log2
logger.log1.name = org.springframework
logger.log1.level = INFO
logger.log1.appenderRef = ConsoleAppender
logger.log2.name = com.example
logger.log2.level = DEBUG
logger.log2.appenderRef = ConsoleAppender
rootLogger.level = ERROR
rootLogger.appenderRef.stdout.ref = ConsoleAppender
So, what I've understand so far is :
I create one appender, named ConsoleAppender, of type Console
I create two loggers that goes each onto ConsoleAppender, one on org.springframework (level INFO) and the other on com.example (level INFO)
I use this page to try to understand how to do the migration.
Do you see any explanation about why I should have this particular message ?
As seen in this answer, the issue may be due to the fact that I was also using slf4j-log4j12 (but in the latest version already) and it was in conflict with other log4j apis :
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
So I did what's told and I removed the org.slf4j:slf4j-log4j12 dependancy and added log4j-api & log4j-slf4j-impl dependancies :
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.8.2</version>
</dependency>
... and for know it seems to solve the error (it doesn't appear anymore into my starting logs
To be more clear, I first only added the three above dependancies without removing the slf4j-log4j12 one, and I got this as well into the logs :
SLF4J: Found binding in
[jar:file:/myuser/apache-tomcat-7.0.75/webapps/mywebapp/WEB-INF/lib/log4j-slf4j-impl-2.8.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/myuser/apache-tomcat-7.0.75/webapps/mywebapp/WEB-INF/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
After having removed the slf4j-log4j12 dependancy, the whole error logs disappeared.
i am using log4j
and my configuration is as follows:
log4j.rootLogger=OFF, stdout, rootLog
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.rootLog=org.apache.log4j.RollingFileAppender
log4j.appender.rootLog.File=${user.home}/logs/mylog.log
log4j.appender.rootLog.MaxFileSize=10000KB
log4j.appender.rootLog.MaxBackupIndex=3
log4j.appender.rootLog.layout=org.apache.log4j.PatternLayout
log4j.appender.rootLog.layout.ConversionPattern=%p %t %c - %m%n
# Control/Limit integrated frameworks logging messages
log4j.logger.org.hibernate=OFF
log4j.logger.org.springframework=OFF
although i am turning off the root logger and the hibernate logging
i am still seeing info in the console from:
2099 [main] INFO org.hibernate.cfg.SettingsFactory
2440 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport
3239 [main] WARN org.hibernate.util.JDBCExceptionReporter
how to stop that, please advise, thanks.
UPDATE: logging jars/dependencies:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
</dependency>
That's not really what your log4j config is. The output you've listed doesn't match either of the conversion patterns you showed in the config file. It's using some other configuration.
Update: Based on your updated question, if you're using an SLF4J Logger, then you aren't even using Log4J because you're using the slf4j-simple binding, which "outputs all events to System.err. Only messages of level INFO and higher are printed" per the SLF4J manual. To use Log4J, you have to use the slf4j-log4j12 binding.
You first need to find out which log4j config file it is using. You can turn on debug on log4j, as a -D parameter when you run Java, and it will tell you.