Page 1 of 1

Creating a custom LoggingFormatter

Posted: 12.02.2015, 09:17
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

Re: Creating a custom LoggingFormatter

Posted: 17.02.2015, 09:39
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

Re: Creating a custom LoggingFormatter

Posted: 23.02.2015, 18:11
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