Log4j use example

zhaozj2021-02-11  157

Log4J Using Example - By Blues (zhaochaohua@sina.com) Part 1 Introduction Log4j is: 1. You can decide where the log information output is output by modifying the configuration file (console, file, ...), whether it is output . In this way, the detailed log information can be printed in the system development phase to track the system operation, and the log output can be turned off after the system is stable, so that the garbage code is reduced while being able to track the system operation (System.Out.println. ...)Wait). 2. Using log4j, you need a unified log mechanism throughout the system to facilitate the planning of the system. Log4j's use itself is simple. However, it is reasonable to plan a system's unified log mechanism that requires a comprehensive consideration. Other information about log4j is visible to the document comes with the log4j.

Part II profile detailed explanation First look at an example of a configuration file: 1. Example of the configuration file log4j.rootlogger = debug # Record the DAO layer log 4j.logger.dao = debug, A2, A4 # will logic Layers log records to Businesslog, alwork log4j.logger.businessLog = Debug, A3, A4 # A1 - Print to the screen log4j.Appender.a1 = org.apache.log4j.consoleAppenderlog4j.Appender.a1.Layout = Org.apache .log4j.patternLayoutLog4j.Appender.a1.Layout.conversionPattern =% - 5P [% T]% 37C% 3X -% M% N # a2 - Print to file DAOLOG - specifically for DAO layer service log4j.Appender.a2 = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.A2.file = DAOLoglog4j.appender.A2.DatePattern = '.' yyyy-MM-ddlog4j.appender.A2.layout = org.apache.log4j.PatternLayoutlog4j.appender.A2. Layout.conversionPattern = [% - 5P]% D {YYYY-MM-DD HH: mm: SS, SSS} Method:% L% N% M% N # A3 - Print to File BusinessLog - Specialized Record Logic Processing service log information layer log4j.appender.A3 = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.A3.file = BusinessLoglog4j.appender.A3.DatePattern = '.' yyyy-MM-ddlog4j.appender.A3.layout = org.apache .log4j.patternlayoutlog4j.appender.a3.Layout.conversionPattern = [% - 5P]% d {YYYY-MM-DD HH: mm: SS, SSS} Method:% L% N% M% N # a4 - play Printed onto the document alllog - log information recorded log4j.appender.A4 = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.A4.file = allloglog4j.appender.A4.DatePattern = yyyy-MM-ddlog4j.appender '.'. A4.Layout = org.apache.log4j.patternLayoutLog4j.Appender.a4.Layout.conversionPattern = [% - 5P]% d {YYYY-MM-DD HH: mm: SS, SSS} Method:% L% N% M% N2.Appender uses an appender representing a place to write to the log information. There are many types of Appenders that log4j can be used, here only 3: consOLLAPPENDER, FILEAPPENDER, DAILYROLLFILEAPPPPPOLLEROLLFILEAPPENDER2.1 ConsoleAppender If you use consOLLAPPENDER, then log information will be written to Console. Just print the information directly on System.out. 2.2 FileAppender Use FileAppender, then log information will be written to the specified file. This should be compared to frequently use. Accordingly, the file name of the log output should be specified in the configuration file.

The following configuration specifies the log file name as demo.txtLog4j.Appender.a2.file = demo.txt Note A2 is replaced with the alias of the appender in the specific configuration. 2.3 Daily RollingAppender Use FileAppender to output the log information to the file, but if the file is too big, it is inconvenient. You can use DailyRollingAppender at this time. DailyRollingAppender can output the log information to a file that is distinguished according to the date. The following configuration file will generate a log file daily, each log file only logs the log information on the day: log4j.Appender.a2 = org.apache.log4j.dailyrollingFileAppenderlog4j.Appender.a2.file = DEMOLOG4J.Appender.a2.datePattern = '.'yyy-mm-ddlog4j.Appender.a2.Layout = org.apache.log4j.patternlayoutLog4j.Appender.a2.Layout.conversionPattern =% m% N3.Layout configures Layout specifies the style of log information output. For more information, see Patternlayout's Javadoc. Example 1: Display date and log information log4j.appender.a2.Layout = org.apache.log4j.patternLayoutLog4j.Appender.a2.Layout.conversionPattern =% D {YYYY-MM-DD HH: mm: SS, SSS}% M % N printed information is: 2002-11-12 11: 49: 42, 866 Select * from role where 1 = 1 Order by create DESC Example 2: Display date, LOG, local and log information log4j.Appender.a2.Layout = Org .apache.log4j.patternLayoutLog4j.Padnder.a2.Layout.conversionPattern =% d {yyyy-mm-dd hh: mm: SS, SSS}% L "#"% M% N2002-11-12 11: 51: 46, 313 CN .NET.UNET.WEBOA.SYSTEM.DAO.ROLEDAO.SYSTEM.DAO.ROLEDAO.SYSTEM.DAO.ROLEDAO.SYSTELECT (Roledao.java:409) "#" Select * from role where 1 = 1 ORDER BY CREATEDATE DESC Example 3: Display LOG Level, Time, Call Method, LOG Information Log4j.Appender.a2.Layout = org.apache.log4j.patternLayoutLog4j.Appender.a2.Layout.conversionPattern = [% - 5P]% d {YYYY-MM-DD HH: mm: SS, SSS} Method:% L% N% M% NLOG information: [Debug] 2002-11-12 12: 00: 57, 376 Method: cnnet.unet.weboa.system.dao.roledao.System.dao.Roledao.select (Roledao.java: 409) Select * from role where 1 = 1 ORDER BY CREATEDATE DESC Part 3 LOG4J Using LOG4J Use 3: 3.1. Initialize the log4j configuration file as described in PART 2 according to the configuration file. Now how to configure log4j in the program. Log4j can be initialized using 3-in-one: Basicconfigurator, domconfigurator, PropertyConfigurator here is PropertyConfigurator.

Use PropertyConfigurator to apply to all systems. The following statement PropertyConfigurator.configure ("log4j.properties"); initializes the log4j environment with log4j.properties to the configuration file. Note that a little: This statement only needs to be executed once when the system is started. For example: can be used in the UNET Weboa project: call once in the init () method of the ActionServlet.

public class ActionServlet extends HttpServlet {... / *** Initialize global variables * / public void init () throws ServletException {// Initialization Action Resource try {initLog4j (); ...} catch (IOException e) {throw new ServletException ("Load ActionRes IS Error");}} ... protected "} ..." {propertyconfigurator.configure ("log4j.properties");} ...} // end class ActionServlet3.2 is available in a place where you need to use log4j Logger instance is an example of usage in the Roledao class: static logger log = logger.getlogger ("DAO"); note that the "DAO" identifier is used here, then the corresponding configuration information corresponding to the configuration file is as follows: # Defines DAO LoggerLog4j .logger.DAO = DEBUG, A2 # Appender A2 set of attributes log4j.appender.A2 = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.A2.file = demolog4j.appender.A2.DatePattern = '.' yyyy-MM-ddlog4j .appender.a2.Layout = org.apache.log4j.patternlayoutlog4j.appender.a2.Layout.conversionPattern =% - 5P% D {YYYY-MM-DD HH: mm: ss}% l% N% M% NPublic Class Roledao Extends basedbobject {... static logger log = logger.getlogger ("DAO"); ... public beancollection selectall () throws sqlexception {stringbuffer sql = new stringbuffer (sqlbuf_len); sql.append (" Select * from " Tablename " ORDER BY ROLDID "); // system.out.println (SQL.TOString ()); log.debug (sql); ...} ...} 3.3 Debug using Logger object , Info, Fatal ... Method Log.debug ("IT IS The Debug Info"); Annex 1: A bug of log4j When using this, DailyRollingFileAppender does not use correctly: public class roding () {static logger log = logger. GetLogger ("DAO"); // Execute a configure () operation during every New Roledao object, a Configure (TransactManager Transmgr) THROPERTYCONFIGURATOR.CONFIGURE ("log4j.properties"); ... } public void select () {... // uses log4j to log.debug ("...");

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

New Post(0)