Log4j does not create the logfile - java

I want to use the unusuable log4J for logfiles in a java maven application. But the logfile is not created.
This is my a part of my .pom file:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
This is my log4j.properties file (under src/main/resources):
# 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=E:\\Test\\logfile.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
# 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
In my main method:
log.debug("debug");
log.info("info");
log.warn("warn");
log.error("error");
The log messages are in the console log, but the logfile has not been created. What am I doing wrong?

your pom.xml is not correct. it has to be <dependency> in the first line. (missing >)
Apart from that, your log4j.properties should do the work, I tried it and it writes a log-file.
How do you create the Logger instance log?
Do you have writing access to E:\\Test\\logfile.log?

Related

Regarding log4j working in java application

I am using log4j in my java application. My log4j property file is as follows
# 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=file_path
log4j.appender.file.MaxFileSize=500MB
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
# 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​
Problem I am facing is maxfilesize is defined as 500 MB, so my understanding is that after 500 MB a new log file should be generated but in my case new file is getting generated only after server restart. If I don't resstart my server, logs get accumulated in the same file even after 500 MB limit. How to resolve it? Is it the normal behaviour of log4j or there is something wrong with my configuration?

Apache log4j and Tomcat 7 DEBUG related logs only logging into "catalina.log" but not only logging into catalina.YYYY-mm-dd.log file

I'm trying to use Apache log4j in Tomcat 7. I need the logs in DEBUG mode so I have used
log4j.rootLogger=DEBUG, stdout, file
I have also used file rolling so as to get the logs daily in a separate file.
log4j.appender.file=org.apache.log4j.RollingFileAppender
but in the DEBUG mode all the debug related logs are logging into "catalina.out" file instead of the daily created log, I cannot see any DEBUG related logs in the daily created log file only INFO and SEVERE are present in it. And my another question is how to not to log everything into catalina.log file as the file size is keep on increasing
log4j.rootLogger=DEBUG, stdout, 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{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
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
Dependencies that I have used:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
If you are working in windows environment please add below line code to your File logging configuration:
log4j.appender.file.File=${catalina.home}\\yourLog.log
This will direct to tomcat->bin folder. If you want specifically in tomcat->logs folder please configure accordingly by going one step backwards.
It is because The Apache Tomcat has a custom version of Log4J (referenced as JULI) built in it.
From the Tomcat Documentation:
The internal logging for Apache Tomcat uses JULI, a packaged renamed
fork of Apache Commons Logging that, by default, is hard-coded to use
the java.util.logging framework. This ensures that Tomcat's internal
logging and any web application logging will remain independent, even
if a web application uses Apache Commons Logging.
You could use:
log4j.rootLogger=TRACE, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\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 %c{1}:%L - %m%n

log4j:WARN No such property [maxFileSize] in org.apache.log4j.DailyRollingFileAppender

Everything works just as fine. But showing this error.
My log4j.properties file like :
# 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.myAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.file.File=D:\\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
Thanks in advance. Just tell me how to do it. I just want log file on daily rolling .
DailyRollingFileAppender doesn't support MaxFileSize, RollingFileAppender does.
DailyRollingFileAppender is for rolling files based on the date and time of the log entry, so if you want to use it you should remove the MaxFileSize property.
I changed the code to
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
from
log4j.appender.FILE=org.apache.log4j.FileAppender
and it worked fine in log4j.properties file

Java Log4J Logger Messages

I am working on log4j with Java. I am redirecting all my applications logger messages to a specific folder inside my project like this:
logs/log4j-MyFirstClass.log
If I want to redirect specific file messages to specific log file, for ex: ABC.java logger messages to ABC.log file, then how should I change this log4J.properties file ? Please help. Here is my log4j.properties file:
# 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=logs/log4j-MyFirstClass.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
You need to define an appender for each file, then specify which packages use which appender.
# Root logger option; everything logs to console at debug
log4j.rootLogger=INFO, stdout
# Classes in package com.foo.bar will use the file1 appender
log4j.logger.com.foo.bar=file1
# Classes in package com.whizz.bang will use the file2 appender, note we also overrode the default log level
log4j.logger.com.whizz.bang=DEBUG, file2
# 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
# Appender for file 1
log4j.appender.file1=org.apache.log4j.RollingFileAppender
log4j.appender.file1.File=logs/log4j-first.log
log4j.appender.file1.MaxFileSize=5MB
log4j.appender.file1.MaxBackupIndex=10
log4j.appender.file1.layout=org.apache.log4j.PatternLayout
log4j.appender.file1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Appender for file 2
log4j.appender.file2=org.apache.log4j.RollingFileAppender
log4j.appender.file2.File=logs/log4j-second.log
log4j.appender.file2.MaxFileSize=5MB
log4j.appender.file2.MaxBackupIndex=10
log4j.appender.file2.layout=org.apache.log4j.PatternLayout
log4j.appender.file2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Need to get log file in folder in java

This is my log4j property file, i need to get log file in this location, but it didn't create
# Root logger option
log = D:\\log
log4j.rootLogger=ALL, 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
in my class i initiate like this
private static final Logger LOGGER = Logger.getLogger(test.class);
& i added info or err when i need ex
LOGGER.info("Get Request from Service");
I can get this output in console but i didn't get that log file.
Use FileAppender for this purpose
# Root logger option
log4j.rootLogger=INFO, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:/log/application.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
read some
log4j tutorial
specify your file location here
log4j.appender.file.File=<location of your file goes here>
ex :log4j.appender.file.File=D:/log/application.log

Categories