Trouble configuring log4j - java

I'm using Java 6 on Win XP and log4j 1.2.12. I'm having trouble getting my logs output to a file (no log4j.log appears). Below is my log4j.properties config, which appears in my war's WEB-INF/classes directory ...
log4j.rootLogger=DEBUG, A2
log4j.appender.A1 = org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
log4j.appender.A2 = org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.File = c:\apache-tomcat-6.0.33\logs\log4j.log
log4j.appender.A2.Append = true
log4j.appender.A2.DatePattern = '.'yyy-MM-dd
log4j.appender.A2.layout = org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
And here's how I access the logger in my class ...
public class XMLNode {
private static Logger logger = Logger.getLogger(XMLNode.class);
...
logger.debug("test:" + main.toString());
Any other ideas what else I should do or check to get my log4j.properties file to appear? Thanks, - Dave

Your problem appears to be with the format of the file name.
log4j.appender.A2.File = c:\apache-tomcat-6.0.33\logs\log4j.log
You will need to change the file path to use either forward slashes (/) or double back slashes (\\)
log4j.appender.A2.File = c:/apache-tomcat-6.0.33/logs/log4j.log
I ran your example above, and when I changed the file path it worked fine.

use direct LOGGER.info(String-Message).. hope so you will get the log file..
i think your log file is present but you are not searching it at right place.. find it in

Related

Log4j with Springboot not appending/creating file

My logger is printing out logs to my console, but I want it to append my .log file. I placed the following poperties inside my application.properties file:
log4j.rootLogger = DEBUG, stdout, stderr, dailyfile
log4j.appender.stdout.Threshold = INFO
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d] %5p %m (%C::%M:%L)%n
log4j.appender.stderr.Threshold = ERROR
log4j.appender.stderr = org.apache.log4j.ConsoleAppender
log4j.appender.stderr.layout = org.apache.log4j.PatternLayout
log4j.appender.stderr.layout.ConversionPattern=[%d] %5p %m (%C::%M:%L)%n
log4j.appender.dailyfile.Threshold = DEBUG
log4j.appender.dailyfile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyfile.File = ../webapps/test/a.log
log4j.appender.dailyfile.layout = org.apache.log4j.PatternLayout
log4j.appender.dailyfile.layout.ConversionPattern=[%d] %5p {%t} %m (%C::%M:%L)%n
log4j.appender.dailyfile.DatePattern ='.'yyyy-MM-dd
And I have a few log.info() calls in my code:
log.info("bla bla bla bla");
For now my logs are visible on the console. This code can't create a file somehow. Even if I create the file manually, it is not being updated (appended). What should I do in order for my code to work?
I didn't know, that file with Log4j properties has to be named exactly log4j.properties. In my case all I had to do was create file, place all properties inside and move this file to src/resources. Thanks for help guys!

Log4j : is it required to have multiple logger objects for creating multiple log files?

i was trying to redirect the log to console and two different log files..
this is my log4j.properties file:
log4j.rootLogger = DEBUG, console, file, csv
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %d{HH:mm:ss} %5p [%t] - %c.%M - %m%n
log4j.appender.file = org.apache.log4j.FileAppender
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern = %d %5p [%t] - %L %c.%M - %m%n
log4j.appender.file.File=./log/logfile.log
log4j.appender.csv = org.apache.log4j.FileAppender
log4j.appender.csv.layout = org.apache.log4j.PatternLayout
log4j.appender.csv.layout.ConversionPattern =%m%n
log4j.appender.csv.File = ./log/log.csv
now the problem is that the logging is happening, but both the files have both the logs that were meant to be logged separately in different files.. i really dont know where am i going wrong! can anyone please help me out??
Log4j does not easily allow for splitting logs between multiple files. The basic idea is that all log statements are considered equal and should be treated equally.
If you were using slf4j as the API you could easily switch the backend from log4j to logback which has SiftingAppender for exactly this purpose.
http://logback.qos.ch/manual/loggingSeparation.html

Java Log4j overwrite output of FileAppenders

I am using Log4j for logging output of my application.
The log4j.properties files contents the following:
log4j.logger.DEFAULT_LOGGER=INFO,main_log, stdout
log4j.additivity.DEFAULT_LOGGER = false
# Direct log messages to a log file
log4j.appender.main_log=org.apache.log4j.FileAppender
log4j.appender.main_log.File=mainLog.log
log4j.appender.main_log.layout=org.apache.log4j.PatternLayout
log4j.appender.main_log.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
Currently the following happens. When I start my main method where I call
static Logger log = Logger.getLogger("DEFAULT_LOGGER");
log.fatal("Process Logger");
it prints the output to stdout and file.log, which is fine. But when I start my application a second time it adds the output to the existing file. But I want to overwrite the log file. Is this possible? (I dont want to use Java for deleting, also I dont want to delete it manually of course) Is there an option in Log4J to tell the logger is to overwrite or the add output?
You need to use RollingFileAppender for this.
Check following SO questions on similar lines:
Post One
Post Two
You can use FileAppender, add log4j.appender.fileAppender.Append=false property to it

Log4J loggers for different classes

I want to use Log4J for logging my java projects.
I created a log4j.properties file in the src directory with the following content:
# Root logger option
log4j.rootLogger=INFO, file, stdout
log4j.logger.DEFAULT_LOGGER=INFO,file2
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=file.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p %c - %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
log4j.appender.file2=org.apache.log4j.FileAppender
log4j.appender.file2.File=file2.log
log4j.appender.file2.layout=org.apache.log4j.PatternLayout
log4j.appender.file2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
For example I want to use the "DEFAULT_LOGGER" only in my main method. So I wrote:
static Logger log = Logger.getLogger("DEFAULT_LOGGER");
log.fatal("Process Logger");
But when I execute the main method I prints the message "Process Logger" to all Appenders (stdout, file and file2) But I only want to print it out to file2. How can I do that or better ro say what do I do wrong?
The second point is, when I execute the main method the second time, it does not overwrite file and file2, it justs adds a line inside the textfile. How can I avoid that?
Log4j has something called additivity. by default it is set to true and it means that every log you write will not only be logged by the specific logger but also by its ancestors (the root logger in this case).
To set it to false try this:
log4j.additivity.DEFAULT_LOGGER = false
Learn more about it here.
try:
log4j.additivity.DEFAULT_LOGGER = false

Can't find log file on server using log4j

I am trying to configure log4j for the first time but its not creating myTest.log file. Below is my log4j.properties file and location of this file is WEB-INF/classes folder.
log4j.rootLogger=INFO, stdout, com.myTest
#tomcat logger
#log4j.logger.org.apache.catalina=DEBUG
log4j.logger.com.myTest=INFO
log4j.logger.org.apache.commons.configuration.PropertiesConfiguration=DEBUG
#appenders
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=[%5p] %d{yyyy-MM-dd HH:mm:ss} %m%n
log4j.appender.com.myTest=org.apache.log4j.FileAppender
log4j.appender.com.myTest.File=myTest.log
log4j.appender.com.myTest.MaxFileSize=15MB
log4j.appender.com.myTest.MaxBackupIndex=10
log4j.appender.com.myTest.layout=org.apache.log4j.PatternLayout
log4j.appender.com.myTest.layout.ConversionPattern=[%5p] %d{yyyy-MM-dd HH:mm:ss} %m%n
log4j.appender.org.apache.ibatis=org.apache.log4j.FileAppender
log4j.appender.org.apache.ibatis.File=myTest.log
log4j.appender.org.apache.ibatis.MaxFileSize=15MB
log4j.appender.org.apache.ibatis.MaxBackupIndex=10
log4j.appender.org.apache.ibatis.layout=org.apache.log4j.PatternLayout
log4j.appender.org.apache.ibatis.layout.ConversionPattern=[%5p] %d{yyyy-MM-dd HH:mm:ss} %m%n
My controller class is like
#Controller
public class LoginController
{
static final Logger logger = Logger.getLogger(LoginController.class);
#RequestMapping("/login")
public ModelAndView login(){
logger.debug("Test logs");
logger.error("test error");
return new ModelAndView("login");
}
}
But I can't see any myTest.log file in my Apache/log folder. Is this a place it should be?
Jar file I am using is log4j-1.2.16.jar. Is anything else required.
I am using Ubuntu and its first time I am using this.
Put log4j.properties file in build/classes/ folder.
Put file path to ${catalina.home}/logs/myTest.log.
log4j.appender.com.myTest.File=${catalina.home}/log/myTest.log
Path of logs can be checked by writing below code in your LoginController class
Enumeration e = Logger.getRootLogger().getAllAppenders();
while ( e.hasMoreElements() ){
Appender app = (Appender)e.nextElement();
if ( app instanceof FileAppender ){
System.out.println("File: " + ((FileAppender)app).getFile());
}
}
As you have posted in your question the location of log4j.properties file is WEB-INF/classes folder.
To solve the issue try placing the log4j.properties file within src folder of your application.
I encountered the same problem。
On the server machine,log4j could't write log file, but used standout to print log info, it is working.(do you?)
I think is the permissions problem.
so i write log file in tomcat log location(logs/)
log4j.appender.com.myTest.File=${catalina.home}/logs/myTest.log
Hope this works for you!!

Categories