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.
Related
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
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
`
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
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.
I wrote the following in my log4j.properties file
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{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.logger.org.springframework.web = TRACE, stdout
But I'm just getting the init logging. What should I do so as to get detailed logging so as to get the trace whenever i enter a url in my browser.
Its very simple to log the execution of program :
1) Add log4j dependency in pom.xml
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.4.2</version>
</dependency>
2) Add the one Listner class in web.xml
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
3) Then add log4jConfigLocation in web.xml
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
and then add your log4j properties as follows :
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %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{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=debug, file, stdout
4) Go to the particular java file in your application import log4j package
and then implement your logging mechanism
5) Now you should be able to trace the log