Spring Boot : LOGBack custom fields in every log statement - spring

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

Related

How to add default logback.xml to spring boot project?

I have created a new spring boot project using spring initializer with spring-boot-starter-web as a starter project.I am able to use logback for logging but am unable to find default logback.xml in the project.I tried to search out but could not find reference to default file on logback documentation as well. I am looking to customize the logback file to use custom appender.
You can customize by using your application.yml, like this :
logging:
level:
root: WARN
org.springframework.security: DEBUG
org.springframework.web.servlet: INFO
org.springframework.jmx: INFO
org.springframework.cloud.config.client: INFO
org.springframework.cloud.context: INFO
org.hibernate: INFO
org.hibernate.sql: DEBUG
pattern:
console: '%date{HH:mm:ss.SSS} [%-5level] %-65logger - %msg%n'
file: '%date{HH:mm:ss.SSS} [%-5level] %-65logger - %msg%n'
file: D:/logs/my-log.log
And define for each profile

spring boot logback not logging my application logs

Need help... what am I doing wrong here???
I believe it does read the path and file name from application.properties file. But I don't think it reads the logback.xml or logback-spring.xml
I did some research and found many questions/answers. But I think everyone says to put the logback xml in resources and give the path and file name in application.properties. I know it is simple, but missing something somewhere...
Thanks in advance!!
application.properties
# ===============================
# LOGGING
# ===============================
# log configuration -- update config location as needed
#logging.config=/logback-spring.xml
#logging.file=/logs/iqs-service.txt
logging.path=/logs
logging.file=${logging.path}/log.log
logging.pattern.file=%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M - %msg%n
Content of log file (log.log.0):
[2018-12-10 15:54:41.999] - 10828 INFO [main] --- org.apache.catalina.core.StandardService: Starting service Tomcat
[2018-12-10 15:54:42.002] - 10828 INFO [main] --- org.apache.catalina.core.StandardEngine: Starting Servlet Engine: Apache Tomcat/8.5.14
[2018-12-10 15:54:42.257] - 10828 INFO [localhost-startStop-1] --- org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/]: Initializing Spring embedded WebApplicationContext
[2018-12-10 15:54:46.551] - 10828 INFO [http-nio-19917-exec-1] --- org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/]: Initializing Spring FrameworkServlet 'dispatcherServlet
Log statements in my java method - This never prints in the log
log.info("init(): [" + modelId + "]");
log.error("init(): [" + modelId + "]");
As far as I can tell the only thing I think that will kill it are these lines:
logging.path=/logs
logging.file=${logging.path}/log.log
I believe that if you include both, Spring Boot will ignore both of them. So I'd try removing one of those, and seeing if that helps.
As another thing (but I don't think it contributes to this problem), Spring recommends that you have just a logback-spring.xml file, not logback.xml (and definitely not both).

How to stop console logging for log4j in spring batch?

This is my log4j.properties file.
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
log4j.category.org.springframework.integration=FATAL
log4j.category.org.springframework.batch=FATAL
log4j.category.org.springframework.jdbc=FATAL
log4j.category.org.springframework.transaction=FATAL
I have tried changing the logging levels to INFO, ERROR etc. but nothing changes. I have this file in webapp/resources folder and i have added
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/main/webapp/resources/log4j.properties</param-value>
</context-param>
to web.xml as well. But making changes to log4j.properties doesn't do anything. I can still see many DEBUG logs in console like this
17:06:06.405 [qtp2018468405-39] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL query
How can I control logging level?
Logging level can be controled by using log4j.rootLogger property in log4j.properties file.
In your properties file I see you have not set log4j.rootLogger to INFO. Hence by default, the root logger is assigned to Level.DEBUG.

log4jdbc not writing to log files

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

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