log4j - Configuration, Ignoring the lib/jar Logging - java

I am working on a java application and I am using log4j for logging. My "log4j.properties" file (in WEB-INF/conf) is here -
log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=C:/billingAutomationService/infotrac20.log
log4j.appender.R.MaxFileSize=30MB
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
In my application there are a some debug messages with logger.debug("debug message"). But with the configuration I ca not see these debug message in my log file - "stdout.log". To see the debug message in my log file ("stdout.log") I have added the following line in my log4j.properties file -
log4j.rootLogger=debug,stdout
But adding this line enables all 'DEBUG' level on my jar/lib along the whole project. I am using a log of spring, hibernate and other jars/libs. This incident makes my log file ("stdout.log") very large even on a single operation. Because a lots of 'DEBUG' message coming from the standard lib/jars which I don't want to debug. Is there any way to turn off the debug message coming form the lib/jars?
Thanks

Please check the below thread.
log4j: package-specific logging
Usually you would have a common package starting for all your files, only enable debug for those package names.
Use root logger as warn.
Also.
Specify only some packages to have debug output

Related

Log4j: how to compose config?

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.

How to set up a log4j.properties file for sub packages

I want to create log file for two packages: net.biomodels.jummp.indexing and net.biomodels.jummp.indexing.solrindexer. However, it does not work as I expect. The log only contains what are involving in net.biomodels.jummp.indexing package.
Could you help me finding out the missing of the following logging configuration?
log4j.rootLogger=WARN, R
# everything goes to the general log
log4j.logger.net.biomodels.jummp.indexing=DEBUG, stdout, R
log4j.additivity.net.biomodels.jummp.indexing=false
# I want to log classes in solrindexer package, underneath indexing package
log4j.logger.net.biomodels.jummp.indexing.solrindexer=INFO, stdout, R
log4j.additivity.net.biomodels.jummp.indexing.solrindexer=false
## general log
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=logs/general.log
log4j.appender.R.MaxFileSize=1MB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%5p %t %d{ISO8601} %c{2} - %m%n
Your configuration is fine from a syntax point of view. I suspect your log level is the problem. You have your first logger set to DEBUG level:
log4j.logger.net.biomodels.jummp.indexing=DEBUG, stdout, R
while your second logger is set to INFO level:
log4j.logger.net.biomodels.jummp.indexing.solrindexer=INFO, stdout, R
Thus if you are expecting to see DEBUG level logs from classes in the package net.biomodels.jummp.indexing.solrindexer you will not see them unless you change the log level of your logger to DEBUG or lower.

log4j with tomcat does not log what is logged in mvn test

I have a web application. When I run 'mvn test', it logs the debug messages to console, as I have configured it. But when I deploy it to tomcat, I don't see the the logs of the application. I am absolutely sure that I got the log4j.properties file on the right place in the war, as when I change values in the deployed /var/lib/tomcat7/webapps/worldmodel/WEB-INF/classes/log4j.properties for root logger or for hibernate and touch web.xml, I see/chease to see debug logs for hibernate. But I cannot get my application's debug messages to be logged with any configuration I've tried.
Here is how I do the logging:
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
logger = Logger.getLogger(BaseObject.class);
log(Level.DEBUG,"message");
Here is log4j.properties for testing:
log4j.rootLogger=INFO, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Encoding=UTF-8
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.SYSLOG = org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.syslogHost = 127.0.0.1
log4j.appender.SYSLOG.layout = org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.SYSLOG.Facility = LOCAL0
log4j.logger.org.rulez.magwas.worldmodel=DEBUG
#log4j.logger.org.hibernate.SQL=debug
#log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=trace
#log4j.logger.org.hibernate.type=trace
here is the log4j.properties which gets deployed:
log4j.rootLogger=INFO, SYSLOG
log4j.appender.SYSLOG = org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.syslogHost = 127.0.0.1
log4j.appender.SYSLOG.layout = org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.SYSLOG.Facility = LOCAL0
log4j.logger.org.rulez.magwas.worldmodel=DEBUG
#log4j.logger.org.hibernate.SQL=debug
#log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=trace
#log4j.logger.org.hibernate.type=trace
I get such lines in mvn test:
2013-05-21 07:12:17,751 [main] DEBUG org.rulez.magwas.worldmodel.BaseObject- setValue0e9é 072r 074t 0e9é 06bk
The whole project: https://github.com/magwas/worldmodel/commit/c6b08da0a733d9b61257c669e0cc4af9e59444be
edit:
ok, forget it, the code (getter and setter methods in a bean) seems not being been called, perhaps hibernate sets/gets the values directly?
Since, you're configuring your ROOT logger with level INFO it won't display any DEBUG level messages.
log4j.rootLogger=INFO, SYSLOG
The above rootLogger should be configured as DEBUG to see debug and other higher levels (Fatal, Error, Warn and Info) in your logs.
log4j.rootLogger=DEBUG, SYSLOG
I'm not sure what your Maven test case configuration is but perhaps the log4j.properties you've shared isn't having any effect there.
EDIT: It seems from your edit that your logging in production is misconfigured. If your logging is set to INFO it should not log DEBUG messages. I mean, that simply defeats the purpose of having log levels, clearly shows Log4j has not been configured properly and drains Prod resources both space and time.

Shunting certain org.apache.log4j log messages off to a particular log file

We already have extensive logging in our application using org.apache.log4j. We now want to shunt some of these messages off to a new XML log file (while continuing to go to the original log file).
Is this possible? Is there a way we can identify these messages and send them someplace special in addition to the regular log file?
I think you can define two file appenders. One at the root level and other at the package(select the package level as appropriate) level as below:
# Root logger option
log4j.rootLogger=DEBUG, RootFileAppender
#Shunted Logger option
log4j.logger.com.shunted=ERROR,ShuntedFileAppender
# RootFileAppender - used to log messages in the root.log file.
log4j.appender.RootFileAppender=org.apache.log4j.FileAppender
log4j.appender.RootFileAppender.File=root.log
log4j.appender.RootFileAppender.MaxFileSize=100MB
log4j.appender.RootFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RootFileAppender.layout.ConversionPattern= %5p [%t] (%F:%L) - %m%n
# ShuntedFileAppender - used to log messages in the shunted.log file.
log4j.appender.ShuntedFileAppender=org.apache.log4j.FileAppender
log4j.appender.ShuntedFileAppender.File=shunted.log
log4j.appender.ShuntedFileAppender.MaxFileSize=10MB
log4j.appender.ShuntedFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ShuntedFileAppender.layout.ConversionPattern= %5p [%t] (%F:%L) - %m%n
Please Note: You can define the two logger option as different levels as well. In above example, ROOT is defined at DEBUG while shunted is devined as ERROR level.

Disable Struts2 logs

How can I disable Struts2 zillions of logs?
I constantly get logs like this:
2012-04-12 23:20:31,487 DEBUG [XWorkConverter.java:388] : Property: menuExpandedOps
I'm using struts 2.0 and standard java logging (I'm not using log4j).
The logging.properties file of the JVM is set by default to INFO, and I already have struts.devMode = false in my struts.properties file.
If you are free to use log4j, i suggest you to use that, since Log4j has the ability to set different log levels for different packages and hence using the log level OFF we can disable logging for a particular package.
In S2 most of the log messages will be from these packages
xwork2
struts2
freemarker
ognl
and a simple property file/xml file in class-path can help you to turn on or off the logging information.
and a simple property file/xml file in class-path can help you to turn on or off the logging information.
For those who want an example:
1) Put log4j.properties inside src (where your packages are):
2) Turn OFF low-level logging:
# root logger option
log4j.rootLogger=DEBUG, stdout, file
# direct log messages to console
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=[%p: %d{yyyy-MM-dd HH:mm:ss} %F:%L] %m%n
# direct log messages to a log file, support file rolling
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=${catalina.home}/logs/sortingmonitor.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p: %d{yyyy-MM-dd HH:mm:ss} %F:%L] %m%n
# disable apache low level logging
log4j.category.com.opensymphony.xwork2=OFF
log4j.category.org.apache.struts2=OFF
log4j.category.freemarker.beans=OFF
log4j.category.freemarker.cache=OFF
Further reading for XML: http://deepaksrivastav.github.io/blog/2011/01/06/disable-struts-2-log-messages/

Categories