log4j - stdout vs fileout? - java

What is the difference between stdout and fileout in log4j?
If I have this set in my log4j.properties file,
log4j.rootLogger=INFO, stdout, fileout
....
log4j.logger.org.springframework.aop.framework.autoproxy=INFO, stdout, fileout
Is it necessary to set these to all 3 of the above options (INFO, stdout, fileout)? Will this cause duplicate lines to appear? If I remove "fileout" will I be losing any potentially important print statements from the log file?

Related

log4j not logging into file

log4j.rootLogger=INFO
log4j.logger.com.app.yum.package.WatchDog=INFO,logfile
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.file=watchdog/watchdog.log
log4j.appender.logfile.maxFileSize=1024KB
log4j.appender.logfile.maxBackupIndex=5
log4j.appender.logfile.append=true
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d | %-5p | %m | %c | %t%n
I am using above config in log4j properties file. I could see a new file has been created but there are no logs inside it.
Instead I see the logs on console. What I am trying to do is log all the logs of particular class in a file watchdog.log.
Below is my logger initialization
private static final Logger logger = LoggerFactory.getLogger(WatchDog.class);
Am I missing anything?
You need to tell the log4j to log on file,
First add:
log4j.rootLogger=INFO, FILE
Then you need to specify the file Appender like:
#settings for FILE: output to file using standard TTCC pattern
#this option backs up the file daily with a date stamp
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd
#this option backs up the file after is reaches a specified size
#log4j.appender.FILE=org.apache.log4j.RollingFileAppender
#log4j.appender.FILE.MaxFileSize=1KB
#log4j.appender.FILE.MaxBackupIndex=3
#optionally change the filepath to the log file
log4j.appender.FILE.File=D:/Reliances/reliance90e_NOVO/logs/reliance.log
log4j.appender.FILE.layout=org.apache.log4j.TTCCLayout
log4j.appender.FILE.layout.ContextPrinting=true
log4j.appender.FILE.layout.DateFormat=ISO8601
log4j.appender.FILE.Threshold=DEBUG
and to intialzed the logger you can do:
//From org.apache.log4j.Logger
Logger logger = Logger.getLogger(obj.getClass().getName());
Note: The value are an example and the also i want to show you the different options in the File Appender, also this configurations should be added to your another configurations.

Log4J out to console and file produces warnings/errors log4j:WARN File option not set for appender

I'd like to integrate Log4J logging into my Java application, writing information out to a log file as well as displaying the info at the console. Unfortunately, I've had some trouble getting this off the ground. I'm working with Eclipse Juno on Win 7.
My log4j.properties file:
#configure logfile
log4j.appender.logfile = org.apache.log4j.RollingFileAppender
log4j.appedner.logfile.File = MyAppLog.log
log4j.appender.logfile.Append = true
log4j.appender.logfile.layout = org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern = %-5p %d [%t][%F:%L] : %m%n
#configure 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 = %-5p %d [%t][%F:%L] : %m%n
log4j.rootLogger = debug, logfile, stdout
I belive the above follows the pattern of some other accepted answers on this topic.
I've specifed the location of this log4j.properties file wtih a VM argument:
-Dlog4j.configuration=file:///${workspace_loc}\path\to\file\log4j.properties
I instantiate the logger in my java class:
static Logger logger = Logger.getLogger(MyApp.class.getName());
log a string:
logger.info("barfoo");
This configuration results in the following warnings/errors on launch:
log4j:WARN File option not set for appender [logfile].
log4j:WARN Are you using FileAppender instead of ConsoleAppender?
log4j:ERROR No output stream or file set for the appender named [logfile]
It does not produce the log file. The console logging seems to be working just fine, however.
This will sound stupid - you have a typo. You have log4j.appedner.logfile.File instead of log4j.appender.logfile.File (note the 'n' and 'd' are switched). This is why it's complaining that you haven't set the File.

Issue with log4j log not writing to file

Does anyone see why this log is not writing to file. It is writing to standard out twice but not to the file:
Also, I tried removing the "Stdout" appender and then I don't get any logging at all.
package org.berlin.wicket;
import org.apache.log4j.Logger;
private static final Logger LOG = Logger.getLogger(QuickstartPage.class);
LOG.info("Loading constructor");
log4j.rootLogger=DEBUG,Stdout,mainAppender
log4j.appender.Stdout=org.apache.log4j.ConsoleAppender
log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.Stdout.layout.conversionPattern=%-5p - %-26.26c{1} -
%m\n
log4j.appender.mainAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.mainAppender.file=logs/core.log
log4j.appender.mainAppender.datePattern='.'yyyy-MM-dd
log4j.appender.mainAppender.append=true
log4j.appender.mainAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.mainAppender.layout.ConversionPattern=[%d{MM/dd/yyyy
HH:mm:ss.SSS}] [%C{1}.%M():%L] [%t] [%p] - %m%n
log4j.logger.org.berlin=DEBUG,Stdout,mainAppender
You have two loggers which use the same appender (Stdout), hence why you see entries on the console twice.
As others have mentioned, the properties are case-sensitive, hence why your file appender is not configured correctly.
The File and Append properties are case senstive.
log4j.appender.mainAppender.File=someFileName.log
log4j.appender.mainAppender.Append=true
You should create an instance of FileHandler that writes log to a file called myfile.log.
FileHandler fileHandler = new FileHandler("myfile.log", true);
logger.addHandler(fileHandler);

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

How to invoke a new notepad with Java and save it in a directory in windows?

I am currently logging test execution results in to a textfile(An existing one.) How do I create a textfile with a timestamp and save it in a windows directory? The code I am using currently is
File outputResultFile = new File(CompleteFileNameForNotepad);
PrintWriter outputFile = new PrintWriter(new FileWriter(outputResultFile));
String str = "text to write in to the file";
outputFile.println(str);
outputFile.close();
Thanks for your help.
String fileName = new SimpleDateFormat("yyyy-MM-ddTHH-mm-ss").format(new Date()) + ".txt";
File outputResultFile = new File(fileName);
...
Note: what you call a notepad is more commonly called a "file".
You should consider using a logging framework instead. Check out Log4j for example. You can then alter the logging format and which classes are logged by tweaking the logging configuration. This means you don't have to alter your code.
The log4j.properties below would log all classes under the package foo, at WARN level or higher to the Console, using the pattern "2000-09-07 14:07:41,508 [main] INFO MyApp - Entering application."
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
# Print the date in ISO 8601 format
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN
To log to a file use a different Appender, such as org.apache.log4j.FileAppender or org.apache.log4j.RollingFileAppender. For your purpose org.apache.log4j.DailyRollingFileAppender would be suitable.

Categories