Why I have question instead line number when logging with log4j - groovy

I try to configure logger to write line number but instead of it I have question.
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t]:%L - %m%n
I figured out that it is not log4j problem but setting of my project. The most probably missing debug information in my compiled artifacts. (Line number not showing?) But how can I set that in IntellijIdea 2019.1?

Related

My requirement is I want my application to create new file after reaching the limit and max backup index ? I am using log4j

My log4j.properties have below defined properties. I want my app to create a new file after reaching the maximum limit
log4j.logger.file=DEBUG, fileAppender
log4j.logger.error=DEBUG, errorAppender
log4j.additivity.file=false
log4j.additivity.admin=false
log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.fileAppender.File=./log/PayHubUI.log
log4j.appender.fileAppender.MaxFileSize=10MB
log4j.appender.fileAppender.MaxBackupIndex=10
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n
log4j.appender.errorAppender=org.apache.log4j.RollingFileAppender
log4j.appender.errorAppender.File=./log/PayHubUIerror.log
log4j.appender.errorAppender.MaxFileSize=10MB
log4j.appender.errorAppender.MaxBackupIndex=10
log4j.appender.errorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.errorAppender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m
If you are using log4j-1.2.11 or earlier, then you will need to create the folder ./log yourself, else log4j will fail to create the log file. In later versions, log4j creates it for you. Once log file reach to maximum limit, current file rename and new file will be create.

log4j appender with a file url

I have a problem where log4j is not logging to one of my files and i'm not sure why. I have some code that runs scripts, the scripts can add logging which is logged to a file using log4j, I am trying to create an appender that only logs for a particular script.
log4j.logger.com.my.class=INFO, JS_LOG
log4j.appender.JS_LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.JS_LOG.Encoding=UTF-8
log4j.appender.JS_LOG.File=${log.outputdir}/js_service.log
log4j.appender.JS_LOG.MaxFileSize=2MB
log4j.appender.JS_LOG.MaxBackupIndex=10
log4j.appender.JS_LOG.Append=true
log4j.appender.JS_LOG=org.apache.log4j.RollingFileAppender
log4j.appender.JS_LOG.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
the above appender works and i get this in my log file
2012-04-18 11:25:52,043 [<MD> Inc Msg Dispatch-1 New] INFO com.my.class.file:/myfile - info
when the script logs something it logs to a logger of the form
com.my.class.file:/myfile this appears in the log using the above config in my log4j.properties file.
if I change the above to:
log4j.logger.com.my.class.file:/myfile=INFO, JS_LOG
log4j.appender.JS_LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.JS_LOG.Encoding=UTF-8
log4j.appender.JS_LOG.File=${log.outputdir}/js_service.log
log4j.appender.JS_LOG.MaxFileSize=2MB
log4j.appender.JS_LOG.MaxBackupIndex=10
log4j.appender.JS_LOG.Append=true
log4j.appender.JS_LOG=org.apache.log4j.RollingFileAppender
log4j.appender.JS_LOG.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
nothing is logged to the file. I would have thought this should work? I retrieve the file
I managed to solve this problem, its something to do with the file: in my log4j.properties I changed the appender to file_ then in my code I do some manipulation to the file url to make it match the appender.

logging configuration with log4j.properties, cannot exclude package from logging to console

i have the following log4j.properties file. what i want to do is log everything at the level INFO or above to the console. however, for anything that falls in the demo.* package, i want to log everything at the DEBUG level to a file. my log4j.properties below does not seem to work. what happens is that any DEBUG message from demo.* still gets output to the console. any idea what i am doing wrong?
how can i exclude demo.* from logging to the console?
log4j.rootLogger=INFO, C
log4j.logger.demo=DEBUG, R1
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.target=System.err
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
log4j.appender.R1=org.apache.log4j.RollingFileAppender
log4j.appender.R1.File=output.log
log4j.appender.R1.MaxFileSize=5MB
log4j.appender.R1.MaxBackupIndex=5
log4j.appender.R1.layout=org.apache.log4j.PatternLayout
log4j.appender.R1.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
sorry, never mind. i solved it by setting the threshold of the console appender.
log4j.appender.C.threshold=INFO

log4j: Rolling logs into .gz file with DailyRollingFileAppender using TimeBasedRollingPolicy

The current system is working as expected and the log files are rolled once an hr according to the logic below:
log4j.appender.oozie=org.apache.log4j.DailyRollingFileAppender
log4j.appender.oozie.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.oozie.File=${oozie.log.dir}/oozie.log
log4j.appender.oozie.Append=true
log4j.appender.oozie.layout=org.apache.log4j.PatternLayout
log4j.appender.oozie.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n
I can understand that rolling of log file directly into a compressed file(.gz or .zip) is possible with RollingFileAppender(rolling based on file size) using TimeBasedRollingPolicy. I'm using DailyRollingFileAppender(time based rolling) and would like to achieve compression with that. I modified my properties as follows. But that doesnt work.
log4j.appender.oozie=org.apache.log4j.DailyRollingFileAppender
log4j.appender.oozie.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.oozie.File=${oozie.log.dir}/oozie.log
log4j.appender.oozie.Append=true
log4j.appender.oozie.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.oozie.RollingPolicy.FileNamePattern=foo.%d{yyyy-MM-dd-HH}.gz
log4j.appender.oozie.layout=org.apache.log4j.PatternLayout
log4j.appender.oozie.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n
Any pointers would be highly appreciated. Here are some relevant links.
Configuring RollingFileAppender in log4j
http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html
Pardon my surprise, but at the exact link you gave above it says in the accepted answer:
Note that TimeBasedRollingPolicy can only be configured with xml, not log4j.properties
Have you tried rewriting your configuration to XML format?
please try the following, it works for me:
log4j.rootLogger=CONSOLE,file
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE..layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.Threshold=WARN
log4j.appender.file.Encoding=UTF-8
log4j.appender.file.File=/path to your logs/logs/log_file.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.file.RollingPolicy.FileNamePattern=/path to your logs/logs/log_file%d{yyyy-MM-dd-HH-mm-ss}.log
log4j.appender.file.layout.ConversionPattern=%d{MM-dd#HH:mm:ss} %-5p (%13F:%L) %3x - %m%n

log4j - filtering by class instead of level

In my app, I need to download 3 different artifacts from a remote ftp server. After I download each artifact, I validate the artifacts. And, if there are any validation errors, I need to log them aka
validation errors with Artifact A => A.log
validation errors with Artifact B => B.log
validation errors with Artifact C => C.log
My log4j.properties looks like:
log4j.rootLogger=DEBUG, rollingfile
log4j.logger.artifactALogger=INFO, AppenderA
log4j.logger.artifactBLogger=INFO, AppenderB
log4j.logger.artifactCLogger=INFO, AppenderC
log4j.appender.AppenderA.File=A.log
log4j.appender.AppenderA.MaxFileSize=5000KB
log4j.appender.AppenderA.MaxBackupIndex=1
log4j.appender.AppenderA.layout=org.apache.log4j.PatternLayout
log4j.appender.AppenderA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.AppenderB.File=B.log
log4j.appender.AppenderB.MaxFileSize=5000KB
log4j.appender.AppenderB.MaxBackupIndex=1
log4j.appender.AppenderB.layout=org.apache.log4j.PatternLayout
log4j.appender.AppenderB.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.AppenderC.File=C.log
log4j.appender.AppenderC.MaxFileSize=5000KB
log4j.appender.AppenderC.MaxBackupIndex=1
log4j.appender.AppenderC.layout=org.apache.log4j.PatternLayout
log4j.appender.AppenderC.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
As you can see, I have 1 appender per logger and the configuration for each appender is exactly the same (except for the name of the log file)...alot of duplication! Down the line, if I end up with more artifacts to download, I would need to declare another logger and another appender for each artifact. Something just doesn't seem right to me doing it this way. Maybe, I'm just not using log4j the way it was designed to be used? I'm currently filtering log output by class rather than by log level. Should I be using a different logging API that is more suitable?
We can create a custom logger for log4j for this purpose. We had a similar requirement once and ended up creating a custom logger.

Resources