Apache Log4j is java based logging framework which is widely used for logging purpose. It keeps info of what happening inside your application. When we deploy any services to server and if something went wrong we will not have any clue what happen to the request, That’s where log4j comes to picture it will keep all information when we have log4j in plate.

Log4j Setup and Configuration:

Create log4j.properties and paste below code

# 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{ISO8601} [%X{TrackId}] %5p %c{1}:%L - %m%n
log4j.logger.org.lightcouch=INFO,Daily,stdout
log4j.additivity.org.lightcouch.=false
log4j.logger.org.apache=ERROR

# Root logger option
log4j.rootLogger=INFO,Daily,stdout
# Hibernate logging options (INFO only shows startup messages)
log4j.logger.org.hibernate=INFO
log4j.logger.org.hibernate.SQL=debug
log4j.logger.org.hibernate.type=trace

# Log JDBC bind parameter runtime arguments
log4j.logger.org.hibernate.type=WARN
log4j.logger.org.hibernate.cache=WARN

########## Appender Daily Rolling
log4j.logger.appender=Daily
log4j.appender.Daily=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Daily.Threshold=INFO
log4j.appender.Daily.File=application.log
log4j.appender.file=org.apache.log4j.RollingFileAppender

# Roll the log file at a certain time
log4j.appender.Daily.DatePattern='.'yyyy-MM-dd

# Append to the end of the file or overwrites the file at start.
log4j.appender.Daily.Append=true
log4j.appender.Daily.layout=org.apache.log4j.PatternLayout
log4j.appender.Daily.layout.ConversionPattern=%d{ISO8601} %r [%X{TrackId}] [%5p] %t (%F:%M:%L)%m%n
log4j.logger.org.springframework.web.client=DEBUG
Levels of log4j logging
  1. INFO : In general whichever log you want to have track you can put with info such as request and response, ex log.info(“Message”);
  2. WARN : Use of deprecated APIs, errors, other runtime errors that are undesirable or unexpected.
  3. ERROR : Other runtime errors or unexpected conditions.
  4. DEBUG : Expect these to be written to logs only log.info(“Message”);

 

Log4j Setup and Configuration

 

Read Here to Spring Boot CRUD Operation

 

Leave a Reply

Your email address will not be published. Required fields are marked *