QuickLogger icon indicating copy to clipboard operation
QuickLogger copied to clipboard

Get log info

Open shineworld opened this issue 1 year ago • 6 comments

This is not an issue but a question.

I'm trying the logger and my events are logged and saved automatically to a file. OK ! I'm need to access to logger data to create graphical report.

It's possible to access to logger data (directly on logger) instead to open log file ?

shineworld avatar Sep 23 '24 16:09 shineworld

Logger only write/send logs to providers. If you want to show logs in your app directly, you could use Event Provider to redir it to a TMemo o something similar, additionaly to log file.

exilon avatar Sep 25 '24 21:09 exilon

At this point how do you frame, what purpose does it serve, the Quick.Logger.Provider.Memory ? Is it possible to use it to retrieve created logs from memory ?

Basically, in my application, I would like to be able to quickly read created logs, even in memory in the Quick.Logger.Provider.Memory to build a timing report between a log of one type and one of the same type.

shineworld avatar Sep 30 '24 16:09 shineworld

Quick.Logger.Provider.Memory internally uses a TObjectList. You can access to "memory" logs with method GlobalLogMemoryProvider.AsStrings and iterate items.

exilon avatar Sep 30 '24 18:09 exilon

I've tried this very simple code but result is always and empty TStrings (TStringList):

program Project10;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.Classes,
  System.SysUtils,

  Quick.Logger,
  Quick.Logger.Provider.Memory;


var
  I: Integer;
  Logs: TStrings;

begin
  try
    // inits global log memory provider
    with GlobalLogMemoryProvider do
    begin

      MaxSize := 1000;
      Init;
      LogLevel := LOG_ALL;
      Enabled := True;
    end;

    // logs something (just a test)
    with GlobalLogMemoryProvider do
    begin
      Log('001 = start', etInfo);
      Log('002 = start', etInfo);
      Log('001 = stop', etInfo);
    end;

    // gets back logs (!!! returns always an empty TStringList)
    Logs := GlobalLogMemoryProvider.AsStrings;
    try
      WriteLn('Retrived logs = ' + IntToStr(Logs.Count));
      for I := 0 to Logs.Count - 1 do
        WriteLn(Logs.Strings[I]);
    finally
      Logs.Free;
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

shineworld avatar Oct 01 '24 06:10 shineworld

Your code isn't correct. Please see how QuickLogger must be used. The concept is QuickLogger has a internal queue and it will dequeue messages to every provider in background to not affect you app performance. If you check just before messages are enqueued, no messages in the provider (Memory in this case):

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.Classes,
System.SysUtils,

Quick.Logger,
Quick.Logger.Provider.Memory;

var
I: Integer;
Logs: TStrings;

begin
  try
    // inits global log memory provider
    with GlobalLogMemoryProvider do
    begin
      MaxSize := 1000;
      LogLevel := LOG_ALL;
      Enabled := True;
    end;

    Logger.Providers.Add(GlobalLogMemoryProvider);

    // logs something (just a test)
      Logger.Info('001 = start');
      Logger.Info('002 = start');
      Logger.Info('001 = stop');

      //you need to wait to queue writes logs to memory
      sleep(1000);
    // gets back logs (!!! returns always an empty TStringList)
    Logs := GlobalLogMemoryProvider.AsStrings;
    try
      WriteLn('Retrived logs = ' + IntToStr(Logs.Count));
      for I := 0 to Logs.Count - 1 do
        WriteLn(Logs.Strings[I]);
    finally
      Logs.Free;
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

exilon avatar Oct 07 '24 20:10 exilon

Thank you very much. I guess at the end is neccessary to free providers, otherwise I always get an error!

program Project10;

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.Classes,
System.SysUtils,

Quick.Logger,
Quick.Logger.Provider.Memory;

var
I: Integer;
Logs: TStrings;

begin
  try
    // inits global log memory provider
    with GlobalLogMemoryProvider do
    begin
      MaxSize := 1000;
      LogLevel := LOG_ALL;
      Enabled := True;
    end;

    Logger.Providers.Add(GlobalLogMemoryProvider);

    // logs something (just a test)
    Logger.Info('001 = start');
    Logger.Info('002 = start');
    Logger.Info('001 = stop');

    //you need to wait to queue writes logs to memory
    sleep(2000);

    // gets back logs (!!! returns always an empty TStringList)
    Logs := GlobalLogMemoryProvider.AsStrings;
    try
      WriteLn('Retrived logs = ' + IntToStr(Logs.Count));
      for I := 0 to Logs.Count - 1 do
        WriteLn(Logs.Strings[I]);
    finally
      Logs.Free;
    end;

    Logger.Providers.Clear;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;

end.

shineworld avatar Oct 08 '24 06:10 shineworld