Preface:
以前就有在用 Log4j, 但大多數情況是使用在 UI 應用程序, 最近要把寫好的 Web service 放上 Server, 就不能再用 System.out 這種方式來 trace 已經在線運行的服務, 因此又想起來 Log4j, 除了它可以透過 configuration 設定 Log 的 行為/Format/輸出 且它也支援 log 檔案 rotate 的功能 (設定 log 檔案到一定大小就進行 rotate, 這樣最後你會有一堆大小一致的 log, 而不是一個大到無法想想的 log 而影響到系統的運行). 下面文章是 Log4j 官網上面的教學文章, 相當實用!
Almost every large application includes its own logging or tracing API. In conformance with this rule, the E.U. SEMPER project decided to write its own tracing API. This was in early 1996. After countless enhancements, several incarnations and much work that API has evolved to become log4j, a popular logging package for Java. The package is distributed under the Apache Software License, a fully-fledged open source license certified by the open source initiative. The latest log4j version, including full-source code, class files and documentation can be found at http://logging.apache.org/log4j/. By the way, log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.
Logging does have its drawbacks. It can slow down an application. If too verbose, it can cause scrolling blindness. To alleviate these concerns, log4j is designed to be reliable, fast and extensible. Since logging is rarely the main focus of an application, the log4j API strives to be simple to understand and to use.
Loggers, Appenders and Layouts
Log4j has three main components: loggers, appenders and layouts. These three types of components work together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.
Logger hierarchy
Loggers are named entities. Logger names are case-sensitive and they follow the hierarchical naming rule:
For example, the logger named "com.foo" is a parent of the logger named "com.foo.Bar". Similarly, "java" is a parent of "java.util" and an ancestor of "java.util.Vector". This naming scheme should be familiar to most developers.
The root logger resides at the top of the logger hierarchy. It is exceptional in two ways:
Invoking the class static Logger.getRootLogger method retrieves it. All other loggers are instantiated and retrieved with the class static Logger.getLogger method. This method takes the name of the desired logger as a parameter.
Loggers may be assigned levels. The set of possible levels, that is:
are defined in the org.apache.log4j.Level class. Although we do not encourage you to do so, you may define your own levels by sub-classing the Level class. A perhaps better approach will be explained later on.
If a given logger is not assigned a level, then it inherits one from its closest ancestor with an assigned level. More formally:
To ensure that all loggers can eventually inherit a level, the root logger always has an assigned level.
Logging requests are made by invoking one of the printing methods of a logger instance. These printing methods are debug, info, warn, error, fatal and log. By definition, the printing method determines the level of a logging request. For example, if c is a logger instance, then the statement c.info("..") is a logging request of level INFO.
A logging request is said to be enabled if its level is higher than or equal to the level of its logger. Otherwise, the request is said to be disabled. A logger without an assigned level will inherit one from the hierarchy. For the standard levels, we have DEBUG < INFO < WARN < ERROR < FATAL.
Here is an example of this rule.
- // get a logger instance named "com.foo"
- Logger logger = Logger.getLogger("com.foo");
- // Now set its level. Normally you do not need to set the
- // level of a logger programmatically. This is usually done
- // in configuration files.
- logger.setLevel(Level.INFO);
- Logger barlogger = Logger.getLogger("com.foo.Bar");
- // This request is enabled, because WARN >= INFO.
- logger.warn("Low fuel level.");
- // This request is disabled, because DEBUG < INFO.
- logger.debug("Starting search for nearest gas station.");
- // The logger instance barlogger, named "com.foo.Bar",
- // will inherit its level from the logger named
- // "com.foo" Thus, the following request is enabled
- // because INFO >= INFO.
- barlogger.info("Located nearest gas station.");
- // This request is disabled, because DEBUG < INFO.
- barlogger.debug("Exiting gas station search");
Log4j makes it easy to name loggers by software component. This can be accomplished by statically instantiating a logger in each class, with the logger name equal to the fully qualified name of the class. This is a useful and straightforward method of defining loggers. As the log output bears the name of the generating logger, this naming strategy makes it easy to identify the origin of a log message. However, this is only one possible, albeit common, strategy for naming loggers. Log4j does not restrict the possible set of loggers. The developer is free to name the loggers as desired.
Appenders and Layouts
The ability to selectively enable or disable logging requests based on their logger is only part of the picture. Log4j allows logging requests to print to multiple destinations.In log4j speak, an output destination is called an appender. Currently, appenders exist for the console, files, GUI components, remote socket servers, JMS, NT Event Loggers, and remote UNIX Syslog daemons. It is also possible to log asynchronously.
More than one appender can be attached to a logger.
The addAppender method adds an appender to a given logger. Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy. In other words, appenders are inherited additively from the logger hierarchy. For example, if a console appender is added to the root logger, then all enabled logging requests will at least print on the console. If in addition a file appender is added to a logger, say C, then enabled logging requests for C and C's children will print on a file and on the console. It is possible to override this default behavior so that appender accumulation is no longer additive by setting the additivity flag to false.
The rules governing appender additivity are summarized below.
More often than not, users wish to customize not only the output destination but also the output format. This is accomplished by associating a layout with an appender. The layout is responsible for formatting the logging request according to the user's wishes, whereas an appender takes care of sending the formatted output to its destination. The PatternLayout, part of the standard log4j distribution, lets the user specify the output format according to conversion patterns similar to the C languageprintf function.
For example, the PatternLayout with the conversion pattern "%r [%t] %-5p %c - %m%n" will output something akin to:
The first field is the number of milliseconds elapsed since the start of the program. The second field is the thread making the log request. The third field is the level of the log statement. The fourth field is the name of the logger associated with the log request. The text after the '-' is the message of the statement.
Configuration
Inserting log requests into the application code requires a fair amount of planning and effort. Observation shows that approximately 4 percent of code is dedicated to logging. Consequently, even moderately sized applications will have thousands of logging statements embedded within their code. Given their number, it becomes imperative to manage these log statements without the need to modify them manually.
The log4j environment is fully configurable programmatically. However, it is far more flexible to configure log4j using configuration files. Currently, configuration files can be written in XML or in Java properties (key=value) format. Let us give a taste of how this is done with the help of an imaginary application MyApp that uses log4j.
- import com.foo.Bar;
- // Import log4j classes.
- import org.apache.log4j.Logger;
- import org.apache.log4j.BasicConfigurator;
- public class MyApp {
- // Define a static logger variable so that it references the
- // Logger instance named "MyApp".
- static Logger logger = Logger.getLogger(MyApp.class);
- public static void main(String[] args) {
- // Set up a simple configuration that logs on the console.
- BasicConfigurator.configure();
- logger.info("Entering application.");
- Bar bar = new Bar();
- bar.doIt();
- logger.info("Exiting application.");
- }
- }
- package com.foo;
- import org.apache.log4j.Logger;
- public class Bar {
- static Logger logger = Logger.getLogger(Bar.class);
- public void doIt() {
- logger.debug("Did it again!");
- }
- }
Note that by default, the root logger is assigned to Level.DEBUG. The output of MyApp is:
The figure below depicts the object diagram of MyApp after just having called the BasicConfigurator.configure method.
As a side note, let me mention that in log4j child loggers link only to their existing ancestors. In particular, the logger named com.foo.Bar is linked directly to the root logger, thereby circumventing the unused com or com.foo loggers. This significantly increases performance and reduces log4j's memory footprint. The MyApp class configures log4j by invoking BasicConfigurator.configure method. Other classes only need to import the org.apache.log4j.Logger class, retrieve the loggers they wish to use, and log away.
The previous example always outputs the same log information. Fortunately, it is easy to modify MyApp so that the log output can be controlled at run-time. Here is a slightly modified version.
- import com.foo.Bar;
- import org.apache.log4j.Logger;
- import org.apache.log4j.PropertyConfigurator;
- public class MyApp {
- static Logger logger = Logger.getLogger(MyApp.class.getName());
- public static void main(String[] args) {
- // BasicConfigurator replaced with PropertyConfigurator.
- PropertyConfigurator.configure(args[0]);
- logger.info("Entering application.");
- Bar bar = new Bar();
- bar.doIt();
- logger.info("Exiting application.");
- }
- }
Here is a sample configuration file that results in identical output as the previous BasicConfigurator based example:
- # Set root logger level to DEBUG and its only appender to A1.
- log4j.rootLogger=DEBUG, A1
- # A1 is set to be a ConsoleAppender.
- log4j.appender.A1=org.apache.log4j.ConsoleAppender
- # A1 uses PatternLayout.
- log4j.appender.A1.layout=org.apache.log4j.PatternLayout
- log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
- log4j.rootLogger=DEBUG, A1
- log4j.appender.A1=org.apache.log4j.ConsoleAppender
- log4j.appender.A1.layout=org.apache.log4j.PatternLayout
- # Print the date in ISO 8601 format
- log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
- # Print only messages of level WARN or above in the package com.foo.
- log4j.logger.com.foo=WARN
Here is another configuration file that uses multiple appenders.
- log4j.rootLogger=debug, stdout, R
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- # Pattern to output the caller's file name and line number.
- log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
- log4j.appender.R=org.apache.log4j.RollingFileAppender
- log4j.appender.R.File=example.log
- log4j.appender.R.MaxFileSize=100KB
- # Keep one backup file
- log4j.appender.R.MaxBackupIndex=1
- log4j.appender.R.layout=org.apache.log4j.PatternLayout
- log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
In addition, as the root logger has been allocated a second appender, output will also be directed to the example.log file. This file will be rolled over when it reaches 100KB. When roll-over occurs, the old version of example.log is automatically moved to example.log.1.
Note that to obtain these different logging behaviors we did not need to recompile code. We could just as easily have logged to a UNIX Syslog daemon, redirected all com.foo output to an NT Event logger, or forwarded logging events to a remote log4j server, which would log according to local server policy, for example by forwarding the log event to a second log4j server.
Default Initialization Procedure
The log4j library does not make any assumptions about its environment. In particular, there are no default log4j appenders. Under certain well-defined circumstances however, the static inializer of the Logger class will attempt to automatically configure log4j. The Java language guarantees that the static initializer of a class is called once and only once during the loading of a class into memory. It is important to remember that different classloaders may load distinct copies of the same class. These copies of the same class are considered as totally unrelated by the JVM.
The default initialization is very useful in environments where the exact entry point to the application depends on the runtime environment. For example, the same application can be used as a stand-alone application, as an applet, or as a servlet under the control of a web-server.
The exact default initialization algorithm is defined as follows:
The PropertyConfigurator will be used to parse the URL to configure log4j unless the URL ends with the ".xml" extension, in which case the DOMConfigurator will be used. You can optionaly specify a custom configurator. The value of the log4j.configuratorClass system property is taken as the fully qualified class name of your custom configurator. The custom configurator you specify must implement the Configurator interface.
Conclusions
Log4j is a popular logging package written in Java. One of its distinctive features is the notion of inheritance in loggers. Using a logger hierarchy it is possible to control which log statements are output at arbitrary granularity. This helps reduce the volume of logged output and minimize the cost of logging.
One of the advantages of the log4j API is its manageability. Once the log statements have been inserted into the code, they can be controlled with configuration files. They can be selectively enabled or disabled, and sent to different and multiple output targets in user-chosen formats. The log4j package is designed so that log statements can remain in shipped code without incurring a heavy performance cost.
沒有留言:
張貼留言