I want to be able to log debugging messages from my Java classes in a file on my web server, using log4j. I am able to log messages successfully to a file on the server if the Java classes are run from a JUnit test, using the following settings:
log4j.properties:
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=D:/logs/fair.log
log4j.appender.rollingFile.MaxFileSize=2MB
log4j.appender.rollingFile.MaxBackupIndex=2
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n
log4j.rootLogger = INFO, rollingFile
FairScheduler.java:
private static Logger logger = Logger.getLogger(FairScheduler.class);
logger.info("initializing Scheduler");
But when I deploy the application to my Coldfusion application, the log messages are no longer logged.
How can I configure my Coldfusion server to log messages to this log file (or view any of my log4j log messages at all)?
Related
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 enable logging in mysql j connector driver?
I used the following command while creating the connection
"jdbc:mysql://localhost/test?logger=com.mysql.jdbc.log.StandardLogger&profileSQL=true";
My log4j property file is like this
# Root logger option
log4j.rootLogger=TRACE, rfile
log4j.logger.com.mysql=trace, rfile
log4j.appender.rfile = org.apache.log4j.RollingFileAppender
log4j.appender.rfile.File =PrepStmt.log
log4j.appender.rfile.MaxFileSize = 100KB
log4j.appender.rfile.Append = true
log4j.appender.rfile.layout = org.apache.log4j.PatternLayout
log4j.appender.rfile.layout.ConversionPattern= %d [%t] %-5p %c %x - %m%n
No log is getting captured in my log file.
Mysql does not know about your log4j properties.
From the docs, scroll to section Debugging/Profiling:
logger
The name of a class that implements "com.mysql.jdbc.log.Log" that will
be used to log messages to. (default is
"com.mysql.jdbc.log.StandardLogger", which logs to STDERR)
Default: com.mysql.jdbc.log.StandardLogger
So your mysql logs simply go to STDERR. If you want to log to a log4j defined destination you can implement com.mysql.jdbc.log.Log, forward to a log4j logger, and specify that implementation class in the connect URL.
You could configure log4jdbc to intercept the JDBC connection and log the sql.
I created java maven application with log4j logging.
In windows it is working. Log files are created in the path which is given the log4j.properties. log4j.properties is in maven resource folder.
But when buliding jar with maven install from eclipse, and running it from ubuntu, log files are not creating. Modified the log4j.properties with linux path.
# Root logger option, logs messages for the all classes in the application
log4j.rootLogger=DEBUG, stdout
# Redirect 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
# log for myproj.sample.Aaa class
log4j.logger.myproj.sample.Aaa = DEBUG,aaa
log4j.appender.aaa = org.apache.log4j.RollingFileAppender
log4j.appender.aaa.file = /home/ushusadmin/aaa.log
log4j.appender.aaa.layout = org.apache.log4j.PatternLayout
log4j.appender.aaa.append = true
log4j.appender.aaa.MaxFileSize=50MB
log4j.appender.aaa.MaxBackupIndex=7
log4j.additivity.aaa=false
Below given the code for logging in Aaa class.
public static final Logger LOG = Logger.getLogger(Aaa.class.getName());
LOG.debug("haiii");
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.
I am using log4j for my application - there I need to see all log messages, so I defined
# General configuration
log4j.rootLogger = ALL, ConsoleAppender
# Appender configuration
log4j.appender.ConsoleAppender = org.apache.log4j.ConsoleAppender
log4j.appender.ConsoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.ConsoleAppender.layout.ConversionPattern = %5p (%c) %m%n
But, after I started using Apache's HTTPClient library (which is awesome btw), my console got clouded up by its logging - everything was logged and I am not able to see the log output from my own application any more.
So how can I tell the HTTPClient library that it should only log WARN messages, while my own application still logs on ALL levels?
I create my application logger using
Logger logger = Logger.getLogger(MyClass.class);
PropertyConfigurator.configure("log4j.properties");
It's quite simple. Just add the below line.
log4j.logger.org.apache.commons.httpclient=WARN
Similarly, you can configure levels for any package com.foo or class com.foo.Bar by appending it to the prefix log4j.logger as follows:
log4j.logger.com.foo=MYLEVEL
log4j.logger.com.foo.Bar=DEBUG