0% found this document useful (0 votes)
39 views

Import Static Class: Log Log

1) Log4j is a logging library that allows writing log messages to files and consoles. It requires adding the log4j jar to the classpath and configuring a log4j.properties file. 2) The Logger class provides methods to get a logger instance by name which is used to write log messages. The log4j.properties file defines appenders and log levels. 3) Appenders define destinations for log messages like files and consoles. The rootLogger defines which appenders will receive messages based on log level settings.

Uploaded by

Samrat singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Import Static Class: Log Log

1) Log4j is a logging library that allows writing log messages to files and consoles. It requires adding the log4j jar to the classpath and configuring a log4j.properties file. 2) The Logger class provides methods to get a logger instance by name which is used to write log messages. The log4j.properties file defines appenders and log levels. 3) Appenders define destinations for log messages like files and consoles. The rootLogger defines which appenders will receive messages based on log level settings.

Uploaded by

Samrat singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Source - http://www.tutorialspoint.com/log4j/log4j_logging_files.

htm

Add log4j [C:\Manish\Code\workspace\delegate\lib\log4j-1.2.17.jar] in classpath

 Project->Properties->java build path->Libraries->AddJars->


 Then select log4j jar and click Ok.

Log4j will be added to the class-path.

As consequence of this you will able to do following in your code.

import org.apache.log4j.Logger;
static Logger log = Logger.getLogger(A_basics.class.getName());
log.debug("Hello this is an debug message");

This will compile fine but throw run time exception regarding appender.

Next add the folder containing log4j.properties file into classpath.


 Project->Properties->java build path->Libraries->AddClassFolder->
 Then select properties [this contains log4j.properties] folder and click Ok.

Concept:
The Logger class does not allow us to instantiate a new Logger instance but it
provides two static methods for obtaining a Logger object:
 public static Logger getRootLogger();
 public static Logger getLogger(String name);

Here the first of the two methods returns the application instance's root
logger and does not have a name.

Any other named Logger object instance is obtained through the second method
by passing the name of the logger.
The name of the logger can be any string you can pass, usually class or
package name. It is mentioned below:
static Logger log = Logger.getLogger(log4jExample.class.getName());

Log4j Configuration file – log4j.properties.

The log4j.properties file is a log4j configuration file which keeps properties


in key-value pairs. By default, the LogManager looks for a file named
log4j.properties in the CLASSPATH.

This file need to be in classpath


log4j supports UNIX-style variable substitution such as ${variableName}
Elements of log4j
Appenders: Appender objects are responsible for printing logging messages to
different destinations such as consoles, files, sockets, NT event logs, etc.
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out

# Define the layout for file appender


log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
possible options are: DateLayout, HTMLLayout
PatternLayout
SimpleLayout
XMLLayout

rootLogger: Define the destination of output of logger.


log4j.rootLogger = DEBUG, FILE, consoleAppender

Here log output goes to console and file.

Few more properties:


log4j.appender.FILE.MaxFileSize=2MB
log4j.appender.FILE.MaxBackupIndex=2

Logging Levels
 ALL All levels including custom levels.
 DEBUG Designates fine-grained informational events that are most useful
to debug an application.
 INFO Designates informational messages that highlight the progress of
the application at coarse-grained level.
 TRACE Designates finer-grained informational events than the DEBUG.
 WARN Designates potentially harmful situations.
 ERROR Designates error events that might still allow the application to
continue running.
 FATAL Designates very severe error events that will presumably lead the
application to abort.
 OFF The highest possible rank and is intended to turn off logging.

ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF.

This level can be set in configuration file as -


log4j.rootLogger = WARN
Once WARN is set here, all the DEBUG and INFO messages will be filter out.
Only Warn and above log message will be displayed.

Same can be achieved through code.


log.setLevel(Level.WARN);

TODO
FileAppender Configuration:

JDBCAppender Configuration:

You might also like