log4j kafka appender doesn't use defined ConversionPattern - log4j

I am running a Storm cluster which uses log4j for logging. I want to add a kafka appender in addition to the standard file-based logging.
My log4j config file looks like this:
log4j.rootLogger=INFO, A1
log4j.appender.A1 = org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File = /var/log/storm/logs/${logfile.name}
log4j.appender.A1.Append = true
log4j.appender.A1.DatePattern = '.'yyy-MM-dd
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
log4j.appender.KAFKA=kafka.producer.KafkaLog4jAppender
log4j.appender.KAFKA.Host=<kafka.ip>
log4j.appender.KAFKA.Port=9092
log4j.appender.KAFKA.Topic=storm_log
log4j.appender.KAFKA.SerializerClass=kafka.producer.DefaultStringEncoder
log4j.appender.KAFKA.layout=org.apache.log4j.PatternLayout
log4j.appender.KAFKA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
log4j.logger.my.package.name=INFO, KAFKA
Everything works fine, except that the kafka appender doesn't use the defined ConversionPattern, even though the definition is exactly the same as in the file appender, which works as intended. How do I have to change the configuration to make the kafka appender work?

I had the same issues with Kafka version 0.7.
There were some changes done to KafkaLog4jAppender in version 0.7.1 and after upgrading, the ConversionPattern works fine for me with a config similar to the one above.

Related

Why I have question instead line number when logging with log4j

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?

Wildfly 8.1 Log System

I am trying to make my Web Services Application Log using Wildfly 8.1 Final Log System Categories.
My App declares
private static final Logger LOGGER = Logger.getLogger("myapp");
It calls in parts of the code for
LOGGER.debug("something");
Using the log4j property file definition below it behaves as expected. The problem is when I access the Wildfly console (http://localhost:9990/console/App.html#logging) and update the Log Category named "myapp" from INFO to DEBUG. After tha I call my Web Service that calls the instruction LOGGER.debug("something"); inside it´s code.
The expected behavior is to show the "something" message written in my log file, but it does not happens because it behaves obeying only the log4j property file supplied below.
How to fix this?
#------------------------------------------------------------------------------
#log4j.rootLogger=DEBUG, stdout
log4j.logger.org.hibernate=INFO, stdout, myapp
log4j.logger.org.hibernate.SQL=INFO, stdout, myapp
log4j.logger.org.hibernate.type.descriptor.sql=ALL, stdout, myapp
log4j.logger.myappaudit=INFO, myappaudit
log4j.logger.myapp=INFO, stdout, myapp
log4j.logger.performance=INFO, myapp
#
# default Appender
#------------------------------------------------------------------------------
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} %c{1} [%p] %C{1}.%M - %m%n
#
# Appender for myappaudit
#------------------------------------------------------------------------------
log4j.appender.myappaudit = org.apache.log4j.DailyRollingFileAppender
log4j.appender.myappaudit.File = ${jboss.server.log.dir}/logs/myappaudit.log
log4j.appender.myappaudit.Append = true
log4j.appender.myappaudit.DatePattern = '.'yyy-MM-dd
log4j.appender.myappaudit.layout = org.apache.log4j.PatternLayout
log4j.appender.myappaudit.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} [%p] %m%n
#
# Appender for application
#------------------------------------------------------------------------------
log4j.appender.myapp = org.apache.log4j.DailyRollingFileAppender
log4j.appender.myapp.File = ${jboss.server.log.dir}/logs/console.log
log4j.appender.myapp.Append = true
log4j.appender.myapp.DatePattern = '.'yyy-MM-dd
log4j.appender.myapp.layout = org.apache.log4j.PatternLayout
log4j.appender.myapp.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} [%p] %m%n
#Spring logs
log4j.logger.org.springframework=DEBUG
log4j.logger.org.springframework.security=DEBUG
log4j.logger.org.springframework.ws=DEBUG
#Spring Transaction Logs
log4j.logger.org.springframework.orm.jpa=DEBUG
log4j.logger.org.springframework.transaction=DEBUG
#Spring Async Logs
log4j.logger.org.springframework.scheduling=ERROR
You will not be able to use the logger options in the console. The reason is that if you include a log4j config an entirely isolated logging instance is created for that deployment. If you want to use jboss's tools, you must remove your logging config and define in the standalone or domain xml. Alternatively, you can create your own servlet or mbean that is part of your deployment to make runtime adjustments to the logging level or any other changes, e.g adding or removing appenders
So, to be clear, are you packaging a log4j.properties file with your application? It that being respected correctly, but you hoped to use the jboss console to amend the logging level at runtime?

Log4j configuration for multiple loggers

I have a single log4j.properties file in the server and to applications deployed in the server.The requirement is to create separate loggers for the application
I defined this in my application
# Root logger option
log4j.rootLogger=INFO, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/opt/ibm/WebSphere/AppServer/profiles/MDMServer/logs/damcoLoging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
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
#logging for jbpm
log4j.logger.jbpmLogger=INFO, jbpmLogger
log4j.appender.jbpmLogger=org.apache.log4j.RollingFileAppender
log4j.appender.jbpmLogger.maxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.jbpmLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.jbpmLogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.jbpmLogger.File=/opt/ibm/WebSphere/AppServer/profiles/MDMServer/logs/jbpmLogging.log
log4j.additivity.jbpmLogger=false
In my java class I have done this for the secondary logger
Logger logger=Logger.getLogger("jbpmLogger");
Now the logs are getting generated properly.But for the secondary logger I want to set the class name as well.So that I can know from which class the log is generating.
Currently the log for the secondary logger looks like this
INFO jbpmLogger:8 - Hi
Is it possible to set the class name as well?
You can add the class name to the output Pattern with %C, although the docs warn that this is slow. Is this what you want?
Or since onegetLogger() method takes a String, you could concatenate the class and your "jbpmLogger" if you wanted to. And since the naming is hierarchical, still just using your single logger in the configuration file. e.g.
Logger logger = getLogger("jbpmLogger." + this.getClass().getName());

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.

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