log4jdbc not writing to log files - jdbc

I am trying to debug connection leak in my Struts application and have set-up log4jdbc. I am able to see log4jdbc logs in console but I wish to have jdbc connection open/close events being written to a log file.
Below is my log4j.properties file
log4j.debug=true
! Log connection open/close events and connection number dump
log4j.logger.jdbc.connection=INFO,connection
log4j.additivity.jdbc.connection=false
! the appender used for the JDBC Connection open and close events
log4j.appender.connection=org.apache.log4j.RollingFileAppender
log4j.appender.connection.File=C:/dev/myworkspace/log/connection.log
log4j.appender.connection.layout=org.apache.log4j.PatternLayout
log4j.appender.connection.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c{1} - %m%n
log4j.appender.connection.MaxFileSize = 2000KB
log4j.appender.connection.MaxBackupIndex = 10
Please suggest what is wrong with my properties file as the logs are not written in connection.log file ?

Try out this,
For Connection settings:
log4j.appender.connection=org.apache.log4j.RollingFileAppender
log4j.appender.connection.file=C:/dev/myworkspace/log/connection.log
log4j.appender.connection.maxFileSize=5MB
log4j.appender.connection.maxBackupIndex=5
log4j.appender.connection.append=true
log4j.appender.connection.layout=org.apache.log4j.PatternLayout
log4j.appender.connection.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=INFO,file

Related

Spring Boot : LOGBack custom fields in every log statement

Thought of sharing the below work as I struggled to find this info:
Requirement is to put the business correlation id(AccCode and Mac) in every log statement. This can be done by using Mapped Diagnostic Context(MDC)..
No need to understand how LOGBack appender->Encoder->Layout work.
Spring boot log4j.properties has below pattern:
LOG_LEVEL_PATTERN=%5p
LOG_PATTERN=[%d{yyyy-MM-dd HH:mm:ss.SSS}] boot%X{context} - ${PID} ${LOG_LEVEL_PATTERN} [%t] --- %c{1}: %m%n
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=${LOG_PATTERN}
Override LOG_LEVEL_PATTERN from application.properties with custom fields:
logging.pattern.level=AccCode:%X{AccCode} Mac:%X{Mac} %5p
Please note, now we have two extra fields defined into the layout. AccCode and Mac. Value for these new fields need to be provided using the MDC:
MDC.put("AccCode", "46014" );
MDC.put("Mac", "2025b9bcf");
Then all the log statements would have AccCode and Mac as below:
2016-09-12 09:11:37.574 **AccCode:46014** **Mac:2025b9bcf** DEBUG 9724 --- [main] c.c.p.sph.http.client.SphApAdminClient : Loging out from Sph

When running test cases with maven, logger not generating log file

Getting below error when run the batch file.
log4j:WARN No appenders could be found for logger (devpinoyLogger).
log4j:WARN Please initialize the log4j system properly.
One quick way to fix this is to add a log4j.properties file in main/src/test/resources.
Here's a sample log4j.properties file that will output logs to the console:
# Set root logger level to INFO and its only appender to stdout.
log4j.rootLogger=INFO, stdout
# Define the stdout appender to output logs to the console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n

Hadoop log4j not working as No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory)

I am working on developing mapreduce using eclipse , and trying to test it using hadoop 2.6.0 windows standalone mode.
But getting the below error for log4j,
How to fix the below appender problem,
No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory)
same problem as hadoop log4j not working but no answer yet
Thanks ,
1- Create the file log4j.properties and put it in the location of
src/main/resources
the content of the log4j.properties
hadoop.root.logger=DEBUG, console
log4j.rootLogger = DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
it happens because of the hadoop logging framework
Create a log4j.properties file with something like the following:
hadoop.root.logger=DEBUG, console
log4j.rootLogger = DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n

log4j configurations for non web application

I've create a simple maven application and using log4j logger. My application is not a WEB application. I've tried to copy log4j.properties everywhere in java folder and everywhere across a project but still getting following error. Could you suggest how can I fix it?
log4j:WARN No appenders could be found for logger (org.springframework.web.client.RestTemplate).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
log4j.properties
# 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
The log4j.properties file should be in a dirctory or JAR file that is on the classpath at runtime.
If you have a Maven project with the standard Maven project directory layout, then put the file in the directory src\main\resources - that way, Maven should put it in the right place for you when you compile your project.

log4j path in production for tomcat 7 on windows

I need simple path setting to use log4j to be used for logging FINEST level of logging. But all forums and discussions show path setting for log4j.properties file either under eclipse or in some dev env. For a non development user, i need to set log4j.properties file, i tried in C:\apache-tomcat-7.0.27\conf path, but dosent help. stdout logs show default logs, note I have deleted default ogging.properties from \conf folder.
Where do i put log4j.properties file in windows tomcat folder ?
well, as official docs say (http://tomcat.apache.org/tomcat-7.0-doc/logging.html), you should have not deleted, the default logging.properites file, present on:
${catalina.base}/conf/logging.properties
,but rather add there what you need:
org.apache.catalina.level=FINEST
and
You would need to ensure the ConsoleHandler's (or FileHandler's') level is also set to collect this threshold, so FINEST or ALL should be set.
Or is there any strict requirement for you to use log4j (as per default JULI is used by tomcat7)?
UPDATE:
OK, as you mentioned, you're interested in the log4j case only. Let me point you to the official documentation: http://tomcat.apache.org/tomcat-7.0-doc/logging.html#Using_Log4j
Please follow the steps present there. As they claim to be working :)
For the explicit points you mentioned, the location of the log4j.properties file:
Create a file called log4j.properties with the following content and save it into $CATALINA_BASE/lib
log4j.rootLogger=FINEST, CATALINA
# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.
log4j.appender.CATALINA.Append=true
log4j.appender.CATALINA.Encoding=UTF-8
# Roll-over the log once per day
log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.LOCALHOST=org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.File=${catalina.base}/logs/localhost.
log4j.appender.LOCALHOST.Append=true
log4j.appender.LOCALHOST.Encoding=UTF-8
log4j.appender.LOCALHOST.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.File=${catalina.base}/logs/manager.
log4j.appender.MANAGER.Append=true
log4j.appender.MANAGER.Encoding=UTF-8
log4j.appender.MANAGER.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
log4j.appender.HOST-MANAGER=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HOST-MANAGER.File=${catalina.base}/logs/host-manager.
log4j.appender.HOST-MANAGER.Append=true
log4j.appender.HOST-MANAGER.Encoding=UTF-8
log4j.appender.HOST-MANAGER.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n
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
# Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=\
INFO, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]=\
INFO, HOST-MANAGER
Compared to the official docs, I just changed the 1.st line of the config to: log4j.rootLogger=FINEST, CATALINA
(But make sure you do the rest of the steps as well - like copying of the jar files,...)

Resources