Logger is the main class for the user. Appender is an interface,
Usually implemented with the ConsoleAppender class, the FileAppender class, the RollingFilleAppender class. Layout is an abstract class, a PatternLayout class, a SimpleLayout class, an HTMLLayout class implementation.
Logger - Log Writer for programmer output log information Appender - log destinations, output formatted log information to the specified place
ConsoleAppender - Destination for the console's appender FileAppender - Appender RollingFileAppender for the destination for files - Destination for the appender layout - log format of the size of the size, used to format the programmer's Logging Request format string into a string
PatternLayout - Format Layout SimpleLayout in the specified Pattern - Format LayoutHtmlLayout in the specified SIMPLELAYOUT - Format Layout with the specified HTML format LOGGING Request
Programmers hold Logger objects to control logs, Logger is going on so step a few steps:
1, instantiate the logger object Logger logger
2, set the Logger's Level, ---- Use the setlevel method in the Logger class
3. Specify which appender used to use Logger, ---- use the Logger class
AddaPnder (Appender Appender) method, instantiate an Appender object before using this method.
To instantiate the Appender object, instantiate a Layout object first.
Write a simple example using the ConsoleAppender class, FileAppender class, SimpleLayOut, HTMLLayout class
Logger = logger.getlogger (COMM0000INITACTION.CLASS);
Logger.SetLevel ((Level) Level.debug;
String Pattern =% D {ISO8601} -% P -% M% N;
// Select PatternLayout format output.
PatternLayout Layout = New PatternLayout (Pattern);
FileAppender appender = null;
Try {
// Select FileAppender as a file format for the storage log
Appender = New FileAppender (Layout, "Test.log");
} catch (exception e) {}
Logger.Addappender (appender);
Logger.Error ("error");
Logger.debug ("debug");
Logger.warn ("WARN");
Logger.info ("info");
Logger.trace ("trace");
Program execution results:
2004-12-14 09: 46: 14,514 - Error - Error
2004-12-14 09: 46: 14,564 - Debug - Debug
2004-12-14 09: 46: 14,564 - WARN - WARN
2004-12-14 09: 46: 14,564 - Info - info
2004-12-14 09: 46: 14,564 - Debug - Trace
Using Logger.Error ("ERROR"); all the operations of all Logger before statement are called initializing log4j environments. These operations can be done using text files or XML files. XML configuration file example: configurationfile.xml
XML Version = "1.0" encoding = "UTF-8"?>
appender>
root>
log4j: Configuration>
Use org.apache.log4j.xml.Domconfigurator. Configure ("configurationfile.xml"); method reads the configuration information file into the Java program.
Public class externalXmltest {
Static logger logger = logger.getlogger (ExternalXMLTest.class);
Public static void main (string args []) {
Domconfigurator.configure ("xmllog4jconfig.xml);
Logger.debug ("Here Is Some Debug");
}
}