logger.IsDebugEnabled() always return false - log4j

i am new to log4j and i have the following log4j.properties file in my java application
i am working on this in websphere 6.1
log4j.properties file
log4j.rootLogger=info, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.package_name=debug
However this is working if i am using only one project as part of my application.If there are multiple projects and i want to use logging facility,logger.isDebugEnabled() always return false.. can anybody suggest a solution for this?
Thanks in advance

Websphere use a classloader for default for each EAR. If you have several Web modules or EJB modules and several files for log4j, only one is loaded by the classloader.
See A Powerful, Easy-to-Use Logging System for configure log4j with several projects in a EAR.
# Set root logger level to INFO and appender to STDOUT.
log4j.rootLogger=INFO, STDOUT
#------------------------------------STDOUT-----------------------------------#
# 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
log4j.appender.STDOUT.layout.ConversionPattern=%d %-5p (%c.java:%L).%M - %m%n
log4j.appender.STDOUT.Encoding=UTF-8
#-----------------------------------------------------------------------------#
# Specify the logging level for loggers from other libraries
log4j.logger.org.apache.commons.beanutils.BeanUtils=DEBUG
log4j.logger.org.apache.struts.action=DEBUG
log4j.logger.org.apache.struts.tiles=DEBUG
log4j.logger.org.apache.struts.util.ModuleUtils=DEBUG
log4j.logger.org.apache.struts.util.RequestUtils=DEBUG
log4j.logger.org.apache.struts.util.PropertyMessageResources=ERROR
log4j.logger.com.ibm._jsp=DEBUG
May you are missing the log4j.logger. prefix for each particular package.
See more of log4j in http://logging.apache.org/log4j/1.2/manual.html

Related

Which logger should I use to get my data in Cloud Logging

I am running a PySpark job using Cloud Dataproc, and want to log info using the logging module of Python. The goal is to then push these logs to Cloud Logging.
From this question, I learned that I can achieve this by adding a logfile to the fluentd configuration, which is located at /etc/google-fluentd/google-fluentd.conf.
However, when I look at the log files in /var/log, I cannot find the files that contain my logs. I've tried using the default python logger and the 'py4j' logger.
logger = logging.getLogger()
logger = logging.getLogger('py4j')
Can anyone shed some light as to which logger I should use, and which file should be added to the fluentd configuration?
Thanks
tl;dr
This is not natively supported now but will be natively supported in a future version of Cloud Dataproc. That said, there is a manual workaround in the interim.
Workaround
First, make sure you are sending the python logs to the correct log4j logger from the spark context. To do this declare your logger as:
import pyspark
sc = pyspark.SparkContext()
logger = sc._jvm.org.apache.log4j.Logger.getLogger(__name__)
The second part involves a workaround that isn't natively supported yet. If you look at the spark properties file under
/etc/spark/conf/log4j.properties
on the master of your cluster, you can see how log4j is configured for spark. Currently it looks like the following:
# Set everything to be logged to the console
log4j.rootCategory=INFO, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.err
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c: %m%n
# Settings to quiet third party logs that are too verbose
...
Note that this means log4j logs are sent only to the console. The dataproc agent will pick up this output and return it as the job driver ouput. However in order for fluentd to pick up the output and send it to Google Cloud Logging, you will need log4j to write to a local file. Therefore you will need to modify the log4j properties as follows:
# Set everything to be logged to the console and a file
log4j.rootCategory=INFO, console, file
# Set up console appender.
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.err
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c: %m%n
# Set up file appender.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/var/log/spark/spark-log4j.log
log4j.appender.file.MaxFileSize=512KB
log4j.appender.file.MaxBackupIndex=3
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.conversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c: %m%n
# Settings to quiet third party logs that are too verbose
...
If you set the file to /var/log/spark/spark-log4j.log as shown above, the default fluentd configuration on your Dataproc cluster should pick it up. If you want to set the file to something else you can follow the instructions in this question to get fluentd to pick up that file.

Log4j logs hidden or swallowed when deployed to CloudFoundry

I've just deployed my first app to CloudFoundry, and I use log4j. When I deploy the app to a local tomcat server, the logs print just fine as all is well. But, when I use the "vmc logs " command to get the logs from the instance on CloudFoundry, I only get the tomcat initialization logs and this message:
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Anything further that I've printed with log4j is not visible. System.out.println messages show up, but no log4j messages.
I've placed my log4j.properties file in my WEB-INF directory, and here are its contents:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=INFO, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-5p %-35c{1} %m%n
log4j.logger.org.springframework=WARN
My logger object is created as would probably be expected in my classes:
private static Logger log = Logger.getLogger(MyClass.class);
Any suggestions as to what configuration I'm missing to have my log4j logs show up in my CloudFoundry logs? Or am I retrieving them incorrectly?
Is Log4j set to output to STDOUT by default? 'vmc logs' will only return the contents of STDOUT, STDERR and the staging log files.
If you app is logging to a different file then use 'vmc file' to view the content.

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

FileAppender not writing anything in log4j

I have a small problem using log4j and FileAppender. When I am using ConsoleAppender, everything is working well but FileAppender won't ever produce a file (or write into a file). Moreover, I have no errors concerning log4j in the osgi console.
Here's my configuration :
# main link (console or file, depending on what we want)
# log4j.category.com.foo=DEBUG, console
log4j.category.com.foo=DEBUG, file
# log4j.category.com.foo=DEBUG, console, file
# console logging
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-4r %-5p %c %x - %m%n
# file logging
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=C:\\Users\\foo\\some_file.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-4r %-5p %c %x - %m%n
Note that the ConversionPattern attributes are exactly the same in the two appenders.
I have tried to change the File argument to write the log in the "run" directory but it didn't do anything.
I also tried to add log4j.appender.file.ImmediateFlush=true without success.
Have I missed something (obvious ?) somewhere ? It sound strange that log4j isn't even able to write a simple log in a file....
Thanks a lot in advance for your help.
Replace :
log4j.category.com.foo=DEBUG, console
By :
log4j.category.com.foo=DEBUG, console, file
I know this is old but for other Googlers...
For me it was simply using wrong package.
I used org.apache.logging.log4j instead org.apache.log4j
If you are using Maven use this:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

Creating an application specific log under Tomcat 7?

I'm trying to capture logging messages, stdout, and stderr for my webapp, which is running under Tomcat 7.
I have a log4j.properties file in my war file, at WEB-IN/classes/log4j.properties, pretty much copied from the log4j docs, except with a filename "myapp.log":
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.File=${catalina.home}/logs/myapp.log
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
WEB-INF/lib includes log4j-1.2.14.jar and commons-logging-1.0.4.
I'm expecting to see myapp.log appear in Tomcat's log directory, but it's not. Please explain to me what I'm doing wrong.
You're using a ConsoleAppender where you should be using a file appender:
log4j.appender.A1=org.apache.log4j.RollingFileAppender

Resources