Using Log4J for logging

Certify and Increase Opportunity.
Be
Govt. Certified Apache Cassandra Professional

Using Log4J for logging

log4j

Apache log4j is a Java-based logging framework that provides runtime application feedback. It provides the ability to control the granularity of log statements using an external configuration file (log4j.properties).

The log4j utility has three main components: loggers, appenders, and layouts. Loggers are logical log file names. They are the names known to the Java application. Each logger is independently configurable for the level of logging. Outputs are controlled by Appenders. Numerous Appenders are available and multiple Appenders can be attached to any Logger. This makes it possible to log the same information to multiple outputs. Appenders use Layouts to format log entries.

Log Levels
The available levels are:

  • All – turn on all logging
  • OFF – no logging
  • FATAL – severe errors causing premature termination
  • ERROR – other runtime errors or unexpected conditions
  • WARN – use of deprecated APIs, poor use of API, near errors, and other undesirable or unexpected runtime situations
  • DEBUG – detailed information on the flow through the system
  • TRACE – more detailed than DEBUG
  • INFO – highlight the progress of the application at a coarse-grained level

Log Messages
The messages that appear in the log are controlled via the conf/log4j.properties file. Using this properties file, you can control the granularity to the Java package and class levels. For example, DEBUG messages from a particular class can be included in the log while messages from others remain at a higher level. This is helpful to reduce clutter and to identify messages. The log is most commonly a file and/or stdout. The format, behavior (such as file rolling), and so on is also configurable at runtime.

Below are sample log messages from a Cassandra node startup:

INFO [main] 2012-02-10 09:15:33,112 DatabaseDescriptor.java (line 495)
Found table data in data directories. Consider using the CLI to define your schema.
INFO [main] 2012-02-10 09:15:33,135 CommitLog.java (line 166)
No commitlog files found; skipping replay
INFO [main] 2012-02-10 09:15:33,150 StorageService.java (line 400)
Cassandra version: 1.0.7
INFO [main] 2012-02-10 09:15:33,150 StorageService.java (line 401)
Thrift API version: 19.20.0
INFO [main] 2012-02-10 09:15:33,150 StorageService.java (line 414)
Loading persisted ring state

Storing log4j Messages in a Column Family
The Cassandra Appender provides the capability to store log4j messages in a Cassandra column family. To enable the Cassandra Appender:

Add resources/log4j-appender/lib/ to your application classpath.

Modify the conf/log4j.properties file, as shown in the example below:

# Cassandra Appender
log4j.appender.CASS=com.datastax.logging.appender.CassandraAppender
log4j.appender.CASS.hosts = 127.0.0.1
log4j.appender.CASS.port = 9160
#log4j.appender.CASS.appName = “myApp”
#log4j.appender.CASS.keyspaceName = “Logging”
#log4j.appender.CASS.columnFamily = “log_entries”
#log4j.appender.CASS.placementStrategy =
“org.apache.cassandra.locator.NetworkTopologyStrategy”
#log4j.appender.CASS.strategyOptions = {“DC1” : “1”, “DC2” : “3” }
#log4j.appender.CASS.replicationFactor = 1
#log4j.appender.CASS.consistencyLevelWrite = ONE
#log4j.appender.CASS.maxBufferedRows = 256

log4j.logger.com.foo.bar= INFO, CASS

Commented lines are included for reference and to show the default values.

log4j.appender.CASS=com.datastax.logging.appender.CassandraAppender specifies the CassandraAppender class and assigns it the CASS alias. This alias is referenced in the last line.

log4j.appender.CASS.hosts = 127.0.0.1 allows using a comma delimited list of Cassandra nodes (in case a node goes down).

Specify replication options in lines:

log4j.appender.CASS.placementStrategy = “org.apache.cassandra.locator.NetworkTopologyStrategy” log4j.appender.CASS.strategyOptions = {“DC1” : “1”, “DC2” : “3” }.

Share this post
[social_warfare]
Monitoring
JMX and managed beans

Get industry recognized certification – Contact us

keyboard_arrow_up