All log4j appenders defined in log4j.properties in play - java

My log4j.properties file is pasted below. My understanding is that we need to add Appenders to the root logger for the appender to work. As you can see in the below properties file, only appender A is attached to the root logger (log4j.rootLogger=info, A). However, what I see is that the logging information is printed to both the appenders (ConsoleAppender - A and File Appender - B). How is this possible?
log4j.rootLogger=info, A
log4j.appender.A=org.apache.log4j.ConsoleAppender
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%-4r [%t] [rid=%X{RID} ] %-5p %c %x - %m%n
log4j.appender.B=org.apache.log4j.FileAppender
log4j.appender.B.layout=org.apache.log4j.PatternLayout
log4j.appender.B.file=target/server.log
log4j.appender.B.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Well, it turns out that the Console appender (A) was printing out to the server.log (glassfish log) which incidentally is also the name of the log file associated with appender B.
The following property was attached to the glassfish server (startup parameters) which helped me understand which log4j.properties (in case of stray properties file) file is loaded along with information like what appenders are used for logging.
-Dlog4j.debug=true

Related

log4j doesn't find appenders

Always I try to use a formal and structurized log api is a nightmare (this explains why so many people just print to console)
I'm trying to use log4j in my project
to instantiate the logger I do:
private static final Logger log = LoggerFactory.getLogger(Instagram.class.getPackage().getName());
when I want to register an event I do:
log.info("some information");
I've a log4j.properties file in the src folder like:
log4j.logger.com.tomatechines.instagramapi.api = INFO, CONSOLE, FILE, ERROR
log4j.rootLogger = INFO, FILE, ERROR
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.DatePattern = yyyy-MM-dd'.log'
log4j.appender.FILE.File = logs/log-
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{HH:mm:ss.SSS} %-5p %C{1}:%L - %m%n
log4j.appender.ERROR=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR.DatePattern = yyyy-MM-dd'.log'
log4j.appender.ERROR.File = logs/errorlog-
log4j.appender.ERROR.layout = org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.conversionPattern=%d{HH:mm:ss.SSS} %-5p %C{1}:%L - %m%n
log4j.appender.ERROR.Threshold=WARN
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern=%d{HH:mm:ss.SSS} %-5p %C{1}:%L - %m%n
but when the code runs the only thing printed to console or file is:
log4j:WARN No appenders could be found for logger (com.tomatechines.instagramapi.api).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
why it doesn't find my file, I set 3 appenders and why can't it find just one
First of all, I can see that you are using SLF4J with log4j 1.2.x, because you are instantiating the logger with LoggerFactory.getLogger. If you want to use only log4j 1.2.x you should get the logger with Logger.getLogger. Making sure that you have imported the classes from org.apache.log4j package.
You must also make sure that you have put the log4j.properties file under the classpath. And in my opinion you should try first with a very simple configuration, to avoid configuration problems. Something like:
# Root logger option
log4j.rootLogger=INFO, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Probably your problem is about the location of log4j.properties file.

Do i have to create appenders for all classes in log4j?

I'm using log4j for my final project. I tested it using single class before I used log4j in my project. That worked fine.
Then I added log4j to my project and I used it in many classes using:
final static Logger logger = Logger.getLogger(Supplier.class);
Then I got a warning:
log4j:WARN No appenders could be found for logger (classes.Supplier).
log4j:WARN Please initialize the log4j system properly.
This is my properties file:
# Root logger option
log4j.rootLogger=DEBUG, file
log4j.rootLogger=DEBUG, errorfile
# Redirect log messages to console
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.errorfile=org.apache.log4j.RollingFileAppender
# Redirect log messages to a log file, support file rolling.
log4j.appender.file.File=C:\\HardwareLog\\INFO.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.errorfile=C:\\HardwareLog\\ERROR.log
log4j.appender.errorfile.Threshhold=ERROR
log4j.appender.errorfile.MaxFileSize=5MB
log4j.appender.errorfile.MaxBackupIndex=10
log4j.appender.errorfile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Do I have to create appenders for each class to use it?
Can I use one properties file in different packages ?
I found a solution. I removed root logger and added two loggers for my two packages.
log4j.logger.package1=DEBUG,FIRST
log4j.logger.package2=DEBUG,SECOND
now its working fine.
you can go for root logger that will log for the entire application.
# Root logger option
log4j.rootLogger=INFO, file
plese go through this link
https://www.mkyong.com/logging/log4j-log4j-properties-examples/

Log4j is not finding configuration in properties file

My log4j.properties file is:
# Log levels
log4j.rootLogger=INFO,CONSOLE,file
# Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
# Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Rolling File Appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
# Path and file name to store the log file
log4j.appender.file.File=C:/Logging/log4jFile.log
log4j.appender.file.MaxFileSize=200KB
# Number of backup files
log4j.appender.file.MaxBackupIndex=2
# Layout for Rolling File Appender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d - %c - %p - %m%n
and the location of log4j.properties file is in WEB-INF of my project.
So when I run my servlet I get this below message:
log4j:WARN No appenders could be found for logger (...).
log4j:WARN Please initialize the log4j system properly.
Where I am going wrong, I don't understand!
I'm pretty sure by default Log4J searches in the classes directory. So try putting your log4j.properties there.
This is due to log4j searching the classpath for the log4j.properties and the WEB-INF Folder is not part of that but the classes directory is. So either put the file in classes directory, add the file to the classpath or manually load in your Servlet with:
Below is my log4j properties file which works for me. It needs to be in the src/main/resources source folder. I notice the top line is different between mine and yours so maybe this is incorrect - try changing rootLogger to rootCategory.
log4j.rootCategory=INFO, CONSOLE, FILE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=ERROR
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d %-5p %c- %m%n
log4j.logger.com.myCompany.log4jexample= TRACE, FILE, CONSOLE
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=Logs.log
log4j.appender.FILE.Threshold=WARN
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d %-5p %c- %m%n

Different logging appender on LOG4J

I'm using log4j 1.2 and I need to:
log everything (including logging from referenced libraries) to console
log from my code to file (and maybe to console)
Using the following code:
log4j.rootLogger=DEBUG, CONSOLE
log4j.com.mypackage=ALL, CONSOLE, CSV
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.err
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n
log4j.appender.CSV=org.apache.log4j.FileAppender
log4j.appender.CSV.File=./myfile.csv
log4j.appender.CSV.Append=false
log4j.appender.CSV.layout=org.apache.log4j.PatternLayout
log4j.appender.CSV.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n
The logfile is not created.
Other try I done:
If I add CSV appender to rootLogger, then the file is created and filled.
If I add CSV appender to rootLogger and disable it, then the file is just created.
If I log only my logger to console... the it works fine
Do you have any idea to solve?
Thanks
You are missing an important part.
The value has to be
log4j.logger.com.mypackage
Note that you are missing logger in the log definition

Web Service and Log4J Output to File

So, I have the log4j configurations set up and it output to the console just fine. But I need it to output to a file now. I am using Axis2 in Eclipse Galileo.
I edited the default log4j.properties file to this:
Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
log4j.rootCategory=INFO, CONSOLE, LOGFILE
Set the enterprise logger priority to FATAL
log4j.logger.org.apache.axis2.enterprise=FATAL
log4j.logger.de.hunsicker.jalopy.io=FATAL
log4j.logger.httpclient.wire.header=FATAL
log4j.logger.org.apache.commons.httpclient=FATAL
CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%p] %m%n
LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=C:\Users\*username*\Desktop\Android\Android_WS\src\wtp\AndroidWS_Log.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
But the output is still not getting to the Log file. Any help would be much appreciated. Thanks Guys!
Remove line 2:
log4j.rootCategory=INFO, CONSOLE
Also, try using a forward slash, '/' for the directory separator:
log4j.appender.LOGFILE.File=C:/Users/*username*/Desktop/Android/Android_WS/src/wtp/AndroidWS_Log.log
Make sure that your server has write access rights to the log file.

Categories