Log4delphi use experience

xiaoxiao2021-04-01  212

Log4D is a Delphi open source sub-project under Apache, its design ideas comes from log4j, but not Log4J's sub-project, the main goal is to develop log output in the environment and production environment.

First, download https://sourceforge.net/project/showfiles.php?group_id=145326 2, install the log4delphi / src / delphi / log4delphi_d6.dpk package under the source code is installed under the IDE. Third, Basic Usage 1. Creating a Logger instance in the Source of the DPR file (tconfiguratorunit.dobasicconfiguration), such as: Application.initialize; // Use the runtime default configuration, the output log file name is 'log4delphi.log' tconfiguratorunit.dobasicConfiguration; Application .CreateForm (TForm1, Form1); Application.Run; 2. configuration Loggervar logger: TLogger; begin logger: = TLogger.getInstance; logger.setLevel (TLevelUnit.INFO); logger.addAppender (TFileAppender.Create ( 'C: / test .log ')); 3. Write the log information in the local area according to the level of Level, there are five types of LOG information logger.debug (' debug message '); logger.info (' Info Message '); Logger. Warn ('Warn Message'); Logger.Error ('Error Message'); Logger.Fatal ('Fatal Message'); 4. Release the Logger Instance Finalization Tlogger.FreeInstances; 4, more in-depth introduction Log4d by five cores Components composition: levels, loggingevents, layouts, appenders and loggers.1. levels. Every log event has a priority, which is used to represent priority. Therefore, each priority is actually a numerical constant for comparison. Level has the following: TlevelUnit.off TlevelUnit.fatal Tlevelunit.Error TlevelUnit.warn Tlevelunit.info TlevelUnit.debug TlevelUnit.all2.LogGingEvents. When a log information is created, it occurs when it is created. Logging includes important information: time, message content, exceptions and priorities that have occurred. 3. It is responsible for the formatting output style of loggingevents. The TLayout following methods: function format (event: TLoggingEvent): String; Virtual; Abstract; function getContentType (): String; Virtual; function getHeader (): String; Virtual; function getFooter (): String; Virtual; function ignoresException ( : Boolean; Virtual; common layout has: l TsImpleLayout output such as: Debug - Button Clickedl Tpatternlayout. It provides more control for formatting output. The formatted mode is similar to the format function, that is, the beginning of the bill, then follow the modifier and the conversion.

Such as: pattern = "% D [% 5P]% m% N" is 12/20/2005 4:53:33 PM [debug] debug message12 / 20/2005 4:53:33 PM [info] info Message12 / 20/2005 4:53:33 PM [WARN] WARN Message12 / 20/2005 4:53:33 PM [Error] Error Message12 / 20/2005 4:53:33 PM [Fatal] Fatal Message Meaning:% D Date% M Message Content% N Rowing% P Priority% e Abnormal Class Name and Message% L logger name 4. appenders. Representing the purpose of the output is the console, file, or database. Commonly used TFileAppender, TDBAppender five programs in the profile 1. Load Profile Application.Initialize; TConfiguratorUnit.doPropertiesConfiguration ( 'log4delphi.properties'); Application.CreateForm (TForm1, Form1); Application.Run;

2. Write the configuration file 'log4delphi.properties'l control the output of information category # Set this to true to turn on Log4Delphi's internal # logginglog4delphi.debug = false log4delphi.info = false log4delphi.all = truel Root Logger # Set the root logger's priority threshold to DEBUG and assign an # appender named "fileAppender" to it.log4delphi.rootLogger = DEBUG, fileAppenderl configuration appender # Specify the appender class for fileAppender.log4delphi.appender.fileAppender = TFileAppender # Specify which file fileAppender should use.log4delphi.appender .fileAppender.File = app.logl disposed layout # Specify the layout class for fileAppender.log4delphi.appender.fileAppender.layout = TSimpleLayout # Use TPattern layoutlog4delphi.appender.fileAppender.layout = TPatternLayoutlog4delphi.appender.fileAppender.layout.pattern =% d [% 5P]% M% N

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

New Post(0)