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.
Related
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?
In a project's pom.xml I am seeing a dependency like below
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
Can someone let me know what is the difference between slf4j-log4j12 & log4j ?
Log4j 1.2
slf4j-log4j12 provides a bridge between SLF4J and Log4j 1.2 so that SLF4J knows about how to log using Log4j.
You are using Log4j 1.2. That version's binding it is maintained by the SLF4J project. Here is a summary from the SLF4J docs:
SLF4J supports various logging frameworks. The SLF4J distribution ships with several jar files referred to as "SLF4J bindings", with each binding corresponding to a supported framework.
slf4j-log4j12-1.7.28.jar
Binding for log4j version 1.2, a widely used logging framework. You also need to place log4j.jar on your class path.
Log4j 2
If you are using Log4j 2 or later, you will need a different binding JAR than slf4j-log4j12. That binding is maintained by the Log4j project. According to the Log4j docs:
The Log4j 2 SLF4J Binding allows applications coded to the SLF4J API to use Log4j 2 as the implementation.
You must provide both dependencies if you want SLF4J to route logging to Log4j. Again, from the Log4j 2 docs:
Simply include the Log4j 2 SLF4J Binding jar along with the Log4j 2 jars and SLF4J API jar to cause all SLF4J logging to be handled by Log4j 2.
To summarize:
<dependency> <!--Facade for logging systems-->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency> <!--Log4j 2 implementation for slf4j-->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.12.0</version>
</dependency>
In addition make sure that you are using a log4j2 properties file. The mistake of using 'log4j.xml' did cost me quite some time
I'll post some points regarding these logger message.
log4j:
logger.debug("This is log message:" + msg);
log4j string is concatenated every time the line is evaluated even if log level is lower than debug so the string will never be used.
slf4j:
logger.debug("this is log slf4j message",msg);
slf4j string and parameters are passed through to the logger which only substitutes them if the log message is actually to be used.
The only difference is the performance. Where log4j will take more time because of string concatenation compare to slf4j.
Slf4j:
An abstract layer for the logging component.
We can change logging at point of time without much changes in code.
Log4j:
A logging component which provides core functionalities of logging.
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.
The problem:
185 INFO [2016-07-20 08:24:50,393] ({RemoteInterpreter async opener} RemoteInterpreter.java[open]:115) - Create remote interpreter org.apache.zeppelin.spark.SparkInterpreter
186 INFO [2016-07-20 08:24:50,437] ({qtp691447552-1500} NotebookRestApi.java[getJobs]:163) - getjobs user ***#***.com, productUser mamm01
187 INFO [2016-07-20 08:25:05,765] ({qtp691447552-1483} NotebookRestA^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^# INFO [2016-07-20 08:25:50,143] ({qtp691447552-1500} NotebookRestApi.java[bind]:119) - binding user ***#***.com to settingId ***#***.com-__-mamm01-__-21
188 INFO [2016-07-20 08:25:50,146] ({RemoteInterpreter async opener} RemoteInterpreterProcess.java[reference]:103) - Run interpreter process [/home/hadoop/zeppelin/zeppelin-0.5.6-incubating/bin/interpreter.sh, -d, /home/hadoop/zeppelin/zeppelin-0.5.6-incubating/interpreter/spark, -p, 35297]
'^#' is '\0'(NUL) in the vim editor. Line 187 is incomplete. the last part is lost and overwrited with '\0'. There are many places in my log file looks like that. It's so weird.
My log4j.properties:
log4j.rootLogger = INFO, dailyfile
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%d] ({%t} %F[%M]:%L) - %m%n
log4j.appender.dailyfile.DatePattern=.yyyy-MM-dd
log4j.appender.dailyfile.Threshold = INFO
log4j.appender.dailyfile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyfile.File = ${zeppelin.log.file}
log4j.appender.dailyfile.layout = org.apache.log4j.PatternLayout
log4j.appender.dailyfile.layout.ConversionPattern=%5p [%d] ({%t} %F[%M]:%L) - %m%n
log4j.appender.dailyfile.Append=true
log4j.appender.dailyfile.ImmediateFlush=true
I tried to set Append and ImmediateFlush to true, but it doesn't help.
My pom.xml:
<slf4j.version>1.7.21</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<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>
I tried to update slf4j and log4j version, but it doesn't help.
My pattern of calling slf4j API:
public class NotebookRestApi {
private static final Logger LOG = LoggerFactory.getLogger(NotebookRestApi.class);
...
LOG.info("binding user {} to settingId {}", email, settingId(email, product, hiveSiteId));
My Application uses JAX-RS and embedded jetty, I don't know if it's related to the problem. My another suspicion is, Is it possible caused by multiple classloaders?
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.