how to access log4j property in all class? - java

I am using log4j in my application. Now I have created property file.How to access this property file in all Java classes. Can anyone please explain me about it?
# 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=${rootPath}WEB-INF/logs/MyLog.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{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

import org.apache.log4j.Logger;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
public class log4jExample{
/* Get actual class name to be printed on */
static Logger log = Logger.getLogger(
log4jExample.class.getName());
public static void main(String[] args)
throws IOException,SQLException{
log.debug("Hello this is an debug message");
log.info("Hello this is an info message");
}
}

Try adding this to your web.xml if you havent added that already
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>./conf/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<!-- Refresh log4j configuration every 5 minutes. -->
<param-value>300000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

Check your catalina.log for any errors. Make sure that log4j is initialized correctly. If you have multilpe log4j jar files (one in your context and another in tomcat/lib) it may not have initialized properly. Try deleting duplicate jars

Related

log File not generating in log4j

I am new to log4j. I have created a sample java program implementing log4j in it.
Below is the java program:
package logging;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class Logging {
/**
* #param args the command line arguments
*/
private static Logger logger = Logger.getLogger(Logging.class);
public static void main(String[] args) {
BasicConfigurator.configure();
logger.trace("This is a Trace");
logger.debug("This is a Debug");
logger.info("This is an Info");
logger.warn("This is a Warn");
logger.error("This is an Error");
logger.fatal("This is a Fatal");
}
}
I am getting the output in the console screen.But the log file is not getting generated. I have also configured my project in the Eclipse neon using the following link:
Configuration
I have done everything good. But the log file is not generating.When I implement log4j programmatically the file is getting generated.The following is my properties file:
#root
log4j.logger.com.apress.logging.log4j=debug,dest
log4j.additivity.com.apress.logging.log4j=false
#define the appender
log4j.appender.dest = org.apache.log4j.DailyRollingFileAppender
#set the name of the file
log4j.appender.dest.File=${user.home}/log.out
#setting the immediate flush to true (default)
log4j.appender.dest.ImmediateFlush=true
#setting the threshold
log4j.appender.dest.Threshold=ERROR
#setting the append to false, overwrite
log4j.appender.dest.Append=true
#set the DatePattern
log4j.appender.dest.DatePattern='.' yyyy-MM-dd
What do I need to do to have Log4J write to the log file?
Ensure log4j.properties is in default package
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
log4j.logger.infoLogger=DEBUG, infoLogger
log4j.additivity.infoLogger = false
# 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=E:\\LOG\\ConvertorLogger.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
# Redirect log messages to a log file, support file rolling.
log4j.appender.infoLogger=org.apache.log4j.RollingFileAppender
log4j.appender.infoLogger.File=E:\\LOG\\ConvertorInfoLogger.log
log4j.appender.infoLogger.MaxFileSize=5MB
log4j.appender.infoLogger.MaxBackupIndex=10
log4j.appender.infoLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.infoLogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
You can refer to this fileļ¼šlog4j.properties
log4j.rootLogger=WARN,stdout,R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c -%m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${user.home}/log.out
log4j.appender.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %p %t %c -%m%n
log4j.logger.com.foo=WARN
I had to give the absolute path (for Windows, using Intellij IDEA) in my log4j2.xml file as following:
fileName="C:\Users\Fotios.Kolytoumpas\IdeaProjects\jetbrains-hibernate\logs\your-app-name.log"

Logger doesn't write to file Log4J

I don't understand why my Log4J is not logging to file. Ive been looking many similar posts but none helped me. I configured all correctly, but it doesn't log to the file.
Let me show you, this is the config 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=log.out
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.MaxFileSize=1024MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.Append=true
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.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
And this is how I use the Logger:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...
private Logger logger = LoggerFactory.getLogger(GameBoardService.class);
...
logger.info("log this text into file....");
Try to specify your base package for the logging
log4j.logger.[your own class base package ]=INFO
e.g.
log4j.logger.org.springframework=WARN
log4j.logger.org.hibernate=WARN
Be specific with the log path to enable you to easily locate the path, e.g. below.
log4j.appender.file.File=/var/logs/log.out
Furthermore, /var/logs/log.out is created in Windows as C:\var\logs\log.out and in Linux as /var/logs/log.out and there is no need to switch path slashes according to OS. Just a reminder, ensure you have permission to the specified path.

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 with spring create empty file

I want to use log4j 1.2.9 with spring 4.0.1 and JBOSS 7.1 but every time i get log information on console but the file is created and empty ...
log4j.properties
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\log\\loggingFile.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
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=INFO, file, stdout
web.xml
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
Simple test Controller
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
#ManagedBean(name="MyController")
public class MyController {
static final Logger logger = Logger.getLogger(MyController.class);
public String test() {
BasicConfigurator.configure();
logger.debug(LoginController.class);
logger.info("Info.. ");
logger.error("Error..");
logger.fatal("Fatal");
return null;
}}
the solution was to add jboss-deployment-structure.xml in /WEB-INF to exclude org.apache.log4j
<jboss-deployment-structure>
<deployment>
dependencies -->
<exclusions>
<module name="org.apache.log4j" />
</exclusions>
</deployment>
</jboss-deployment-structure>
As your configurations look fine to me, I believe you have an extra copy of the log4j.jar in your webapp.
Moreover, why don't you use the logging subsystem present in JBoss AS 7.x? By this subsystem, you can have centralized configuration for all you want to achieve. It will be easier to maintain as well.
For this, just remove log4j.properties and import log4j/slf4j via dependencies and make sure you don't pack your own log4j/slf4j jars in your webapp.
Check out Logging Configuration for JBoss.
Shishir

Enabling complete Path Logging in Spring MVC

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

Categories

Resources