log File not generating in log4j - java

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"

Related

log4j:ERROR No output stream or file set for the appender named [file]

This is my log4j.properties file.
log =/src/main/resources
log4j.rootLogger=INFO, file, stdout
# Define the file appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=${log}/logger.log
log4j.appender.file.MaxFileSize=12MB
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
# configuration to print on 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
#--------------------------------------------------------------------#
And this is my test class .logs are printing in console but not writing in log files can anyone tell what's wrong here i am getting error like
log4j:ERROR No output stream or file set for the appender named [file].
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Test {
static final Logger logger = Logger.getLogger(Test.class);
public static void main(String[] args) {
System.out.println("hello");
PropertyConfigurator.configure("/src/main/resources/log4j.properties");
logger.debug("Log4j appender configuration is successful !!");
logger.info("Log4j appender configuration is successful info!!");
}
}

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

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.

Disable log4j console logging in linux

I have my log4j.properties file as -
# Root logger option
log4j.rootLogger=WARN, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/tmp/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
Problem is, log messages are also getting printed on console. How can I disable that? I have read some of the posts here on stackoverflow but other users were facing issues because they defined the consoleAppender into their file but I haven't defined it in my log4j.properties file and still it displays it on console.
Please help.
Try doing additivity false. like below in your property file.
log4j.additivity=false
log4j.rootLogger=ERROR, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %5p %c (%F:%L) - %m%n
#hide the Log4jMemoryAppender from console
log4j.logger.nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender= ERROR, MEMORY_APPENDER
log4j.appender.MEMORY_APPENDER=nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender.Log4jMemoryAppender
log4j.additivity.rootLogger = false
log4j.additivity.console = false
log4j.additivity.MEMORY_APPENDER=false

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

Categories

Resources