slf4j + log4j problem - log4j

I'm trying to configure slf4j with log4j in my java application and I get no success.
My log4j file looks like this:
log4j.rootLogger=DEBUG,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %X{file} %c{1} - %m%n
log4j.logger.lt.hltech.tools.webcrawler=DEBUG,webcrawler
log4j.additivity.lt.hltech.ws.webcrawler=false
log4j.logger.webcrawler=DEBUG,webcrawler
log4j.additivity.webcrawler=false
log4j.appender.webcrawler=org.apache.log4j.DailyRollingFileAppender
log4j.appender.webcrawler.File=D:\\Projects\\IntellijIdeaProjects\\hltech_tools\\WebCrawler\\crawling.log
log4j.appender.webcrawler.DatePattern='.'yyyy-MM-dd
log4j.appender.webcrawler.layout=org.apache.log4j.PatternLayout
log4j.appender.webcrawler.layout.ConversionPattern=%d [%t] %-5p %X{file} %c{1} %m%n
log4j.appender.webcrawler.ImmediateFlush=true
MANIFEST.MF in jar file looks like this:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.5.0_20-b02 (Sun Microsystems Inc.)
Main-Class: lt.hltech.tools.webcrawler.main.TopicalCrawler
Class-Path: ../../lib/antlr-2.7.6.jar ../../lib/asm-attrs.jar ../../li
b/asm.jar ../../lib/axis-1.4.jar ../../lib/axis-ant-1.4.jar ../../lib
/axis-spring-provider-0.1.0.jar ../../lib/cglib-2.1.3.jar ../../lib/c
heckstyle-5.4-all.jar ../../lib/checkstyle-5.4.jar ../../lib/commons-
collections-2.1.1.jar ../../lib/commons-logging-1.1.1.jar ../../lib/c
ommons-logging-adapters-1.1.1.jar ../../lib/commons-logging-api-1.1.1
.jar ../../lib/dom4j-1.6.1.jar ../../lib/ehcache-1.2.3.jar ../../lib/
hibernate-annotations.jar ../../lib/hibernate-commons-annotations.jar
../../lib/hibernate-entitymanager.jar ../../lib/hibernate-tools.jar
../../lib/hibernate3.jar ../../lib/javassist.jar ../../lib/jaxen-1.1.
1.jar ../../lib/jaxrpc-1.1-axis14.jar ../../lib/jdbc2_0-stdext.jar ..
/../lib/jericho-html-3.1.jar ../../lib/jing.jar ../../lib/jta.jar ../
../lib/junit.jar ../../lib/log4j-1.2.9.jar ../../lib/mysql-connector-
java-5.1.6-bin.jar ../../lib/ontopia-vizlet.jar ../../lib/ontopia.jar
../../lib/pmd-4.2.6.jar ../../lib/signed-vizlet.jar ../../lib/slf4j-
api-1.5.11.jar ../../lib/slf4j-log4j12-1.5.11.jar ../../lib/xercesImp
l-2.6.2.jar ../../src/main/config/log4j.properties
I have added slf4j-log4j12-1.5.11.jar, log4j-1.2.9.jar, slf4j-
api-1.5.11.jar and even log4j.properties in my classpath. But the logging doesnt work.
Any ideas how to solve it?
Thanks

Try out with slf4j-api-1.7.5 , log4j-1.2.17 and slf4j-log4j12-1.7.5 and make sure U have one binder jar (only slf4j-log4j12-1.7.5.jar) in your class path. Then add log4j.properties file into src/main/resources . Just try out with this log4j.properties example.

At the end of your classpath there's:
../../src/main/config/log4j.properties
As far as I know, classpath can only contain jars, zips or directories. Adding other files has no effect.
Try packing your properties into a jar file or include the directory that contains it.

Related

Log4j properties for an Webservice/API

In my project there are multiple Rest webservices/APIs where I need to have a separate log file for each webservice with appropriate Input request, processing steps and response. All these webservice will call the same jar.
I have tried the below configurations but the common jar processing logs are getting logged in all the api logs.The root package of the common jar is fr.com.api.common . I have even placed the addivity as false,but it is not working.
log4j.appender.api1Log=org.apache.log4j.RollingFileAppender
log4j.appender.api1Log.File=api1Log.log
log4j.appender.api1Log.Threshold=INFO
log4j.appender.api1Log.Append=true
log4j.appender.api1Log.ImmediateFlush=true
log4j.appender.api1Log.MaxFileSize=50000KB
log4j.appender.api1Log.MaxBackupIndex=10
log4j.appender.api1Log.layout=org.apache.log4j.PatternLayout
log4j.appender.api1Log.layout.ConversionPattern=%d %-5p [%c] (%t) %m%n
log4j.logger.fr.com.api.rest.api1=INFO, api1Log
log4j.additivity.fr.com.api.rest.api1=false
log4j.appender.api2Log=org.apache.log4j.RollingFileAppender
log4j.appender.api2Log.File=api2Log.log
log4j.appender.api2Log.Threshold=INFO
log4j.appender.api2Log.Append=true
log4j.appender.api2Log.ImmediateFlush=true
log4j.appender.api2Log.MaxFileSize=50000KB
log4j.appender.api2Log.MaxBackupIndex=10
log4j.appender.api2Log.layout=org.apache.log4j.PatternLayout
log4j.appender.api2Log.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} %-5p %l - %m%n
log4j.logger.fr.com.api.rest.api2=INFO, api2Log
log4j.additivity.fr.com.api.rest.api2=false
log4j.logger.fr.com.api.common=INFO,api2Log,api1Log
log4j.additivity.fr.com.api.common=false

log4j Warning : No appenders found

I have the following log4.properties :
log4j.rootLogger=WARN,console
log4j.rootCategory=debug,A1,D
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%p %d{MM/dd/yyyy HH:mm:ss} %x %c - %m%n
log4j.appender=org.apache.log4j.RollingFileAppender
log4j.appender.D.File=c:/opt/logs/MyLogs.log
log4j.appender.D.layout=org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern=%p %d{MM/dd/yyyy HH:mm:ss} %x %c - %m%n
I am linking it to my project using Java option "-Dlog4j.configuration=c:\opt\config\log4j.properties"
In my Java code I import log4j.Logger and then instantiate it :
public static final Logger logger = Logger.getLogger("testServlet.class");
However I am still getting log4j warnings and the log file is not getting generated :
log4j:WARN No appenders could be found for logger (testServlet.class).
log4j:WARN Please initialize the log4j system properly.
What am I doing wrong?
The file name should be log4j.properties. And the file should be located one of the folders in the classpath.
In Eclipse, I have also tried many options but the best solution in Eclipse is to create another 'source folder' in your project and put the log4j.properties file in the directory. As all the source folders are in the classpath, the Log4j system will locate your configuration file.

logger.IsDebugEnabled() always return false

i am new to log4j and i have the following log4j.properties file in my java application
i am working on this in websphere 6.1
log4j.properties file
log4j.rootLogger=info, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.package_name=debug
However this is working if i am using only one project as part of my application.If there are multiple projects and i want to use logging facility,logger.isDebugEnabled() always return false.. can anybody suggest a solution for this?
Thanks in advance
Websphere use a classloader for default for each EAR. If you have several Web modules or EJB modules and several files for log4j, only one is loaded by the classloader.
See A Powerful, Easy-to-Use Logging System for configure log4j with several projects in a EAR.
# Set root logger level to INFO and appender to STDOUT.
log4j.rootLogger=INFO, STDOUT
#------------------------------------STDOUT-----------------------------------#
# STDOUT is set to be a ConsoleAppender.
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
# STDOUT uses PatternLayout.
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%d %-5p (%c.java:%L).%M - %m%n
log4j.appender.STDOUT.Encoding=UTF-8
#-----------------------------------------------------------------------------#
# Specify the logging level for loggers from other libraries
log4j.logger.org.apache.commons.beanutils.BeanUtils=DEBUG
log4j.logger.org.apache.struts.action=DEBUG
log4j.logger.org.apache.struts.tiles=DEBUG
log4j.logger.org.apache.struts.util.ModuleUtils=DEBUG
log4j.logger.org.apache.struts.util.RequestUtils=DEBUG
log4j.logger.org.apache.struts.util.PropertyMessageResources=ERROR
log4j.logger.com.ibm._jsp=DEBUG
May you are missing the log4j.logger. prefix for each particular package.
See more of log4j in http://logging.apache.org/log4j/1.2/manual.html

FileAppender not writing anything in log4j

I have a small problem using log4j and FileAppender. When I am using ConsoleAppender, everything is working well but FileAppender won't ever produce a file (or write into a file). Moreover, I have no errors concerning log4j in the osgi console.
Here's my configuration :
# main link (console or file, depending on what we want)
# log4j.category.com.foo=DEBUG, console
log4j.category.com.foo=DEBUG, file
# log4j.category.com.foo=DEBUG, console, file
# console logging
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-4r %-5p %c %x - %m%n
# file logging
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=C:\\Users\\foo\\some_file.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-4r %-5p %c %x - %m%n
Note that the ConversionPattern attributes are exactly the same in the two appenders.
I have tried to change the File argument to write the log in the "run" directory but it didn't do anything.
I also tried to add log4j.appender.file.ImmediateFlush=true without success.
Have I missed something (obvious ?) somewhere ? It sound strange that log4j isn't even able to write a simple log in a file....
Thanks a lot in advance for your help.
Replace :
log4j.category.com.foo=DEBUG, console
By :
log4j.category.com.foo=DEBUG, console, file
I know this is old but for other Googlers...
For me it was simply using wrong package.
I used org.apache.logging.log4j instead org.apache.log4j
If you are using Maven use this:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

Creating an application specific log under Tomcat 7?

I'm trying to capture logging messages, stdout, and stderr for my webapp, which is running under Tomcat 7.
I have a log4j.properties file in my war file, at WEB-IN/classes/log4j.properties, pretty much copied from the log4j docs, except with a filename "myapp.log":
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.File=${catalina.home}/logs/myapp.log
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
WEB-INF/lib includes log4j-1.2.14.jar and commons-logging-1.0.4.
I'm expecting to see myapp.log appear in Tomcat's log directory, but it's not. Please explain to me what I'm doing wrong.
You're using a ConsoleAppender where you should be using a file appender:
log4j.appender.A1=org.apache.log4j.RollingFileAppender

Resources