log4j, foreign logging - java

When my logger is set to "all", i am seeing messages that my code does not explicitly place. I am using a jar a friend of mine gave me to do some things (and i suspect he is logging stuff himself)
I would like to ONLY log stuff I ask to be logged by issuing the
logger.info ("something clever"); command
Below is my log4j.properties
please advise.
# ***** Set root logger level to WARN and its two appenders to stdout and R.
log4j.rootLogger=all, R
# ***** R is set to be a RollingFileAppender.
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=log/something.log
# ***** Max file size is set to 100KB
log4j.appender.R.MaxFileSize=100KB
# ***** Keep one backup file
log4j.appender.R.MaxBackupIndex=1
# ***** R uses PatternLayout.
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

Take a look at this excellent log4j cheatsheet:
http://www.johnmunsch.com/projects/Presentations/docs/Log4J/log.properties
Basically, you need to set something like this:
log4j.rootCategory=error, R
log4j.category.com.your.package=debug
or, alternatively:
log4j.category.com.your.friends.package=error
Replace the package names as necessary.
These would make the root logger set to error (so any package - including other libraries you might be using, like Hibernate, Spring, etc.) will not log anything low-level (debug, info, warn), but log only errors.
It will also set your package (include your top level package or packages) to log on a debug level, so all your loggers will log normally. It will also set your friend's package to error, so it doesn't output anything.
If you have a common root package, just use it. E.g. if you have packages:
com.example
com.example.a
com.example.a.aa
com.example.b
com.example.c.d
just include com.example and it will inherit for the packages below by default.
You could, of course, override it, e.g. specify:
com.example - warn
com.example.a.aa - debug
or something similar. Take a look here for a detailed explanation:
http://logging.apache.org/log4j/1.2/manual.html

Related

Log4j not writing logs to file on one Websphere server and writing to file on other

I have maven application with log4j.properties in it with setting to write the logs to a specified file instead of console. When I run the EAR on one of the websphere servers it is creating the file as expected and writing logs to it. But, when I am running the same EAR on other webspehere server it is writing to console instead of writing the logs to the specified file. I have checked the permissions and everything seems to be fine. Please help me in identifying what the issue is. Thanks in advance.
# CONSOLE APPENDER (stdout)
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=DEBUG
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d] [%t] %-5p %20c - %m%n
# ROLLING FILE APPENDER (on the file system) for memberpolicyattributesservice code
log4j.appender.xxxxService=org.apache.log4j.RollingFileAppender
log4j.appender.xxxxService.Threshold=DEBUG
log4j.appender.xxxxService.File=/var/logs/xxxServer1/xxxServiceLog.log
log4j.appender.xxxxService.layout=org.apache.log4j.PatternLayout
log4j.appender.xxxxService.layout.ConversionPattern=%d{MM-dd#HH:mm:ss} %-5p (%13F:%L) %3x - %m%n
log4j.appender.xxxxService.MaxFileSize=10000KB
log4j.appender.xxxxService.MaxBackupIndex=30
log4j.appender.xxxxService.layout=org.apache.log4j.PatternLayout
log4j.appender.xxxxService.layout.ConversionPattern=[%d] [%t] %-5p %20c - %m%n
# ROLLING FILE APPENDER (on the file system) for hiberate, open source code log files
log4j.appender.open_source_code=org.apache.log4j.RollingFileAppender
log4j.appender.open_source_code.layout=org.apache.log4j.PatternLayout
log4j.appender.open_source_code.Threshold=DEBUG
#message format:YYYY-MM-DD HH:mm:ss,ms [ThreadId] <PRIORITY> classname.message
log4j.appender.open_source_code.layout.ConversionPattern=%d [%t]<%-5p> %c.%m \r\n
#file that will be logged to
log4j.appender.open_source_code.File=/var/logs/xxxServer1/open_source_code.log
log4j.appender.open_source_code.Append=true
log4j.appender.open_source_code.MaxFileSize=1024KB
log4j.appender.open_source_code.MaxBackupIndex=5
#turn on log4j verbose mode
log4j.debug = true
# Set root logger level to INFO and its appender to DSInstrumentor,stdout.
log4j.rootLogger=DEBUG,stdout,xxxxService
# YOUR CATEGORIES (to customize logging per class/pkg/project/etc)
log4j.category.fatal=FATAL,xxxxService
log4j.category.error=ERROR,xxxxService
#This will also enable the logging for all the children (packages and classes) of this package
log4j.logger.com.xxxxx=ALL,xxxxService
# Print only messages of level INFO in the open source code
log4j.logger.org=INFO,open_source_code
You have multiple loggers defined in your root logger (DEBUG, stdout, xxxxService) but the xxxxService loggers look like they're bound to the file system on one of the systems:
log4j.appender.open_source_code.File=/var/logs/xxxServer1/open_source_code.log
Make sure that path is valid for each server in your WAS cluster.
As a side note, you should probably avoid using debug and stdout on a remote server. This is fine for local development in your workstation, but not on a remote box. Instead, provide different log4j properties on your various deployment tiers. This lets you customize the log location or appender (say c:\temp or CONSOLE on your desktop, but /var/logs ... on all remote machines) as well as your log levels (DEBUG for desktop, maybe INFO for QA or Staging, and WARN or ERROR for Production).

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: logging works when package inside app, but not when package contributed by a JAR

This isn't just a rehash of how to do package-specific logging. I think I've got that nailed. It's about logging not being done when the package is moved out to a JAR shared with others.
Originally, I wrote some special logging into my web application which worked, putting special statements into a file separate from catalina.out. Later, it seemed useful to share this facility with other, cooperating web applications, so we moved the package, consisting of one interface and one class, to a utilities JAR. While the information-gathering continued successfully, the output to the "special" log file, separate from catalina.out, ceased.
When I relocate a copy of the package back into my application, the logging begins working again as originally designed.
The package name remains the same no matter whether it's physically part of my application code or linked in from a consumed library JAR.
I'm struggling to understand a class linked from a JAR is seemingly excluded from the logging scheme while it works flawlessly when simply part of the application code.
Thank you for taking the time to look at this. Any suggestions would be greatly appreciated.
Here's log4j.properties:
# --------------------------------------------------------------------------------------------
# 1) Standard Tomcat log (tomcat): /var/log/tomcat6/catalina.out
# 2) Splunkable output log (splunkable): /var/log/tomcat6/myapp.log.
# 3) Console used when debugging myapp from Eclipse.
log4j.rootLogger=TRACE,tomcat,console
# Rolling-file appender for Tomcat -----------------------------------------------------------
log4j.appender.tomcat.Threshold=INFO
log4j.appender.tomcat=org.apache.log4j.DailyRollingFileAppender
log4j.appender.tomcat.DatePattern='.'yyy-MM-dd
log4j.appender.tomcat.File=${catalina.home}/logs/catalina.out
log4j.appender.tomcat.file.MaxFileSize=500Mb
log4j.appender.tomcat.file.MaxBackupIndex=5
log4j.appender.tomcat.layout=org.apache.log4j.PatternLayout
log4j.appender.tomcat.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Splunkable log -----------------------------------------------------------------------------
log4j.appender.splunkable.Threshold=TRACE
log4j.appender.splunkable=org.apache.log4j.RollingFileAppender
log4j.appender.splunkable.File=${catalina.home}/logs/myapp.log
log4j.appender.splunkable.MaxFileSize=1Gb
log4j.appender.splunkable.MaxBackupIndex=7
log4j.appender.splunkable.layout=org.apache.log4j.PatternLayout
log4j.appender.splunkable.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}: %m%n
# Set the additivity flag to false to avoid propagating the rather verbose splunkable
# output to the normal Tomcat and console logs. Is this mere superstition?
log4j.category.com.acme.web.myapp.logging=TRACE,splunkable
log4j.additivity.com.acme.web.myapp.logging=false
# This logs output to the Eclipse console when running in that mode --------------------------
log4j.appender.console.Threshold=TRACE
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.SimpleLayout
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

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/

How to make log4j record to a file and print to console

I can make the log to go the console but I cant seem to make it go to a log file. Here is my properties file.
log4j.rootLogger=DEBUG, LOG , stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d{d/MM/yy HH:mm:ss}:%m%n
# log4j.appender.LOG.Threshold=INFO
log4j.appender.LOG=org.apache.log4j.RollingFileAppender
log4j.appender.LOG.File=C:\dev\harry\data\logs\core.log
log4j.appender.LOG.layout=org.apache.log4j.PatternLayout
log4j.appender.LOG.Append=true
log4j.appender.LOG.layout.ConversionPattern=%5p %d{d/MM/yy HH:mm:ss}:%m%n
# log4j.appender.LOG.Threshold=INFO
The problem is that your single \ should be \\. This is true in most properties files.
There are some issues with the \ in the file path that need to be fixed. The syntax is a bit cryptic but the way to log to multiple locations is to attach a named logging appender to the root logger. In this example this is:
log4j.rootLogger=DEBUG, LOG , stdout
DEBUG is a logging level (threshold filter) to use for the root logger.
LOG is the name of a logging appender
stdout is the name of a second appender
The logging appenders are specified by
log4j.appender.{logging-appender-name}={some.log4j.appender.class}
log4j.appender.{logging-appender-name}.{some-other-property}=...
Where {logging-appender-name} is a name selected by yourself (in this case LOG and stdout) and {some.log4j.appender.class} is one of the many log4j logging appender classes such as DailyRollingFileAppender or ConsoleAppender.
I would have added:
log4j.appender.LOG.Threshold=ALL
I'm not sure what the default is.

Categories