Log4Delphi日志学习

发布时间 2023-11-16 16:09:51作者: 涂磊

转载请注明出处:https://www.cnblogs.com/coder163/p/9309717.html

https://log4delphi.sourceforge.net/tutorial.html

Log4D下载:

官网地址

导入Delphi:Tool-->Options-->Environment Options--->Delphi Options--Library-->Library path

 三个目录


使用

载入配置文件

菜单--->Project-->View Source

uses
  Vcl.Forms,
  TConfiguratorUnit, {需要引用该单元}
  Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  doPropertiesConfiguration('log4delphi.properties'); { 初始化,读取属性 }
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

拷贝

 到exe所在目录

 

unit Unit2;

interface

uses
 TLoggerUnit, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
TLogger.getInstance.debug('Button Clicked!');
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
TLogger.freeInstances;
end;

end.
    1. Compile and run the application. Click the button three times and then exit the application.
    2. You should have a log file in the tutorial folder named app.log with the following content
      DEBUG - Button Clicked
      DEBUG - Button Clicked
      DEBUG - Button Clicked