Creating a custom LoggingFormatter

The official tester's forum for ZeosLib 7.1. Ask for help, post proposals or solutions.
Post Reply
User avatar
Sergiomaster
Fresh Boarder
Fresh Boarder
Posts: 24
Joined: 13.06.2011, 12:37

Creating a custom LoggingFormatter

Post by Sergiomaster »

Hello,

Still writing my ZEOS tutorial after a long pause (:prog2: full time)
I am trying to use a loggingformatter different of the default one , using the function TZLoggingEvent.AsString(LoggingFormatter) . But i d'nt undertand how to do that ?

How and where do i declare this formatter ?

for sure i have a turn around (using procedure ZSQLMonitor1LogTrace event and event values )

Code: Select all

procedure TTransactionForm.ZSQLMonitor1LogTrace(Sender: TObject;
  Event: TZLoggingEvent);
  
var myFormat : String;  
begin
myFormat:=Format('%s %s',[FormatDateTime('dd-mm-yy hh:mm:ss', Event.Timestamp),event.msg] );
Memo1.lines.add(myformat);
end;
but i'll appreciate to use this fonction Event.AsString(myFormatter)

Code: Select all

procedure TTransactionForm.ZSQLMonitor1LogTrace(Sender: TObject;
  Event: TZLoggingEvent);
begin
Memo1.lines.add(Event.asString(myformatter));
end;


Serge
Main uses Delphi3/7/2010/Rio 10.3/Sidney 10.4 + Interbase/Firebird
User avatar
Sergiomaster
Fresh Boarder
Fresh Boarder
Posts: 24
Joined: 13.06.2011, 12:37

Re: Creating a custom LoggingFormatter

Post by Sergiomaster »

[Solved]
I use this code

Code: Select all

type
   ...
  MyLoggingFormatter = class (TInterfacedObject, IZLoggingFormatter)
  private
  public
    function Format(LoggingEvent: TZLoggingEvent) : string; virtual;
  end;

var
  ...
  myFormatter : MyLoggingFormatter;

implementation
...
{ MyLoggingFormatter }

function MyLoggingFormatter.Format(LoggingEvent: TZLoggingEvent): string;
begin
result:='Transaction '+formatDateTime('hh:nn:sss',loggingEvent.Timestamp)+' '+LoggingEvent.Message;
end;
...
// Usage
procedure TForm1.ZSQLMonitor1LogTrace(Sender: TObject;
  Event: TZLoggingEvent);
begin
Memo1.lines.add(Event.asString(myFormatter));
end;
initialization
  MyFormatter := MyLoggingFormatter.Create;

finalization
//  if assigned(myFormatter) then MyFormatter.Free;
the only remaining "problem/bug" is the finalization raising an unassigned error but :
- this is not the main unit
- the form of the unit is destroyed before closing the project so perhaps it should be better to free the "formatter" on closing form
Main uses Delphi3/7/2010/Rio 10.3/Sidney 10.4 + Interbase/Firebird
marsupilami
Platinum Boarder
Platinum Boarder
Posts: 1956
Joined: 17.01.2011, 14:17

Re: Creating a custom LoggingFormatter

Post by marsupilami »

Hello Sergiomaster,

in your example MyLoggingFormatter inherits from TInterfacedObject and IZLoggingFormatter. In doing it this way you don't neet to free it explicitly because it gets freed automatically as soon as the last reference to its IZLoggingFormatter is destroyed.
With best regards,

Jan
Post Reply