Log4delphi is an open-source project designed to create a high-quality and practical log suite for borland. It is a log4j package based on the Apache Software Foundation.
Installation:
1. Extract from http://sourceforge.net/projects/log4delphi/files/download 0.8133 to the folder. Open the folder ".. \ log4delphi-0.8" and create a new folder "build ";
2. run Delphi 7, open the component package "log4delphi_d7_prof.dpk", located under "... \ log4delphi-0.8 \ SRC \ Delphi", click "compile" and "Install" to compile and install;
3. menu "Tools" → "Environment options" → "library" → "library path", add "... \ log4delphi-0.8 \ build "Directory (if you need to connect source code, then add "... \ log4delphi-0.8 \ SRC \ Delphi "and "... \ log4delphi-0.8 \ SRC \ Delphi \ util ");
Usage:
1. Create an application and save the current project.
2. Choose "project"> "View Source" and add initialization logs. The Code is as follows:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 |
|
Program project1; Uses Forms, Tconfiguratorunit, {need to reference this unit} Unit1 in 'unit1. pa' {form1 }; {$ R *. Res} Begin Application. initialize; Dopropertiesconfiguration ('log4delphi. properties'); {initialization, read attribute} Application. createform (tform1, form1 ); Application. Run; End. |
3. Copy ".. \ log4delphi-0.8 \ example \ log4delphi. properties" to the project directory;
4. place two buttons on the form, and click the event and the form destruction event as follows:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 |
|
Implementation Uses tloggerunit; {reference this unit} {$ R *. DFM} Procedure tform1.btn1click (Sender: tobject ); Begin Tlogger. getinstance. debug ('button 1 clicked '); {Get tlogger instance} End; Procedure tform1.btn2click (Sender: tobject ); Begin Tlogger. getinstance. Info ('button 2 is also clicked '); {includes fatal, error, warn, info, debug, trace} End; Procedure tform1.formdestroy (Sender: tobject ); Begin Tlogger. freeinstances; {release when not needed} End; |
5. Run the program. Click Buttons 1 and 2 to close the program. The "app. log" file is found in the project directory. open the file as follows:
6. Open the attribute file "log4delphi. properties" to view some default attributes and attribute descriptions. For example, you can set the log file layout. The original content is as follows:
1 2 3 4 5 6 |
|
Log4delphi. appender. fileappender. layout = tsimplelayout # Log4delphi. appender. fileappender. layout = txmllayout # Log4delphi. appender. fileappender. layout = thtmllayout # Log4delphi. appender. fileappender. layout. Title = document title # Log4delphi. appender. fileappender. layout = tpatternlayout # Log4delphi. appender. fileappender. layout. pattern = % d {dd Mmm yyyy hh: NN: SS: ZZZ} [% 5 p] % m % N |
By default, tsimplelayout is enabled, which is a simple layout in the same format as above. To output logs in time format, disable tsimplelayout and then enable tpatternlayout and its style, for example:
1 2 3 4 5 6 |
|
# Log4delphi. appender. fileappender. layout = tsimplelayout # Log4delphi. appender. fileappender. layout = txmllayout # Log4delphi. appender. fileappender. layout = thtmllayout # Log4delphi. appender. fileappender. layout. Title = document title Log4delphi. appender. fileappender. layout = tpatternlayout Log4delphi. appender. fileappender. layout. pattern = % d {dd Mmm yyyy hh: NN: SS: ZZZ} [% 5 p] % m % N |
7. Execute Step 1 again and open it, as shown in:
8. If the time format does not meet personal requirements, you can customize the format, specific can refer to the User Wizard (http://log4delphi.sourceforge.net/userguide.html ).
Address: http://blog.csdn.net/jhlz1968/article/details/3293493