Reading:  

What is Log4J. An absolute beginners tutorial.


Logging in Files

The org.apache.log4j.FileAppender is the base class for logging the information into files. Some of the configurable parameter of fileAppender: 

File Appender Configuration:

 

Property

Description

immediateFlush

Output stream to file will be flushed on each append operation.

encoding

Character encoding is possible.

threshold

Appender threshold level.

Filename

Log file Name

fileAppend

Logging information gets appended at the end of same file.

bufferedIO

This flag indicates writing  for buffered enabled

bufferSize

buffer size limit for bufferd I/O

 

XML configuration for the log4j:

 

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
  <appenders>
    <File name="MyFile" fileName=" Applicationdemo.log ">
        <PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>            
  </appenders>
  <loggers>    
    <root level="debug">
      <appender-ref ref="MyFile " level="warn"/>
    </root>   
  </loggers>
</configuration>

Logging in Multiple Files:

There are needs for writing the log message into multiple files due file size reach etc. To do that we use org.apache.log4j.RollingFileAppender class this extends FileAppender class and inherits its properties. Some of the configurable parameter to the FileAppemder:

 

Property

Description

maxFileSize

Maximum size of file before rolling to next file

maxBackupIndex

This denote number backup file to be created

 

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Properties>
<Property name="log-path">C:/logs/</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile" fileName="${log-path}/Applicationdemo.log" filePattern="${log-path}/Applicationdemo-%d{yyyy-MM-dd}-%i.log" >
<PatternLayout>
<pattern>%d{dd/MMM/yyyy HH:mm:ss,SSS}- %c{1}: %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="5 MB" />
</Policies>
<DefaultRolloverStrategy max="4"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="root" level="debug" additivity="false">
<appender-ref ref="RollingFile" level="debug"/>
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>


In this configuration the maximum permissible size each log file is 5MB. Upon reaching maximum size a new log file will be created since maxBackupIndex is defined as 2.Once second log file reaches maximum size the first log file will be deleted and thereafter all logging information’s will be rolled back to first log file.

Daily Log File Generation:

There are needs for writing the log message into single logfile per day. To do that we use org.apache.log4j.DailyRollingFileAppender class this extends FileAppender class and inherits its properties. Some of the configurable parameter to the FileAppender:

Property

Description

Date Pattern

Naming convention of file based on date pattern.Default roll over is midnight each day

DatePattern controls rollover schedule based one of following patterns:

  • '.' yyyy-MM -        Roll over at end of each month and beginning of next month.
  • '.' yyyy-MM-dd -       Midnight each day roll over occur
  • '.' yyyy-MM-dd-a - Midday and midnight of each dayroll over occur.
  • '.' yyyy-MM-dd-HH - Roll over at top of every hour.
  • '.' yyyy-MM-dd-HH-mm - Every minute Roll over
  • '.' yyyy-ww - Roll over on first day of each week.

Config file example

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
	<Properties>
		<Property name="log-path">C:/logs/</Property>
	</Properties>
	<Appenders>
		<RollingFile name="RollingFile" fileName="${log-path}/Applicationdemo.log" filePattern="${log-path}/Applicationdemo-%d{yyyy-MM-dd}-%i.log">
			<PatternLayout>
				<pattern>%d{ yyyy-MM-dd-a }</pattern>
			</PatternLayout>
			<Policies>
				<SizeBasedTriggeringPolicy size="5 MB" />
			</Policies>
			<DefaultRolloverStrategy max="4" />
		</RollingFile>
	</Appenders>
	<Loggers>
		<Logger name="root" level="debug" additivity="false">
			<appender-ref ref="RollingFile" level="debug" />
		</Logger>
		<Root level="debug" additivity="false">
			<AppenderRef ref="RollingFile" />
		</Root>
	</Loggers>
</Configuration>

 

Description

This tutorial is aimed at learners who want to get an understaind on what Log4J is and what it is used for. This tutorial have 10 part to it as shown below

  1. Overview    
  2. How to Install Log4J
  3. Understanding Architecture
  4. Getting started with Log4J Configuration
  5. Sample Program
  6. Logging Methods
  7. Understanding different Logging Levels
  8. Log4J log formatting
  9. How to log in files
  10. How to log in database using Log4J

We hope that this tutorial will help you out with Log4J. Please note that this tutorial is for absolute starters and does not go into much depth. Let us know how we can improve by sending  your feedback.



Prerequisites

A good understanding of Java programming language is required.

Audience

Absolute beginners

Author: Subject Coach
Added on: 7th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!