Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get log info #116

Open
shineworld opened this issue Sep 23, 2024 · 6 comments
Open

Get log info #116

shineworld opened this issue Sep 23, 2024 · 6 comments

Comments

@shineworld
Copy link

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 ?

@exilon
Copy link
Owner

exilon commented Sep 25, 2024

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.

@shineworld
Copy link
Author

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.

@exilon
Copy link
Owner

exilon commented Sep 30, 2024

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

@shineworld
Copy link
Author

shineworld commented Oct 1, 2024

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.

@exilon
Copy link
Owner

exilon commented Oct 7, 2024

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.

@shineworld
Copy link
Author

shineworld commented Oct 8, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants