Delphi txt日志log


本文整理自网络,侵删。

 
{新建一个TXT文档}
Procedure NewTxt(FileName:String);
Var
    F : Textfile; {定义 F 为 Textfile}
Begin
    AssignFile(F,FileName); {将文件名与变量 F 关联}
    ReWrite(F); {创建Txt文档并命名为 “FileName ” }
    Closefile(F); {关闭文件 F}
End;

{先附上原内容在写入新内容}
Procedure AppendTxt(Str:String;FileName:String);
Var
    F:Textfile;
Begin
    AssignFile(F, FileName);
    Append(F); {附上原来的内容以免原内容被清空}
    Writeln(F, Str); {把内容 Ser 写入文件F }
    Closefile(F);
End;

// 记录日志
procedure WriteTxtLog(const strFileName, strContent: string);
var
    strDir: string;
begin
    try
        if (Trim(strFileName) = '') then
            exit;
        strDir := ExtractFileDir(strFileName);
        // 如果文件夹不存在,则创建
        if (not DirectoryExists(strDir)) then
        begin
            ForceDirectories(strDir);
            Application.ProcessMessages;
            Sleep(100);
            Application.ProcessMessages;
        end;
            
        // 如果文件不存在,则创建
        if (not FileExists(strFileName)) then
        begin
            NewTxt(strFileName);
            Application.ProcessMessages;
            Sleep(100);
            Application.ProcessMessages;
        end;

        AppendTxt(strContent, strFileName);
    except
        ;
    end;
end;

// 打印日志
procedure WriteLog(const strText : string);
var
    dt: TDateTime;
    strLogText, strLogFileName: string;
begin
    dt := Now();
    strLogText := FormatDateTime('yyyy-mm-dd hh:mm:ss', dt);
    strLogText := strLogText + ' ' + strText;
    frmMain.mmoLog.Lines.Add(strLogText);
    strLogFileName := FormatDateTime('yyyymmdd', dt);
    strLogFileName := g_strPath + 'log\' + strLogFileName + '.txt';
    WriteTxtLog(strLogFileName, strLogText);
end;

相关阅读 >>

Delphi逐个读取access中的数据

Delphi 利用tidhttp实现文件下载的分块断点续传

Delphi双进程监控

Delphi 对比两个文件是否相同的函数

Delphi ord chr byte等转换

Delphi is1251char

Delphi结构体指针的使用

Delphi 获取与设置系统环境变量

Delphi 简单的旋转图像角度代码

Delphi - system.runerror

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...