log4j configurations for non web application - spring

I've create a simple maven application and using log4j logger. My application is not a WEB application. I've tried to copy log4j.properties everywhere in java folder and everywhere across a project but still getting following error. Could you suggest how can I fix it?
log4j:WARN No appenders could be found for logger (org.springframework.web.client.RestTemplate).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
log4j.properties
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
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
# 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

The log4j.properties file should be in a dirctory or JAR file that is on the classpath at runtime.
If you have a Maven project with the standard Maven project directory layout, then put the file in the directory src\main\resources - that way, Maven should put it in the right place for you when you compile your project.

Related

When running test cases with maven, logger not generating log file

Getting below error when run the batch file.
log4j:WARN No appenders could be found for logger (devpinoyLogger).
log4j:WARN Please initialize the log4j system properly.
One quick way to fix this is to add a log4j.properties file in main/src/test/resources.
Here's a sample log4j.properties file that will output logs to the console:
# Set root logger level to INFO and its only appender to stdout.
log4j.rootLogger=INFO, stdout
# Define the stdout appender to output logs to the console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n

Hadoop log4j not working as No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory)

I am working on developing mapreduce using eclipse , and trying to test it using hadoop 2.6.0 windows standalone mode.
But getting the below error for log4j,
How to fix the below appender problem,
No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory)
same problem as hadoop log4j not working but no answer yet
Thanks ,
1- Create the file log4j.properties and put it in the location of
src/main/resources
the content of the log4j.properties
hadoop.root.logger=DEBUG, console
log4j.rootLogger = DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
it happens because of the hadoop logging framework
Create a log4j.properties file with something like the following:
hadoop.root.logger=DEBUG, console
log4j.rootLogger = DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n

Spring Boot and Log4j Issue

my log4j setup is following. When I run as Boot App, the logs are correctly written in console, debug.log and dump.log. Below is what I do in my program to write log in console and debug.log
static final Logger LOG = Logger.getLogger(EnvironmentLoader.class);
LOG.info("blah blah!");
Below is what I do in my program to write log in dump.log
private static final Logger DUMP_LOG = Logger.getLogger("dumpLogger");
DUMP_LOG.info("blah blah!");
Both works fine if I run as Spring Boot App. If I package it as war and run in tomcat, DUMP_LOG writes correctly in dump.log but LOG is not writing in console or debug.log. I wonder why.
log4j.rootLogger=INFO, stdout, debugLog
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p %c.%M:%L - %m%n
log4j.appender.stdout.Target=System.out
log4j.appender.debugLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debugLog.Append=true
log4j.appender.debugLog.DatePattern='.'dd-MM-yyyy
log4j.appender.debugLog.File=${catalina.base}/logs/debug.log
log4j.appender.debugLog.MaxFileSize=10MB
log4j.appender.debugLog.encoding=UTF-8
log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout
log4j.appender.debugLog.layout.ConversionPattern=%d %-5p %c.%M:%L - %m%n
log4j.category.debugLogger=DEBUG, debugLog
log4j.additivity.debugLogger=false
log4j.appender.dumpLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dumpLog.Append=true
log4j.appender.dumpLog.DatePattern='.'dd-MM-yyyy
log4j.appender.dumpLog.File=${catalina.base}/logs/dump.log
log4j.appender.dumpLog.MaxFileSize=10MB
log4j.appender.dumpLog.encoding=UTF-8
log4j.appender.dumpLog.layout=org.apache.log4j.PatternLayout
log4j.appender.dumpLog.layout.ConversionPattern=%d - %m%n
log4j.category.dumpLogger=DEBUG, dumpLog
log4j.additivity.dumpLogger=false
The following dependency resolved the issue.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>

Why does this only log for hibernate?

Within my log4j.properties, log4j.rootLogger=DEBUG, stdout only shows debug data for hibernate. Can anyone explain this? I'm trying to debug other packages but none of them will work.
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{ABSOLUTE} %5p %t %c:%L - %m%n
I realized that you do NOT have log4j in your pom, you need these 3 dependencies to log with log4j and slf4j:
slf4j-api-x.x.x.jar
slf4j-log4j12-x.x.x.jar
log4j-x.x.x.jar
log4j and slf4j-log4j12 should have the same version.
Please also note the commet from #Stefan Lindenberg

log4j path in production for tomcat 7 on windows

I need simple path setting to use log4j to be used for logging FINEST level of logging. But all forums and discussions show path setting for log4j.properties file either under eclipse or in some dev env. For a non development user, i need to set log4j.properties file, i tried in C:\apache-tomcat-7.0.27\conf path, but dosent help. stdout logs show default logs, note I have deleted default ogging.properties from \conf folder.
Where do i put log4j.properties file in windows tomcat folder ?
well, as official docs say (http://tomcat.apache.org/tomcat-7.0-doc/logging.html), you should have not deleted, the default logging.properites file, present on:
${catalina.base}/conf/logging.properties
,but rather add there what you need:
org.apache.catalina.level=FINEST
and
You would need to ensure the ConsoleHandler's (or FileHandler's') level is also set to collect this threshold, so FINEST or ALL should be set.
Or is there any strict requirement for you to use log4j (as per default JULI is used by tomcat7)?
UPDATE:
OK, as you mentioned, you're interested in the log4j case only. Let me point you to the official documentation: http://tomcat.apache.org/tomcat-7.0-doc/logging.html#Using_Log4j
Please follow the steps present there. As they claim to be working :)
For the explicit points you mentioned, the location of the log4j.properties file:
Create a file called log4j.properties with the following content and save it into $CATALINA_BASE/lib
log4j.rootLogger=FINEST, CATALINA
# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.
log4j.appender.CATALINA.Append=true
log4j.appender.CATALINA.Encoding=UTF-8
# Roll-over the log once per day
log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.LOCALHOST=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.File=${catalina.base}/logs/localhost.
log4j.appender.LOCALHOST.Append=true
log4j.appender.LOCALHOST.Encoding=UTF-8
log4j.appender.LOCALHOST.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.File=${catalina.base}/logs/manager.
log4j.appender.MANAGER.Append=true
log4j.appender.MANAGER.Encoding=UTF-8
log4j.appender.MANAGER.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.HOST-MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HOST-MANAGER.File=${catalina.base}/logs/host-manager.
log4j.appender.HOST-MANAGER.Append=true
log4j.appender.HOST-MANAGER.Encoding=UTF-8
log4j.appender.HOST-MANAGER.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Encoding=UTF-8
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
# Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=\
INFO, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]=\
INFO, HOST-MANAGER
Compared to the official docs, I just changed the 1.st line of the config to: log4j.rootLogger=FINEST, CATALINA
(But make sure you do the rest of the steps as well - like copying of the jar files,...)

Resources