原创 DLL编写经验总结(二) 日志的使用

本文介绍了日志的分类及其作用,包括运行日志和调试日志,并详细解释了日志如何帮助区分责任、查找问题及统计访问量。此外,还提供了具体的日志实现代码示例,说明了如何有效地管理和使用日志。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一、日志的分类

日志分为运行日志和调试日志,运行日志记录运行情况帮助使用过程中出现故障的分析,需要简单明了,为使得实施人员甚至客户能看懂,难免啰嗦。

调试日志是系统测试上线过程中程序员方便进一步了解情况而写的日志,有部分比较生涩难懂,但是精简高效。

二、日志的作用

说到底,日志是为人服务的,而且使用范围广,具有通用性。

对于DLL的日志

1、可以用于区分责任,特别是不同厂家之间经常会因为某些东西而相互推脱、争论,日志可以区分责任与原因

2、帮助程序员查找问题点,发现问题,统计访问量等

三、使用日志需要注意的点

对于运行日志和部分调试日志内容可能比较多。

1)需要注意防止同一个目录下不能有太多文件,否则会引起的操作系统缓慢的情况。

2)需要注意单个日志文件不能太大,否则增加读写困难,降低整个软件性能。

procedure _log(logStr:string);overload;
procedure _log(logStr:string;datatype:string);overload;

const LOG_SUB_DIR='log\';

constNO_LOG='FALSE';

procedure _log(logStr:string);

var 

  sysDir : string;

  logfile: string;

  mylog :TextFile;

begin

  if NO_LOG = 'TRUE' thenExit;

 sysDir:=ExtractFilePath(Application.ExeName)+LOG_SUB_DIR+formatdatetime('yyyyy-mm-dd',Now())+'\';

 logfile:=sysDir+'dataup_'+formatdatetime('yyyyy-mm-dd_hh', Now()) +'.log'; // 在字符串前加上日期

  try

    try

      if notdirectoryexists(sysdir) then

       ForceDirectories(sysdir);

       AssignFile(mylog,logfile);

      iffileexists(logfile) then 

        append(mylog)

      else 

       rewrite(mylog);

      Writeln(mylog,'['+datetimetostr(now())+'] '+#9+logStr);

    except

 

    end;

  finally

    CloseFile(mylog);

  end;

end;

改进版的日志,能帮助提取特殊日志,方便做分析

procedure _log(logStr:string;datatype:string);
var  
  sysDir : string;
  logfile: string;
  mylog :TextFile;
begin
  if NO_LOG = 'TRUE' then Exit;
  sysDir:=ExtractFilePath(Application.ExeName)+LOG_SUB_DIR+formatdatetime('yyyyy-mm-dd', Now())+'\'+datatype+'\';
  logfile:=sysDir+'dataup_'+formatdatetime('yyyyy-mm-dd_hh', Now()) + '.log'; // 在字符串前加上日期
  try
    try
      if not directoryexists(sysdir) then
        ForceDirectories(sysdir);
        AssignFile(mylog,logfile);
      if fileexists(logfile) then  
        append(mylog)
      else  
       rewrite(mylog);
       Writeln(mylog,'['+datetimetostr(now())+'] '+#9+logStr);
    except

    end;
  finally
    CloseFile(mylog);
  end;
end;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值