Below is the log4j properties file content. I am trying to write WARN logs to two differnt different log filesBY by define two log appenders as shown below.
Problem : The logs are wrting into both the files (duplicate) .
log4j.rootLogger=WARN, A1, xmlout, GPSMSPLogger
#log4j.rootLogger=DEBUG, xmlout
log4j.logger.mailLogger=ALL, mail
log4j.category.com.moodys=INFO
# Setting additivity to false
# For not writing same logs into two files
# R3'13
log4j.additivity.org.apache=false
log4j.additivity.A1=false;
log4j.additivity.GPSMSPLogger=false;
log4j.logger.GPSMSPLogger=DEBUG,GPSMSPLogger
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.threshold=DEBUG
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c %x %m%n
# xmlout is set to be a DatedFileAppender
log4j.appender.xmlout=biz.minaret.log4j.DatedFileAppender
log4j.appender.xmlout.Threshold=DEBUG
#log4j.appender.xmlout.Directory=e\:\\logs\\surveillance.
#log4j.appender.xmlout.Prefix=surveillance
#log4j.appender.xmlout.Suffix=.log
log4j.appender.xmlout.Append=true
log4j.appender.xmlout.layout=org.apache.log4j.xml.XMLLayout
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
#log4j.appender.mail.To=dmitry.nikelshpur#moodys.com
#log4j.appender.mail.To=valeriy.vinnychuk#moodys.com
log4j.appender.mail.To=Moody's-SFGSMART#moodys.com
log4j.appender.mail.From="SFG SMART Application Mailer [Dmitry Nikelshpur]"
# Q2'12
#log4j.appender.mail.SMTPHost=mdynycmsxswp.moodys.com
log4j.appender.mail.SMTPHost=${SMTP.MAIL.SERVER}
# Q2'12
log4j.appender.mail.Threshold=ERROR
log4j.appender.mail.BufferSize=1
log4j.appender.mail.Subject=Test Surveillance logger email handler *** PLEASE DISREGARD ***
log4j.appender.mail.layout=com.moodys.sfg.smart.util.SMARTMailerHTMLLayout
log4j.additivity.mailLogger=true
log4j.appender.mail.layout.LocationInfo=true
# R3'13
# Setup GPSMSPLogger
log4j.appender.GPSMSPLogger=biz.minaret.log4j.DatedFileAppender
log4j.appender.GPSMSPLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.GPSMSPLogger.layout.ConversionPattern=%d [%t] %-5p %c %x %m%n
log4j.appender.GPSMSPLogger.Directory=${msp.log.directory}
log4j.appender.GPSMSPLogger.Prefix=gps_msp.
log4j.appender.GPSMSPLogger.Suffix=.log
log4j.appender.GPSMSPLogger.Threshold=DEBUG
below is the code used to log:
public class GPSMSPLogger {
public static Logger getLogger(){
return Logger.getLogger("GPSMSPLogger");
}
public static void main(String arg[]){
getLogger().info("Log example INFO.......... ");
}
}
You need to remove the trailing semicolons on these lines:
log4j.additivity.A1=false;
log4j.additivity.GPSMSPLogger=false;
The additivity value should be either true or false, not false;
Related
I'm using log4j2.
I have two appenders that write logs to two different files, and this works fine.
But these logs also get into the console appender.
I want to free the console from logs for files, and so that the rest of the logs get to the console.
How can I do that?
# Declare logger
name=LoggingConfig
appenders=a_console, a_test1, a_test2
loggers=l_project, l_test1, l_test2
# Root logger
rootLogger.level=INFO
rootLogger.appenderRefs=a_console
rootLogger.appenderRef.a_console.ref=STDOUT
# Project logger
logger.l_project.name=re.vianneyfaiv.log4j2
logger.l_project.level=DEBUG
logger.l_project.additivity=false
logger.l_project.appenderRef.a_console.ref=STDOUT
# Console logger
appender.a_console.type=Console
appender.a_console.name=STDOUT
appender.a_console.layout.type=PatternLayout
appender.a_console.layout.pattern=%d{ISO8601} [%t] %-5p (%F\:%L) - %m%n
# File logger - 1
appender.a_test1.type=RollingFile
appender.a_test1.name=FILE1
appender.a_test1.append=false
appender.a_test1.layout.type=PatternLayout
appender.a_test1.layout.pattern=%d{ISO8601} [%t] %-5p (%F\:%L) - %m%n
appender.a_test1.fileName=log4j2-sample-1.log
appender.a_test1.filePattern=log4j2-sample-1-%d{yyyy-MM-dd}.log
appender.a_test1.policies.type=Policies
appender.a_test1.policies.time.type=TimeBasedTriggeringPolicy
appender.a_test1.policies.time.interval=1
logger.l_test1.name=re.vianneyfaiv.log4j2.Log4J2Test1
logger.l_test1.level=ERROR
logger.l_test1.appenderRef.a_test1.ref=FILE1
# File logger - 2
appender.a_test2.type=RollingFile
appender.a_test2.name=FILE2
appender.a_test2.append=false
appender.a_test2.layout.type=PatternLayout
appender.a_test2.layout.pattern=%d{ISO8601} [%t] %-5p (%F\:%L) - %m%n
appender.a_test2.fileName=log4j2-sample-2.log
appender.a_test2.filePattern=log4j2-sample-2-%d{yyyy-MM-dd}.log
appender.a_test2.policies.type=Policies
appender.a_test2.policies.time.type=TimeBasedTriggeringPolicy
appender.a_test2.policies.time.interval=1
logger.l_test2.name=re.vianneyfaiv.log4j2.Log4J2Test2
logger.l_test2.level=INFO
logger.l_test2.appenderRef.a_test2.ref=FILE2
Just remove this console logger portion from properties file
# Console logger
appender.a_console.type=Console
appender.a_console.name=STDOUT
appender.a_console.layout.type=PatternLayout
appender.a_console.layout.pattern=%d{ISO8601} [%t] %-5p (%F\:%L) - %m%n
I'm add:
logger.l_test2.additivity=false
logger.l_test2.additivity=false
I am new to the Log4j framework and after reading some stuff i got some fare idea about logging mechanism, but still
i have some doubt about the following properties.
log4j.category.com.cloud.sample=INFO, file, C
log4j.additivity.com.cloud.sample=true
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.Target=System.out
log4j.appender.C.ImmediateFlush=true
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%-5p %d [%t] %m%n
#log4j.rootLogger=INFO, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
### direct messages to file ###
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}/var/basic/logs/sample.log
log4j.appender.file.Append=true
log4j.appender.file.MaxFileSize=10MB
# mylog.log.10 \u307e\u3067\u4fdd\u6301
log4j.appender.file.MaxBackupIndex=50
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p [%t] %c{1} - %m%n
log4j.rootLogger=INFO, C, file
In the first line of the above code contains two appenders(file, C) after that we will be having appender for both file and C. So as per my understanding logs will of category will be stored to Console and sample.log. Please let me know if i am wrong.
log4j.rootLogger=INFO, A1 and respective properties are not used right?
log4j.rootLogger=INFO, C, file: This line is about root logger, I think in my case it is not useful, because it is defined at the last line and there is no properties defined over here.
Please could any body confirm my understanding and suggest me if any changes required in the above configuration
The root logger resides at the top of the logger hierarchy. It is exceptional in three ways:
it always exists,
its level cannot be set to null
it cannot be retrieved by name.
The rootLogger is the father of all appenders. Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy (including rootLogger).
I am trying to create 2 log files using log4j using this configuration:
log4j.rootLogger=debug,stdout,logfile
#debug info warn error fatal
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache=FATAL,logfile
# ConsoleAppender properties
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.Threshold=debug
log4j.appender.stdout.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n
#%-5p [%t]: %m%n
# LogFileAppender properties
log4j.appender.logfile=com.xx.util.SizeRollingFileAppender
log4j.appender.logfile.File=log.log
log4j.appender.logfile.MaxFileSize=50240KB
log4j.appender.logfile.BackupsDirectory=log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %5p %c{1}:%L - %m%n
log4j.appedner.logfile.Threshold=error
# Log1FileAppender properties
log4j.appender.log1=com.xx.util.SizeRollingFileAppender
log4j.appender.log1.File=log1.log
log4j.appender.log1.MaxFileSize=5120KB
log4j.appender.log1.BackupsDirectory=log
log4j.appender.log1.layout=org.apache.log4j.PatternLayout
log4j.appender.log1.layout.ConversionPattern=%d %5p %c{1} :%L - %m%n
(the xx is for the project that I am working on) -
And here it is the Java Code:
public class TestLog4j {
/**
* #param args
*/
private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(TestLog4j.class);
private static org.apache.log4j.Logger log1= org.apache.log4j.Logger.getLogger("log1");
public static void main(String[] args) {
// TODO Auto-generated method stub
org.apache.log4j.PropertyConfigurator.configure("src/com/test/log4j.properties");
log.error("Error Test Log");
log1.info("Test Log1");
}
}
My problem is that I want to write two different files, one for log and one for log1. What I am getting with this code is only one file being created with both logs in the same file.
What I am doing wrong?
Short answer:
Add:
log4j.logger.log1=<yourLogLevel>,log1
to your log4j.properties.
NOTE:
Every enabled log-request to log1 (log-level >= yourLogLevel) will go to both stdout and logfile unless you turn off appender-additivity!
I'd like to write to two different files using my logger, which is declared like this:
public static final Logger logger = Logger.getLogger(Adapt.class);
PropertyConfigurator.configure("log4j.properties");
the file log4j contains:
log4j.rootLogger=DEBUG, FA
#File Appender
log4j.appender.FA=org.apache.log4j.FileAppender
log4j.appender.FA.File=temp.ppr
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.append=false
log4j.appender.FA.layout.ConversionPattern= %m%n
Is it possible at all to use logger to write different text to two different files easily?
If not, is there a way to do that with two loggers? (I tried that and got problems because of the function configure which is static.)
Thanks.
Just define a second logger variable:
Logger otherLogger = Logger.getLogger("OTHER_LOGGER");
define a configuration for it (notice the log4j.logger.OTHER_LOGGER syntax cf. log4j.rootLogger, as pointed out by user623395 and venkatesh Dodla):
log4j.logger.OTHER_LOGGER=DEBUG, OtherAppender
log4j.additivity.OTHER_LOGGER = false
#File Appender
log4j.appender.OtherAppender=org.apache.log4j.FileAppender
log4j.appender.OtherAppender.File=temp2.ppr
log4j.appender.OtherAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.OtherAppender.append=false
log4j.appender.OtherAppender.layout.ConversionPattern= %m%n
and log your different text as usual:
logger.debug("My normal log");
otherLogger.info("My special text");
it worked for me.
My Log file:
# log4j.properties
log4j.rootLogger=DEBUG,hfis,stdout
#For second log
log4j.logger.OTHER_LOGGER=DEBUG, OtherAppender
log4j.additivity.OTHER_LOGGER = false
#File Appender
log4j.appender.OtherAppender=org.apache.log4j.FileAppender
log4j.appender.OtherAppender.File=C:\\Legacy.log
log4j.appender.OtherAppender.ImmediateFlush=true
log4j.appender.OtherAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.OtherAppender.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss.SSS} [%t] %-5p %c - %m%n
log4j.rootCategory=ERROR
log4j.rootLogger.additivity=false
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.hfis=org.apache.log4j.RollingFileAppender
log4j.appender.hfis.Threshold=DEBUG
log4j.appender.hfis.file=C:\\hfis.log
log4j.appender.hfis.ImmediateFlush=true
log4j.appender.hfis.MaxFileSize=5MB
log4j.appender.hfis.layout=org.apache.log4j.PatternLayout
log4j.appender.hfis.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss.SSS} [%t] %-5p %c - %m%n
# R is the RollingFileAppender that outputs to a rolling log
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=DEBUG
# Define a pattern layout for the file.
log4j.appender.mystdout.layout=org.apache.log4j.PatternLayout
log4j.appender.mystdout.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss.SSS} %-5p: %m%n<br>
and my log variables are
final static Logger log = Logger.getLogger("hfis");
final static Logger log2 = Logger.getLogger("OTHER_LOGGER");
To create temp2.ppr, change from
log4j.OTHER_LOGGER=DEBUG, OtherAppender
to
log4j.**logger**.OTHER_LOGGER=DEBUG, OtherAppender
I'm wondering how to convert the following code to output those lines into a text file, and not to standard output:
import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator;
public class HelloWorld {
static final Logger logger = Logger.getLogger(HelloWorld.class);
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
logger.debug("Sample debug message");
logger.info("Sample info message");
logger.warn("Sample warn message");
logger.error("Sample error message");
logger.fatal("Sample fatal message");
}
}
The properties file is :
log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=%m%n
Thanks.
Change the ConsoleAppender to a FileAppender.
I find the org.apache.log4j.RollingFileAppender
to be useful.
If you use this,
you must add a property for the fileName and
may want to set the maxFileSize as well.
Here is an example (put these in the log4j.properties file):
log4j.appender.NotConsole=org.apache.log4j.RollingFileAppender
log4j.appender.NotConsole.fileName=/some/path/to/a/fileName.log
log4j.appender.NotConsole.maxFileSize=20MB
There are other appenders.
DailyRollingFileAppender rolls based on time.
FileAppender does not roll.
If you use the RollingFileAppender,
you will need to guess as to a good value for maxFileSize and
then address the size at a future date if it is causing issues.
Shortly use FileAppender instead of ConsoleAppender.
Here is a simple example of configuration. It additionally configures the layout. You can omit it for the first approach.
log4j.appender.F=org.apache.log4j.FileAppender
log4j.appender.F.File=mylog.log
log4j.appender.F.layout=org.apache.log4j.PatternLayout
log4j.appender.F.layout.ConversionPattern=%d{MM-dd#HH:mm:ss} %-5p (%13F:%L) %3x - %m%n
The following would be helpful:
Class containing main method
package abc;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class ClassOne {
static Logger logger = Logger.getLogger(ClassOne.class);
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.info"); //log4j.info file should be in the class path(same location as ClassOne.class)
logger.info("Program started.... "); //Whenever you want to write something to the log text file use logger.info("Log Message")
}
}
log4j.info file
log4j.rootLogger=INFO, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender // Replacing ConsoleAppender with FileAppender gives text file logging
# Set the name of the file
log4j.appender.FILE.File=src/abc/log.out //Here you can specify either absolute or relative path
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d %m%n
following configuration should aslo work
direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.FileAppender
log4j.appender.stdout.fileName=error.log
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p
%c{1}:%L - %m%n
in log4j.properties
# Define the root logger with file appender
log4j.logger.App = DEBUG ,CA
#set file text
log4j.appender.CA = org.apache.log4j.RollingFileAppender
log4j.appender.CA.File = D:\\database.log
log4j.appender.CA.maxFileSize = 20MB
log4j.appender.CA.MaxBackupIndex=10
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n