How to configure Log4j Multiple Appenders for classes in same package - java

Please help to correct the appenders with Threshold of info level to reroute to correct appenders for the classes within a package. Below is the properties file
`
# Root logger option
log4j.rootLogger=INFO, ffile, stdout ,sfile
# 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
# Direct log messages to a log file
log4j.appender.ffile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ffile.Threshold=ERROR
log4j.appender.ffile.File=failure.log
log4j.appender.ffile.layout=org.apache.log4j.PatternLayout
log4j.appender.ffile.layout.ConversionPattern=%d{ISO8601} %c{1}: - %m%n
# Direct log messages to a log file
log4j.appender.sfile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.sfile.Threshold=INFO
log4j.appender.sfile.File=success.log
log4j.appender.sfile.layout=org.apache.log4j.PatternLayout
log4j.appender.sfile.layout.ConversionPattern=%d{ISO8601} %c{1}: - %m%n
#Additivity to stop the mix/repeate of logs in multiple appenders
log4j.additivity.com.rp.handler.AsynchSeeder=false
log4j.additivity.com.rp.handler=false
#Configured logger to route to the appender files. Failing
log4j.logger.com.rp.handler.AsynchSeeder=ERROR, stdout, ffile
log4j.logger.com.rp.handler=INFO, stdout, sfile
#Suppress all low level for open source 3rdparty
log4j.logger.org.apache=WARN
`

Related

Log4j doesnot log the info in server

I have implemented the log4j with following properties.
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to consol
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
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/home/../log4j-application.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
It is working on localhost where in server side it log nothing . I guess above are the general configuration. Is there something i am missing here? I use TomEE on both client and server.Also i used the logger as following. It also does not write in local file. Only logger seems to work on client.
private static final Logger LOGGER = Logger.getLogger(MyClass.class);
I think problem with this line
log4j.appender.file.File=/home/../log4j-application.log
Please use (if you have a log folder)
log4j.appender.file.File=log/log4j-application.log

Weird "No appenders could be found for logger" with netbean maven (Windows)

I have a snippset that I use like gazillion times in Linux that absolutely have no problem. But on windows it fails.
log4j: Using URL [file:/C:/Users/vbb/Documents/NetBeansProjects/MailNotification/./log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL file:/C:/Users/vbb/Documents/NetBeansProjects/MailNotification/./log4j.properties
log4j: Could not find root logger information. Is this OK?
log4j: Finished configuring.
log4j:WARN No appenders could be found for logger (com.mycompany.mailnotification.Main).
log4j:WARN Please initialize the log4j system properly.
The code like this:
System.setProperty("log4j.configuration", new File(".", File.separatorChar + "log4j.properties").toURL().toString());
final Logger logger = Logger.getLogger(Main.class);
logger.debug("Program start!");
Here is the properties file which is placed everywhere from src\main\resources, src\test\resources... In linux, I just place it in project folder.
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
# Redirect log messages to console
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
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log4j-application.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

Maven project log4j not working

I have a simple maven project and trying to get log4j implemented.
deploying to local tomcat. Nothing printing in console of eclipse where I have logger.debug().
Am I missing something?
This is my structure:
pom.xml:
<!-- http://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
Class:
final static Logger logger = Logger.getLogger(myclass.class);
.properties file
# Root logger option
log4j.rootLogger=INFO, stdout
log4j.rootLogger=DEBUG, stdout
log4j.rootLogger=ERROR, 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
Simply remove the superfluous rootLogger declarations:
# Root logger option
log4j.rootLogger=DEBUG, 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
Logging levels are inclusive of those levels "above" them. For example, setting the logging level to DEBUG will include DEBUG, INFO, WARN, ERROR, and FATAL messages automatically. There is no need to declare logging levels for each one.

How to initialize the log4j system properly

I'm getting two warnings.
log4j:WARN No appenders could be found for logger (com.org.Resolver).
log4j:WARN Please initialize the log4j system properly.
How to resolve it?
My java class name Resolver.java
In java class i'm calling
private static Log log = LogFactory.getLog(Resolver.class);
You need to put log4j.properties or log4j.xml in your classpath. So, create file name log4j.properties and replace the content with below:
# Root logger option
log4j.rootLogger=INFO, DEBUG, stdout, file
# Redirect log messages to console
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
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
#. represents the current folder (usually the project root folder)
log4j.appender.file.File=./logging.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 doesn't write to file in a Tomcat7 web application on Amazon server

I used log4j for logging in my web application which is deployed using Tomcat on Amazon Cloud.
I can see the output of log4j on catalena. But, log file is not being created in the given location "/home/ec2-user/EmailLog/userdetails.log" and no logging is being done.
The same web application is running fine and creating the file and logging when tried on localhost.
My log4j.properties content:
# 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=/home/ec2-user/EmailLog/userdetails.log
log4j.appender.file.MaxFileSize=15MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%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:%L - %m%n
And I instantiated the Logger by:
private static final Logger logger = Logger.getLogger(DBManager.class);
Please, suggest modification to make it work on Tomcat-Amazon.

Categories

Resources