如何创建和读写LOG文件

卡卡西 2007-01-21 12:50:06
我在一个应用程序中使用日志来记录程序运行中发生的异常等信息.想使用LOG文件做为日志文件.不需要如何创建和读写.
...全文
1532 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
caoguoqing209 2007-01-31
  • 打赏
  • 举报
回复
procedure TfrmMain.WriteLog(str: string);
var
F:textfile;
filename: string;
begin
if trim(str)<>'' then
try
filename := exepath+FormatDateTime('yyyy-mm-dd',now)+'.log';
assignfile(f,filename);
if FileExists(filename) then reset(f) else rewrite(F);
append(f);
writeln(f,trim(str)+'->'+FormatDateTime('HH:MM:SS',now));
flush(f);
finally
closefile(f);
end;
end;
cn_tigers 2007-01-27
  • 打赏
  • 举报
回复
以前寫的,
strLog:日志內容;
modlevel:本模塊的跟蹤級別;
sysloglevel:系統跟蹤級別;
(用戶版的sysloglevel就小點,測試版的sysloglevel就大點.modlevel就跟據自已需要定義)

procedure WriteLog(strLog:string;ModLevel,SysLogLevel:integer);
var
outFile:TextFile;
i:integer;
sLog:string[128];
acLog:array[0..159] of char;
sLogFile:string;
bNewFile:boolean;
begin
if ModLevel>SysLogLevel then exit;

for i:=ModLevel-1 downto 0 do begin //Indent
sLog:=Format('%s%s',[' ',sLog]);
end;

sLog:=FormatDateTime('YYYY-MM-DD HH:MM:SS:zzz',Now)+#9+strLog;
if length(sLog)>158 then sLog:=copy(sLog,1,158);
fillMemory(@acLog,160,ord(' '));
for i:=0 to length(sLog)-1 do begin
acLog[i]:=sLog[i+1];
end;
sLogFile:='Log\SmartUp_'+formatdatetime('MMDD',Now)+'.log';
bNewFile:= FileExists(sLogFile);

AssignFile(outFile,sLogFile);
if Not bNewFile then ReWrite(outFile)
else Append(outFile);

WriteLn(outFile,acLog);
CloseFile(outFile);
end;
Sunniness 2007-01-27
  • 打赏
  • 举报
回复
TApplicationEvents 这个控件可以捕获所有错误,在additionial页上,捕获代码我日志就写在onexception 事件里就可以了!
hsgrass37301 2007-01-24
  • 打赏
  • 举报
回复
// 如果你用心学一下PASCAL的基础知识就会看明白的.

// astr是需要记录的字符串内容, FN: 日志文件的名称, 默认为程序名称后加log.txt
procedure LogToFile(astr: string; fn: string = 'AppLog');
var
f: TextFile; // 声明文件类型的变量
begin
astr := formatdatetime('yy/mm/dd HH:nn', now) + ':'+ astr; // 在字符串前加上日期
if fn = 'AppLog' then
fn := ChangeFileExt(GetModuleName(HInstance), '.' + formatdatetime('yymmdd', now) + '.log.txt'); // 如果不指定文件名称,则使用程序名称加.log.txt为日志名称
AssignFile(f, fn); // 关联文件变量到指定的文件名
try
if FileExists(fn) then
Append(f) // 如果文件已经存在则设为追加模式
else Rewrite(f); //否则重写一个新的文件.
Writeln(f, astr); // 写入内容到日志文件
{ insert code here that would require a Flush before closing the file }
Flush(f); { ensures that the text was actually written to file 刷新缓冲,确保内容已经写入文件}
finally
CloseFile(f); // 关闭打开的文件
end;
end;
constantine 2007-01-24
  • 打赏
  • 举报
回复
http://www.delphifans.com/SoftView/SoftView_1065.html
看看这个例子吧,支持多线程
hsgrass37301 2007-01-22
  • 打赏
  • 举报
回复
procedure LogToFile(astr: string; fn: string = 'AppLog');
var
f: TextFile;
begin
astr := formatdatetime('yy/mm/dd HH:nn', now) + ':'+ astr;
if fn = 'AppLog' then
fn := ChangeFileExt(GetModuleName(HInstance), '.' + formatdatetime('yymmdd', now) + '.log.txt');
AssignFile(f, fn);
try
if FileExists(fn) then
Append(f)
else Rewrite(f);
Writeln(f, astr);
{ insert code here that would require a Flush before closing the file }
Flush(f); { ensures that the text was actually written to file }
finally
CloseFile(f);
end;
end;
zuoansuifeng 2007-01-22
  • 打赏
  • 举报
回复
其实就相当于操作.txt文件一样
你换下后缀不就行了?
卡卡西 2007-01-22
  • 打赏
  • 举报
回复
感谢:hsgrass37301(零点) 和 zuoansuifeng(左岸) 的帮助.
请问零点:
你发的程序我有点看不太明白,能否将主要的地方加上注释啊?我是菜鸟.
还有我看了半天这段程序到底是做什么用的我都没看明白.LOGTOFILE是将LOG文件转化为普通文件吗?参数又是做什么用的呀?总之是不明白呀.
卡卡西 2007-01-21
  • 打赏
  • 举报
回复
不知道如何创建和读写

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧