I currently have the below pattern layout in log4j. I want to add the Process id to the log file. How can I do it?
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
Pasted sample log message
2011-01-07 11:48:21,940 [main] INFO Testing1
2011-01-07 11:48:21,942 [main] INFO Test.common.ApplicationProperties - Used log4j
log4j.properties
"log4j.properties" [Read only] 26 lines, 884 characters
log4j.rootCategory=DEBUG, stdout, A1
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=WARN
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss} %-5p (%c) %m%n
log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.Threshold=DEBUG
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.A1.File=/homw/cus/logs/ccl.02.log
log4j.appender.A1.MaxFileSize=5MB
log4j.appender.A1.MaxBackupIndex=40
log4j.category.test.common.DBConnectionPool=WARN
log4j.category.test.common.DataBaseHandler=WARN
log4j.category.test.cttg.tables=WARN
log4j.category.test.middleware.tables=WARN
log4j.logger.org.apache.axis=ERROR
log4j.logger.org.apache.catalina=ERROR
You should use MDC to do it
In the config file :
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss} %-5p (%c) %m%n %X{PID}
%X{PID} is used to match the context value PID
And then, in the code, before the logging begins :
log4j 1.x
RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
String pid = rt.getName();
MDC.put("PID", pid);
log4j 2.x
RuntimeMXBean rt = ManagementFactory.getRuntimeMXBean();
String pid = rt.getName();
ThreadContext.put("PID", pid);
There is no way of doing it using standard Java classes. Generally process ID is appended at file level not at the log level. And here (archived here) is an example of doing it.
With the following pattern, showing threadID and class. If you want to see the Process ID, might check here
log4j.appender.SYSLOG.layout.conversionPattern=%-5p %d{ddMMyyyy HH:mm:ss.SSS} [%t:%c] %m%n
I managed to do so, but I have several appenders, one for each part of the application, like the following:
log4j.rootCategory=ERROR, SYSLOG2
log4j.logger.com.myself.logic=DEBUG, SYSLOG
log4j.logger.com.myself.database=DEBUG, SYSLOG3
log4j.logger.com.myself.other=DEBUG, SYSLOG
Here are some examples of generated logs, to check if this is what you require:
INFO 20012015 11:56:17.318 [pool-1-thread-1:com.myself.logic.MonitoringTask] 10602: Validation of orders before UTC=2015-01-20T16:56:17
INFO 20012015 11:56:34.200 [main:com.gmv.pazgs.mus.mpu.CLibWrapper] 10402: MPU Library folder: xxxxx
INFO 20012015 11:56:34.209 [main:com.gmv.pazgs.mus.mpu.CLibWrapper] 10402: MPU Configuration folder: xxxxx
INFO 20012015 11:56:34.773 [main:com.gmv.pazgs.mus.mpu.CLibWrapper] 10402: Calling InitLeap()
INFO 20012015 11:56:34.786 [main:com.gmv.pazgs.mus.mpu.CLibWrapper] 10402: Calling InitFourier()
INFO 20012015 11:57:10.151 [CRON_Thread:com.myself.other.DTOLDispatcher] 10202: Times to send: [11:00, 23:00] - current time = Tue Jan 20 11:57:10 UTC 2015
INFO 20012015 11:58:10.165 [pool-1-thread-4:com.myself.logic.MonitoringTask] 10602: Executing Monitoring Task UTC=2015-01-20T11:58:10
INFO 20012015 11:58:10.171 [pool-1-thread-5:com.myself.logic.OrderValidationTask] 10602: Executing OrderValidationTask UTC=2015-01-20T11:58:10
INFO 20012015 11:58:10.291 [CRON_Thread:com.myself.other.DTOLDispatcher] 10202: Times to send: [11:00, 23:00] - current time = Tue Jan 20 11:58:10 UTC 2015
INFO 20012015 11:58:10.684 [pool-1-thread-4:com.myself.logic.MonitoringTask] 10602: Expiration of orders before UTC=2015-01-20T11:58:10
INFO 20012015 11:58:11.218 [pool-1-thread-4:com.myself.logic.MonitoringTask] 10602: checking orders for suppresed status before UTC=2015-01-20T11:58:11
INFO 20012015 11:58:11.244 [pool-1-thread-4:com.myself.logic.MonitoringTask] 10602: Validation of orders before UTC=2015-01-20T16:58:11
Related
I want the log file to be empty every time I start the application. But I cannot find a way to do so. There is nothing stated as such in the Quarkus documentation (https://quarkus.io/guides/logging#quarkus-log-logging-log-config_quarkus.log.file-file-logging).
I would really appreciate if someone can tell me how to do it or suggest a workaround in case it is not possible.
This is the logging configurations in the application properties file
# Log
quarkus.log.category."com.helesto".level=DEBUG
quarkus.log.level=INFO
# Log File
quarkus.log.file.enable = true
quarkus.log.file.format = %d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n
quarkus.log.file.path = logs/test.log
I configured quarkus to begin a new file on reboot.
As documented here you need to either set a max-file-size or configure a file-suffix for it to work.
This is my working configuration:
quarkus.log.file.enable= true
quarkus.log.file.path= /log/backend.log
quarkus.log.file.level= DEBUG
quarkus.log.file.format= %d{HH:mm:ss} %-5p [%c{2.}] (%t) %s%e%n
quarkus.log.file.rotation.file-suffix= .yyyy-MM-dd
quarkus.log.file.rotation.rotate-on-boot= true
quarkus.log.file.rotation.max-backup-index= 31
with this configuration it produces a daily log and begin a new file on reboot up to maximum 31 files.
The API I am working on cannot be connected to a database, but need to log events that are happening in the API. To do this I was thinking on using log4j to create log file with API event information.
The problem is that all log entries end up in both logs, and not separated.
Requirements I am need to fulfill
Multiple log files with certain information inside
Backup log files live indefinitely
Log4j properties file
log4j.rootLogger=QuietAppender, LoudAppender, FirstLog, SecondLog, TRACE
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n
# setup A1
log4j.appender.QuietAppender=org.apache.log4j.RollingFileAppender
log4j.appender.QuietAppender.Threshold=INFO
log4j.appender.QuietAppender.File=${wls.logs-path}/test-api/test-api-info.log
log4j.appender.QuietAppender.MaxFileSize=512KB
log4j.appender.QuietAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.QuietAppender.layout.ConversionPattern=%d %p [%c] - %m%n
# Keep three backup files.
log4j.appender.QuietAppender.MaxBackupIndex=100
# Pattern to output: date priority [category] - message
log4j.appender.QuietAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.QuietAppender.layout.ConversionPattern=%d %p [%c] - %m%n
# setup A2
log4j.appender.LoudAppender=org.apache.log4j.RollingFileAppender
log4j.appender.LoudAppender.Threshold=DEBUG
log4j.appender.LoudAppender.File=${wls.logs-path}/test-api/test-api-debug.log
log4j.appender.LoudAppender.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.LoudAppender.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.LoudAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.LoudAppender.layout.ConversionPattern=%d %p [%c] - %m%n
# setup FirstLog
log4j.appender.FirstLog=org.apache.log4j.RollingFileAppender
log4j.appender.FirstLog.Threshold=INFO
log4j.appender.FirstLog.File=${wls.logs-path}/test-api/first-info.log
log4j.appender.FirstLog.MaxFileSize=10240kB
log4j.appender.FirstLog.MaxBackupIndex=99999
log4j.appender.FirstLog.layout=org.apache.log4j.PatternLayout
log4j.appender.FirstLog.layout.ConversionPattern=%d %p [%c] - %m%n
# setup SecondLog
log4j.appender.SecondLog=org.apache.log4j.RollingFileAppender
log4j.appender.SecondLog.Threshold=INFO
log4j.appender.SecondLog.File=${wls.logs-path}/test-api/second-info.log
log4j.appender.SecondLog.MaxFileSize=10240kB
log4j.appender.SecondLog.MaxBackupIndex=99999
log4j.appender.SecondLog.layout=org.apache.log4j.PatternLayout
log4j.appender.SecondLog.layout.ConversionPattern=%d %p [%c] - %m%n
Java Class
private static final Logger logkp = Logger.getLogger("FirstLog");
private static final Logger logda = Logger.getLogger("SecondLog");
logkp.info(sb.toString());
logda.info(sb.toString());
Current Results
2015-05-27 10:27:46,175 INFO [SecondLog] - 12645,APIServer1,0,000bdc5000000100011055042d0114a6
2015-05-27 10:27:46,583 INFO [FirstLog] - APIServer1,Caller,test-Version-1.0,certValue,1
2015-05-27 10:28:22,458 INFO [SecondLog] - 12645,APIServer1,0,000bdc5000000100011055042d0114a6
2015-05-27 10:28:22,793 INFO [FirstLog] - APIServer1,Caller,test-Version-1.0,certValue,1
2015-05-27 10:28:25,203 INFO [SecondLog] - 12645,APIServer1,0,000bdc5000000100011055042d0114a6
2015-05-27 10:28:25,528 INFO [FirstLog] - APIServer1,Caller,test-Version-1.0,certValue,1
2015-05-27 10:28:26,686 INFO [SecondLog] - 12645,APIServer1,0,000bdc5000000100011055042d0114a6
I'm not 100% sure about this, because it's been a while that I was using log4j and we used to write the configuration in xml, but I think you have to create loggers like this:
log4j.rootLogger=QuietAppender, LoudAppender, TRACE
log4j.logger.FirstLogger = FirstLog, INFO
log4j.additivity.FirstLogger = false
log4j.logger.SecondLogger = SecondLog, INFO
log4j.additivity.SecondLogger = false
... // then configure appenders as you did
To get the outputs you want. Setting the additivity to false "cuts" the connection to the rootLogger. It is set to true by default and will cause all logmessages to be appended to the logger and to all ancestors of the logger. Setting it to false will change that.
If your API has its own namespace - let's say "my.own.API" then you could also create an API-Logger like this:
log4j.logger.my.own.API = MyAPIAppender, INFO
log4j.additivity.my.own.API = false
And create loggers like this:
package my.own.API
public class MyAPIClass{
private static Logger apiLog = Logger.getLogger(MyAPIClass.class);
// ...
}
I want to create 2 types of log 1 Debug log which will create all log another I want to create activity log I mean each method how much time took to execute or any specific info,
I am using below log4j property file-
please correct me, its logging all messages in only one file,in java I have instantiated both log object, kindly don't send any pointers or just Google because I have tried since last 2 days all options as described in Google,
Thanks in advance for your kind support,
log4j.rootLogger=debugLog,reportsLog
log4j.appender.debugLog=org.apache.log4j.FileAppender
log4j.appender.debugLog.File=logs/debug.log
log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout
log4j.appender.debugLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n
log4j.appender.reportsLog=org.apache.log4j.FileAppender
log4j.appender.reportsLog.File=logs/reports.log
log4j.appender.reportsLog.layout=org.apache.log4j.PatternLayout
log4j.appender.reportsLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n
log4j.category.debugLogger=INFO, debugLog
log4j.additivity.debugLogger=false
log4j.category.reportsLogger=DEBUG, reportsLog
log4j.additivity.reportsLogger=false
Below log4j.properties file will configure the logger to log the messages with debug level to the logs/debug.log file. Messages with level INFO...FATAL are logged to logs/reports.log.
log4j.rootLogger=DEBUG, debugLog, reportsLog
log4j.appender.debugLog=org.apache.log4j.FileAppender
log4j.appender.debugLog.File=logs/debug.log
log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout
log4j.appender.debugLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n
log4j.appender.debugLog.filter.f1=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.debugLog.filter.f1.LevelMax=DEBUG
log4j.appender.debugLog.filter.f1.LevelMin=DEBUG
log4j.appender.reportsLog=org.apache.log4j.FileAppender
log4j.appender.reportsLog.File=logs/reports.log
log4j.appender.reportsLog.layout=org.apache.log4j.PatternLayout
log4j.appender.reportsLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n
log4j.appender.reportsLog.filter.f1=org.apache.log4j.varia.LevelRangeFilter
log4j.appender.reportsLog.filter.f1.LevelMax=FATAL
log4j.appender.reportsLog.filter.f1.LevelMin=INFO
log4j.category.debugLogger=DEBUG, debugLog
log4j.additivity.debugLogger=false
log4j.category.reportsLogger=INFO, reportsLog
log4j.additivity.reportsLogger=false
I am getting duplicate entries in my log file.
Have attached my log4j.properties below.
log4j.properties:
###############################################################################
# log4j Configuration file: Defines following loggers
# SL - Standard root Logger
# EL - Error Logger with the threshold level explicitly set to ERROR
# DL - Data base logger - to log db queries separately
# BL - Batch logger
###############################################################################
log4j.rootLogger=TRACE,SL,EL
log4j.rootLogger.additivity=false
#Standard Log
log4j.appender.SL=org.apache.log4j.DailyRollingFileAppender
log4j.appender.SL.File=${log.file}/log.log
log4j.appender.SL.layout=org.apache.log4j.PatternLayout
log4j.appender.SL.layout.ConversionPattern=[%5p] [%t %d{HH:mm:ss:SSS}] [%X{sessionId}:%X{hostId}:%X{userId}] (%F:%M:%L) %m%n
#Error Log
log4j.appender.EL=org.apache.log4j.DailyRollingFileAppender
log4j.appender.EL.File=${log.file}/error.log
log4j.appender.EL.layout=org.apache.log4j.PatternLayout
log4j.appender.EL.Threshold=ERROR
log4j.appender.EL.layout.ConversionPattern=[%5p] [%t %d{HH:mm:ss:SSS}] [%X{sessionId}:%X{hostId}:%X{userId}] (%F:%M:%L) %m%n
# Database Log
log4j.logger.org.springframework.jdbc=DEBUG,DL
log4j.appender.DL=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DL.File=${log.file}/db.log
log4j.appender.DL.layout=org.apache.log4j.PatternLayout
log4j.appender.DL.layout.ConversionPattern=[%5p] [%t %d{HH:mm:ss:SSS}] [%X{sessionId}:%X{hostId}:%X{userId}] (%F:%M:%L) %m%n
#Forecast Log
log4j.appender.MAPS_FC=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MAPS_FC.File=${log.file}/forecast.log
log4j.appender.MAPS_FC.layout=org.apache.log4j.PatternLayout
log4j.appender.MAPS_FC.layout.ConversionPattern=[%5p] [%t %d{HH:mm:ss:SSS}] [%X{sessionId}:%X{hostId}:%X{userId}] (%F:%M:%L) %m%n
#Logger configuration
log4j.logger.com.singaporeair.maps=TRACE,SL,EL
log4j.logger.com.singaporeair.maps.app.service.impl.gantt=DEBUG,MAPS_FC
log4j.logger.com.singaporeair.maps.app.dao.impl.gantt=DEBUG,MAPS_FC
Getting dulicate entries in log.log file configured above.
Log extract:
[ INFO] [SimpleAsyncTaskExecutor-9 19:04:00:800] [::] (AppProfiler.java:doProfile:69) Processing Time(ms): BaseDAOImpl: getBatchDetails: 63
[ INFO] [SimpleAsyncTaskExecutor-9 19:04:00:800] [::] (AppProfiler.java:doProfile:69) Processing Time(ms): BaseDAOImpl: getBatchDetails: 63
[ INFO] [SimpleAsyncTaskExecutor-9 19:04:00:800] [::] (AppProfiler.java:doProfile:71) BaseDAOImpl: getBatchDetails: OUT
[ INFO] [SimpleAsyncTaskExecutor-9 19:04:00:800] [::] (AppProfiler.java:doProfile:71) BaseDAOImpl: getBatchDetails: OUT
Pls help
If you turn off additivity, the loggers that are children of the parents won't cause double logging. For instance:
#Logger configuration
log4j.logger.com.singaporeair.maps=TRACE,SL,EL
log4j.additivity.com.singaporeair.maps=false
log4j.logger.com.singaporeair.maps.app.service.impl.gantt=DEBUG,MAPS_FC
log4j.additivity.com.singaporeair.maps.app.service.impl.gantt=false
log4j.logger.com.singaporeair.maps.app.dao.impl.gantt=DEBUG,MAPS_FC
log4j.additivity.com.singaporeair.maps.app.dao.impl.gantt=false
Probably would be helpful for those who experience duplicate problem in a multithread application (couldn't find the answer in google):
This happens when one thread is done and another thread open logger to the same log file which the first thread used to write.
.removeAllAppenders() before I added a new appender helped to resolve the issue.
com.singaporeair.maps is a superset of com.singaporeair.maps.app.service.impl.gantt and com.singaporeair.maps.app.dao.impl.gantt
Everything that matches com.singaporeair.maps.app.dao.impl.gantt will also match com.singaporeair.maps which will result in 2 log entties.
Guess 1: You need to turn off appender inheritance. It appears that this is wrong.
Guess 2: The root logger and the com.singaporeair.maps are both logging to the SL and EL appenders. This is just a guess, but try changing this:
log4j.logger.com.singaporeair.maps=TRACE,SL,EL
to this:
log4j.logger.com.singaporeair.maps=TRACE
I'm trying to log certain info messages onto a file but as soon as I run the application both warn and info messages are logged. Now, from what I've read from this site, you cannot log one without logging the other. Has anyone tried this before? If so, how did your properties file look like?
My properties file looks like this:
***** Set root logger level to INFO and its two appenders to stdout and R.
log4j.rootLogger=INFO, stdout, R
# ***** stdout is set to be a ConsoleAppender.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# ***** stdout uses PatternLayout.
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# ***** Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%M has started] (%F:%L) - %m%n
/
# ***** R is set to be a RollingFileAppender.
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.R.File="folder where log will be saved"
log4j.appender.R.layout.ConversionPattern=%5p [%m has started] %c{2}.[%x] (%F:%L) %d{yyyy-MM-dd HH:mm:ss} - %m%n
# ***** R uses PatternLayout.
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%5p [%m has started] %c{2}.[%x] (%F:%L) %d{yyyy-MM-dd HH:mm:ss} - %m%n
AFAIK there's no standard way to suppress higher log levels than those you are interested in.
However, you might be able to use a custom appender to do that.
It might look similar to this:
public class MyAppender extends AppenderSkeleton {
protected void append(LoggingEvent event) {
if( event.getLevel() == Level.INFO ) {
//append here, maybe call a nested appender
}
}
}
The log level WARN is higher than INFO, and the logging configuration defines the minimum threshold level to be logged by the appender. So, all messages higher than that level will also be logged.
Hence, the WARN messages are expected. And I don't think you can configure it to the way you want.
If the WARN messages that should not be printed come from a different package than the INFO messages, then you can define different log levels for these packages. The first can specify level ERROR and the second INFO
it should look something like this:
log4j.logger.com.test.something=ERROR
log4j.logger.com.other.package=INFO
cheers
Why do you want to filter the WARN level? As peshkira said, you could use two different loggers for splitting/filtering your log output, or you could use tools like grep for filtering (offline) or you could simply remove the WARN logs from your code if your don't need them anyway.
As far as I understand, advanced filters like LevelMatchFilter and LevelRangeFilter can do the trick for you.
Thing worth keeping in mind though is that using these may require xml config instead of properties: Can't set LevelRangeFilter for log4j