Configuration of Logging Context
Configuration of Logging Options
Logging format and contents may be configured through the use of the log4j.properties file.Â
Noted in the log4j.properties file are several options to configure logging context to be associated with the logs. Logging context can be used to help trace a message through the logging output (console, file, or database) by providing a unique identifier associated with the log statement. This unique identifier is established upon entry into a Web Service and is a concatenation of the uuid of the message, the uuid of any related messages, and a randomly generated uuid. The benefits of this context are as follows:
- The ability to trace a message (and associated parent/child messages) through logging creating a "context" attribute that can be used to group together logging statements
- The configuration of a database logging appender to allow for log entries to be stored in a database, in order to more easily group statements
The level of logging detail is established through the rootLogger and the threshold settings on the various logging output devices. It is suggested for performance reasons that these properties be set to the INFO level or higher where available log levels are DEBUG, INFO, WARN, ERROR, FATAL or ALL. Within the log4j.properties file, C is defined to be the org.apache.log4j.ConsoleAppender, R is defined to be the apache.log4j.RollingFileAppender, and db is defined to be the org.apache.log4j.jdbc.JDBCAppender. With this understanding of the following properties, configure the use of all 3 logging output devices at the INFO level of detail:
log4j.rootLogger=INFO, C, R, db log4j.threshold=INFO log4j.appender.C.Threshold=INFO log4j.appender.R.Threshold=INFO log4j.appender.db.Threshold=INFO
The formatting of the log statements on each output device are configured through the ConversionPattern. The following properties will add the logging context information into the log statements for the ConsoleAppender and the RollingFileAppender:
log4j.appender.C.layout.ConversionPattern=[%d{dd/HH:mm:ss:SSS}] [context=%-25x] %-5p %-30.30c{1} %m%n log4j.appender.R.layout.ConversionPattern=[%d{M/d/yyyy}, %d{HH:mm:ss:SSS}] [context=%-25x] %-5p %-40C %m%n
This adds the context output, encased in brackets above, after the output of the date/time. It is left justified with a character spacing of 25. This can be adjusted as needed.
During the installation, MySQL would have been installed as the CONNECT database. With this release a new table would have been added to support context logging within the logging.log database table. The following properties will define the use of this new feature:
log4j.appender.db=org.apache.log4j.jdbc.JDBCAppender log4j.appender.db.Threshold=INFO log4j.appender.db.Driver=com.mysql.jdbc.Driver log4j.appender.db.URL=jdbc:mysql://localhost/ log4j.appender.db.User=nhincuser log4j.appender.db.Password=nhincpass log4j.appender.db.layout=org.apache.log4j.PatternLayout log4j.appender.db.layout.ConversionPattern=insert into logging.log(context,logLevel,class,message) values('%x','%p','%C','%m')
The database appender is defined as with a threshold of INFO as the level of detail on the logs. The MySQL driver is specified and the username and password are provided for communication with the database. The ConversionPattern provides the syntax to insert the logging content into the database table.