Log4j Introduction

xiaoxiao2021-03-06  38

First, log4j uses 1. Configure the operating environment, contains log4j-1.2.9.jar, and configure the running environment using BasicConfigurator.configure (). 2. Get the Logger instance using Logger.getLogger (TestLogginlevel.Class). 3, use Fatal (), Error (), Warn (), Info (), and Debug () print Log. Second, the logger log4j allows the programmer to define multiple recorders, each recorder has its own name, and the recorder is passed by the name (or family relationship). 1, Logger hierarchy. X.y's Logger's Logger's Logger's Logger Logger, X is X.y.z's ancestors logger. Logger loggerx = logger.getlogger (y.class); logger loggery = logger.getlogger (x.class); obtained the same Logger instance. 2, Logger-Level Filter. If the logger does not define a level, use its parent Logger level. The root Logger level is Debug. Level order: OFF> Fatal> Error> Warn> Info> Debug> ALL; You can use Logger. Setlevel (Level.Debug) to set Logger level. Level Filter: If the level

5. Object rendering, providing a pluggable object translator, which is to translate objects into corresponding messages. You can let you format the information that is not adding a toString () method in the object class. 1. The class of aspirations to translate the toString () method, you can use defaultrenderer. 2, your own object translator needs to implement org.apache.log4j.or.ObjectRenderer interface. 3. If the child object does not have a corresponding translator class, the translator class corresponding to the parent class will be used. 6. Perform timing of Logger. 1. Execute Hierarchy-Wide Threshold filtering. 2, perform Logger-Level filtering. 3, generate loggingevent. 4, call the appender. 5. Call Layout format LogGingEvent. 6. Return to formatted LogGingEvent, perform the corresponding LOG action in the appender. Seven, performance (already in harness) 1, Logging Remove performance: ISDeBuGenabled () and corresponding methods should be judged before writing log. Excellent: You can save some objects to call toString (). Visual: Two judgments will be made when logging is open. 2, how to logGing performance: Search from inheritance path to the time-time, it is recommended that the inheritance path should be short. 3, performance in logging: Physical hardware such as operating file systems is very time. Eight, XML configuration file 1, log4j first look for the default configuration file log4j.xml, if you don't find Log4j.properties. 2, log4j.xml is only supported by log4j1.2.7, and the previous version only supports log4j.properties. Nine, reread the configuration file. 1, it is a thread safe. 2, it is not all of the Apnder, Logger deletes, reconfigured in the previous configuration file, but refers to the Appender that will be involved in the new configuration file and delete it. 3, support incremental configuration. Note: Unable to re-renewal involves the configured content, once modified a logger configuration might close some appender. Ten, log4j's configuration is as seen, if the program is not configured log4j, he reported that some log4j did not initialize an error, and the user may not see such an error, which can be solved by the following functions: static void turnOffLogging () {Logger root = Logger.getRootLogger (); boolean rootIsConfigured = root.getAllAppenders () hasMoreElements ();. if (! rootIsConfigured) {root.setLevel (Level.OFF);}} eleven, Sevlet in the Log4j Configuration 1. It is recommended to place log4j-1.2.9.ja in a web-inf / lib / directory. The configuration file is placed in a web-inf / class, so that no application can be a separate configuration. Of course, if you want to use a configuration across the server, the configuration of the log4j is given to the application server such as JBoss, WebLogic. 2, if you cannot determine where to initialize log4j, you can use a sevlet to make initialization work.

转载请注明原文地址:https://www.9cbs.com/read-63708.html

New Post(0)