Log4j: how to compose config? - java

I added Log4j2 to the project, and added config.
# 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=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
# 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
Than, I log code like here:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
...
final static Logger logger = LogManager.getLogger(AbstractEditor.class);
...
logger.info("updated: " + entity);
logger.debug("==> debug");
logger.info("==> info");
logger.warn("==> warn");
logger.error("==> error");
logger.fatal("==> fatal");
logger.trace("==> trace");
As I understand, all logs with level higher than DEBUG must be written to console and file. But only this has been printed into console:
15:08:52.285 [http-nio-8080-exec-1] ERROR ru.example.AbstractEditor - ==> error
15:08:52.292 [http-nio-8080-exec-1] FATAL ru.example.AbstractEditor - ==> fatal
I see this strings not matches my config. And they are not witten into file. When I added this config, all logs disappeared from console, excluding this 2 strings.
Please help to write config to see all logs with level from DEBUG on console and file.

You are programmatically using Log4j 2 but using the configuration format of Log4j 1. Log4j 2 is ignoring your configuration and using the default. You have 2 choices.
Convert your configuration to use Log4j 2 syntax (recommended), or
Enable Log4j 2's experimental support for log4j 1 configuration files by setting the system property "log4j1.compatibility=true". This support was added in release 2.13.0 so you would have to be using that version. The property file would also have to be named log4j.properties, not log4j2.properties.

Related

Multiple process using the same log4j.properties

I'm actually on a project using slf4j/log4j.
For that, we use log4j.properties files to configure the logging, especially the DailyRollingFileAppender.
# Root logger option
log4j.rootLogger=INFO, file, stdout
log4j.logger.com.thales.ecosystem=DEBUG,stdout,file
log4j.additivity.com.thales.ecosystem=false
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
# this is set dynamically
log4j.appender.file.File=${log.basedir}/decoders/nm-flight-decoder.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.ImmediateFlush=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} v${release.version} %-5p %c{1}:%L - %m%n
log4j.appender.file.Append=true
# 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.SSS} v${release.version} %-5p %c{1}:%L - %m%n
And in code, we use the loggerFactory :
private static final Logger LOGGER = LoggerFactory.getLogger(xxxxxxxx.class);
But we have now to deal with multiple (but limited in number) identical process.
So the question is : how can we have two process (with the same class)
with only one log4j.properties per class, writting on a file each, and keeping the DailyRollingFileAppender ?
Thank's !
Make the file name in the configuration a property, and start each process with a different value for that property.
log4j.appender.file.File=${log.basedir}/decoders/${proc}.log
Start one process with -Dproc=nm-flight-decoder, and the other one with a different value.
(Also, consider upgrading to Log4j2. Log4j 1.2 has been End of Life since summer 2015 (archived here) and is known to be broken on Java 9 (archived here).)

Do i have to create appenders for all classes in log4j?

I'm using log4j for my final project. I tested it using single class before I used log4j in my project. That worked fine.
Then I added log4j to my project and I used it in many classes using:
final static Logger logger = Logger.getLogger(Supplier.class);
Then I got a warning:
log4j:WARN No appenders could be found for logger (classes.Supplier).
log4j:WARN Please initialize the log4j system properly.
This is my properties file:
# Root logger option
log4j.rootLogger=DEBUG, file
log4j.rootLogger=DEBUG, errorfile
# Redirect log messages to console
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.errorfile=org.apache.log4j.RollingFileAppender
# Redirect log messages to a log file, support file rolling.
log4j.appender.file.File=C:\\HardwareLog\\INFO.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
log4j.appender.errorfile=C:\\HardwareLog\\ERROR.log
log4j.appender.errorfile.Threshhold=ERROR
log4j.appender.errorfile.MaxFileSize=5MB
log4j.appender.errorfile.MaxBackupIndex=10
log4j.appender.errorfile.layout=org.apache.log4j.PatternLayout
log4j.appender.errorfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Do I have to create appenders for each class to use it?
Can I use one properties file in different packages ?
I found a solution. I removed root logger and added two loggers for my two packages.
log4j.logger.package1=DEBUG,FIRST
log4j.logger.package2=DEBUG,SECOND
now its working fine.
you can go for root logger that will log for the entire application.
# Root logger option
log4j.rootLogger=INFO, file
plese go through this link
https://www.mkyong.com/logging/log4j-log4j-properties-examples/

Sending log to email using log4j SMTPAppender

I have a Spring Boot application. I'm trying to send log to mail using SMTPAppender, but I don't get any mails. I've managed to use log4j for ConsoleAppender and RollingFileAppender, so I guess I have log4j added properly, and the file with log4j properties is visible.
Here is my log4j.properties file:
log4j.rootLogger = info, email, stdout, file
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.SMTPHost=smtp.google.com
log4j.appender.email.SMTPUsername=xyz#gmail.com
log4j.appender.email.SMTPPassword=abc123
log4j.appender.email.From=xyz#gmail.com
log4j.appender.email.To=xyz#gmail.com
log4j.appender.email.Subject=Log
log4j.appender.email.BufferSize=1
log4j.appender.email.EvaluatorClass=TriggerLogEvent
log4j.appender.email.layout=org.apache.log4j.PatternLayout
log4j.appender.email.layout.ConversionPattern=%m
#------------------------------------------------------------------------------------------
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#------------------------------------------------------------------------------------------
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=mylog.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
I found a important piece from the documentation about SMTPAppender.
By default, an email message will be sent when an ERROR or higher severity message is appended. The triggering criteria can be modified by setting the evaluatorClass property with the name of a class implementing TriggeringEventEvaluator, setting the evaluator property with an instance of TriggeringEventEvaluator or nesting a triggeringPolicy element where the specified class implements TriggeringEventEvaluator
Also, you can't use "smtp.google.com" - it should be smtp.gmail.com
You should use GmailSMTPAppender, the details are here -
http://www.tgerm.com/2010/05/log4j-smtpappender-gmail-custom.html

Log4j Log hibernate specific messages to separate file

I want my Hibernate specific logs to go to a separate file using log4j.
I tried this:
# Root logger option--First parameter is logLevel, second says redirect to file , third says redirect to standard output alos
log4j.rootLogger=INFO, file, stdout
# Log everything. Good for troubleshooting
log4j.logger.org.hibernate=INFO,hblog
# Log all JDBC parameters
log4j.logger.org.hibernate.type=ALL
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=D:\\a2bnextLogs\\a2bnext.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.DailyRollingFileAppender
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
# Direct hibernate messages to a log file
log4j.appender.hblog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.hblog.File=D:\\a2bnextLogs\\hibernate.log
log4j.appender.hblog.layout=org.apache.log4j.PatternLayout
log4j.appender.hblog.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
But this doesent seem to work.Could anyone suggest what changes I need to make in above configuration?

Different logging appender on LOG4J

I'm using log4j 1.2 and I need to:
log everything (including logging from referenced libraries) to console
log from my code to file (and maybe to console)
Using the following code:
log4j.rootLogger=DEBUG, CONSOLE
log4j.com.mypackage=ALL, CONSOLE, CSV
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.err
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n
log4j.appender.CSV=org.apache.log4j.FileAppender
log4j.appender.CSV.File=./myfile.csv
log4j.appender.CSV.Append=false
log4j.appender.CSV.layout=org.apache.log4j.PatternLayout
log4j.appender.CSV.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p - %m%n
The logfile is not created.
Other try I done:
If I add CSV appender to rootLogger, then the file is created and filled.
If I add CSV appender to rootLogger and disable it, then the file is just created.
If I log only my logger to console... the it works fine
Do you have any idea to solve?
Thanks
You are missing an important part.
The value has to be
log4j.logger.com.mypackage
Note that you are missing logger in the log definition

Categories