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).)
Related
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.
Currently my code is printing logs on the console, yet it's not creating a file with the logs. This is the code of log4j.properties:
log4j.rootLogger=file, stdout
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=C:/logs/logging.log
log4j.appender.file.ImmediateFlush=true
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} %L - %m%n
log4j.appender.file.Append=true
log4j.appender.file.Threshold=INFO
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} %m%n
It's in the resource folder of my spring application.
And this is where i use it:
public void getPreviousDay() {
PropertyConfigurator.configure("log4j.properties");
logger = Logger.getLogger(LocationScheduler.class);
logger.info("Test");
}
There are couple of changes you need to make to your log4j.properties
First of all log4j.rootLogger specifies Log Level and than the appender. so in your case it has to be
log4j.rootLogger= INFO, file, stdout
Instead of log4j.appender.file=org.apache.log4j.FileAppender use log4j.appender.file=org.apache.log4j.RollingFileAppender in order to get a rollover logfile.
Hope this helps.
EDIT
It seems your program is not able to reach your log4j.properties and the reason is the method you are using for loading it only takes a fully qualified path of the log4j.properties
If you want to load your log4j.properties from classpath use below version:
PropertyConfigurator.configure(ClassLoader.getSystemResource("log4j.properties"));
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?
I am using below log4j.properties. I want to create file minute basis. I checked many post on this same question and tried but it didnt worked for me. I am using log4j first time, Please suggest if I am doing something wrong in properties file.
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %-5p [%c{1}] %m %n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=/opt/aTest/log.out
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern="%d %-5p [%c{1}] %m %n
log4j.appender.DailyRoller=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DailyRoller.datePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.DailyRoller.file=/opt/aTest/log.out
log4j.appender.DailyRoller.layout=org.apache.log4j.PatternLayout
log4j.appender.DailyRoller.layout.ConversionPattern=%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n %-5p %m%n
log4j.appender.LOGFILE=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.LOGFILE.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.LOGFILE.RollingPolicy.FileNamePattern=/opt/aTest/log_%d{yyyy-MM-dd-HH}.out
I am getting only one log.out file created and for each run log gets appended in same file.
You should try the XML format for configuration; the properties files are hard to understand, easy to get wrong (as you can tell :-)
The problem here is that it's not enough to define an appender, you also have to tell log4j to use it:
log4j.rootLogger=debug, stdout, DailyRoller
Also DailyRoller is a confusing name for a per-minute rolling appender.
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