一、日志的分类
日志分为运行日志和调试日志,运行日志记录运行情况帮助使用过程中出现故障的分析,需要简单明了,为使得实施人员甚至客户能看懂,难免啰嗦。
调试日志是系统测试上线过程中程序员方便进一步了解情况而写的日志,有部分比较生涩难懂,但是精简高效。
二、日志的作用
说到底,日志是为人服务的,而且使用范围广,具有通用性。
对于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;