频道分类

Delphi 生成日志记录单元

作者:admin 来源: 日期:2019/10/3 21:36:12 人气: 标签:

 
调用方法:

uses untLog;


log.WriteLog('hello' );





{ ********日志单元******* }
{ }
{ Cobbler }
{ }
{ 版权所有 (C) 2011 Cobbler }
{ }
{ 功能说明: }
{ 日志记录 实现 ILog }
{ }
{ 创建日期:2011-07-11 }
{ 作者:Cobbler }
{ }
{ ******************************************************* }
unit untLog;

interface

uses
  SysUtils, Forms;

Type
  TLog = Class
  private
    procedure WriteToFile(const Msg: string);
  public
    Constructor Create;
    Destructor Destroy; override;
    procedure WriteLog(const Str: String);
    procedure WriteLogFmt(const Str: String; const Args: array of const );
    function GetLogFileName: String;
  End;

var
  flog: TLog=nil;

function log: TLog;

implementation

{ TLog }



function log: TLog;
begin
  if not Assigned(flog) then
    flog := TLog.Create;
  result:=flog;
end;

function TLog.GetLogFileName: String;
var
  Logpath: String;
begin
  Logpath :=ExtractFileDir(ParamStr(0))+ '\Logs\';
  if not DirectoryExists(Logpath) then
    ForceDirectories(Logpath);
  Result := Logpath + FormatDateTime('YYYY-MM-DD', Now) + '.log';
end;

constructor TLog.Create;
begin

end;

destructor TLog.Destroy;
begin

  inherited;
end;

procedure TLog.WriteLog(const Str: String);
begin
  WriteToFile(Str);
end;

procedure TLog.WriteLogFmt(const Str: String; const Args: array of const );
begin
  WriteToFile(Format(Str, Args));
end;

procedure TLog.WriteToFile(const Msg: string);
var
  FileName: String;
  FileHandle: TextFile;
begin
  FileName := GetLogFileName;
  Assignfile(FileHandle, FileName);
  try
    if FileExists(FileName) then
      Append(FileHandle)
    else
      ReWrite(FileHandle);

    WriteLn(FileHandle, FormatDateTime('[HH:MM:SS]', Now) + '  ' + Msg);
  finally
    CloseFile(FileHandle);
  end;
end;

end.