Logger doesn't write to file Log4J - java

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.

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

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"

can't able to implement log4j for JBOSS EAP 7 using java?

i have an application, i have applied log4j, but i can't able to create log file inside jboss application.
when i put absolute path of system in log4j.appender.file.File= D:/log/application.log it was working fine but when i put log4j.appender.file.File= ${jboss.server.home.dir}/log/application.log it is not working.
# 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.DailyRollingFileAppender
#log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File= ${jboss.server.home.dir}/log/application.log
log4j.appender.file.MaxFileSize=5KB
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
try using $JBOSS_HOME instead of ${jboss.server.home.dir}.. so it becomes
log4j.appender.file.File= ${JBOSS_HOME }/log/application.log

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