Re: Logging components?
- From: "Danijel Tkalcec" <dtkalcec@xxxxxxxxxxx>
- Date: Tue, 17 May 2005 07:59:39 +0200
I think Logging is pretty much individual, so that usualy everyone has their
own version.
I've used different versions of log units over the past years, most of them
simply for writing into a textfile.
For logging database actions, it depends on the database structure and
things you want to log (also, where you want to log them).
I think its best to log everything you think is worth logging,
then have a separate program to summarize this information on stuff you feel
like summarizing.
Here is the log-writing unit I use for most apps today.
It is simple to use, thread-safe and there are no special preparations
needed
(call "StartLog" to enable, "StopLog" to disable logging and "Log"/"XLog" to
add to the log).
You could create a class from this, but it's easier to use as set of
procedures.
----------------------------------------
{
@html(<b>)
Log File Creation
@html(</b>)
- Copyright (c) Danijel Tkalcec
@html(<br><br>)
This unit gives you thread-safe Log writing support.
}
unit rtcLog;
interface
uses
SysUtils, SyncObjs, Windows;
var
{ Write Logged exception into the Log file?
Dafault=True. By changing this to False will remove any
Connection component exceptions from the Log file. }
LOG_EXCEPTIONS:boolean=True;
{ Write exception with a short string description into the Global App Log
file.
This procedure will have no effect if Log writer not started
(by calling StartLog) or LOG_EXCEPTIONS is @false }
procedure Log(s:string; E:Exception; const name:string=''); overload;
{ Write message into the Global App Log file.
This procedure will have no effect if Log writer not started. }
procedure Log(s:string; const name:string=''); overload;
{ Write message into the Log file for the current date.
This procedure will have no effect if Log writer not started. }
procedure XLog(s:string; const name:string=''); overload;
{ Before Log() procedures will have any effect,
you have to call this procedure to start the Log writer.
Without it, no Log file. }
procedure StartLog;
{ To stop Log file creation, simply call this procedure.
To continue log writing, call StartLog. }
procedure StopLog;
implementation
var
ThrCS:TCriticalSection;
doLog:boolean=False;
procedure StartLog;
begin
ThrCS.Enter;
try
doLog:=True;
finally
ThrCS.Leave;
end;
end;
procedure StopLog;
begin
ThrCS.Enter;
try
doLog:=False;
finally
ThrCS.Leave;
end;
end;
procedure XLog(s:string; const name:string='');
var
f:TextFile;
d:TDateTime;
fname,
s2:string;
begin
ThrCS.Enter;
try
if not doLog then Exit; // Exit here !!!!
try
d:=Now;
s2:=FormatDateTime('[yyyy-mm-dd hh:nn:ss] ',d);
if name<>'' then
fname:=ParamStr(0)+'.'+FormatDateTime('yyyy_mm_dd',d)+'.'+name+'.log'
else
fname:=ParamStr(0)+'.'+FormatDateTime('yyyy_mm_dd',d)+'.log';
Assign(f,fname);
{$I-}
Append(f);
{$I+}
if IOREsult<>0 then
Rewrite(f);
try
Writeln(f,s2+s);
finally
CloseFile(f);
end;
except
end;
finally
ThrCS.Leave;
end;
end;
procedure Log(s:string; const name:string='');
var
f:TextFile;
d:TDateTime;
fname,
s2:string;
begin
ThrCS.Enter;
try
if not doLog then Exit; // Exit here !!!!
try
d:=Now;
s2:=FormatDateTime('[yyyy-mm-dd hh:nn:ss] ',d);
if name<>'' then
fname:=ParamStr(0)+'.'+name+'.log'
else
fname:=ParamStr(0)+'.log';
Assign(f,fname);
{$I-}
Append(f);
{$I+}
if IOREsult<>0 then
Rewrite(f);
try
Writeln(f,s2+s);
finally
CloseFile(f);
end;
except
end;
finally
ThrCS.Leave;
end;
end;
procedure Log(s:string; E:Exception; const name:string='');
begin
if LOG_EXCEPTIONS then
Log(s+' Exception! '+E.ClassName+': '+E.Message, name);
end;
initialization
ThrCS:=TCriticalSection.Create;
finalization
ThrCS.Free;
end.
-----------------------------------------
This unit is part of RealThinClient component package:
www.realthinclient.com
XLog() is for creating daily logs (every day, a new log file), while Log()
is for writing global logs.
By using the "name" parameter, you can use different files for different
parts of your application,
or different kinds of logs: error, warning, information, etc.
This should be simple enough to understand and powerful enough to suit most
apps needs.
Best Regards,
Danijel Tkalcec
"Lauchlan M" <LMackinnonAT_NoSpam_ozemailDOTcomDOTau> wrote in message
news:42894f74@xxxxxxxxxxxxxxxxxxxxxxxxx
> It's simple enough to do anyway, but out of interest:
>
> What logging components are there out there?
>
> Do people use them?
>
> What advantages do they offer?
>
> Which ones do you use?
>
> Basically I mean something that would take a handled exception and log
> whatever details you give it to file. Normally I'd just log to a text
> file,
> but if I used a logging component I guess it would be better if it gave me
> options, eg to text file, XML, binary, database, etc.
>
> It should do all the obvious and normal things, eg record the error
> name/category and details I pass to it, attach a timestamp, attach a
> userID
> if appropriate, etc.
>
> If it were reasonably clever, it would have logic to allow you to
> configure
> whether to record every error, or just the first instance of each (perhaps
> with a running total of how many times and how frequently it has been
> occuring).
>
> Or maybe everyone's logging requirements are individual/specific anyway,
> so
> everyone just rolls their own.
>
> By logging components, I specifically DON'T MEAN components for catching
> UNHANDLED exceptions like MadExcept, EurekaLog, etc. I'm interested in
> what
> you do (or don't) use to log HANDLED exceptions and errors.
>
> Thanks
>
> Lauchlan M
>
>
.
- References:
- Logging components?
- From: Lauchlan M
- Logging components?
- Prev by Date: Logging components?
- Next by Date: Re: Logging components?
- Previous by thread: Logging components?
- Next by thread: Re: Logging components?
- Index(es):
Relevant Pages
|