Lazarus 日誌工具 MultiLog

MultiLog是一種同時以靈活性和低開銷爲目標的日誌系統。顧名思義,它可用於將日誌實例到多個目標,如文本文件、可視控件或其餘應用程序。添加新的日誌目標使用兩個方法就能夠實現了,其中一個方法是可選的。
MultiLog一般相似於CodeSite、Smart Inect、Overseer和EstLogger,但它沒有緊跟其中的任何一個,以不一樣的方式實現了許多功能,甚至具備一些獨特的功能。ide

image

目前(2019年5月)版本是v 0.6.0.0。spa

MultiLog的使用是極其簡單的,如前面說的,僅僅兩步就能夠實現日誌記錄:debug

1:構建通道日誌

2:輸出code

通道能夠是 IPCChannel,FileChannel,或可視控件,MultiLog預製了三個功能,直接使用便可,IPCChannel實現進程間輔助監視,利用例程View項目,便可實現跨進程Log監視orm

image

直接在中應用 MultiLog 單元,其定義了Logger全局實例。blog

image

若是要輸出到某文件好比日期型號日誌 「20190520.log」,進程

vLOGFileNameM  := FormatDateTime('yyyymmdd', Now);
vlogName       := vLOGFileNameM + '.log';
FFileChannel   := TFileChannel.Create(vlogName);
FFileLogHaneld := Logger.Channels.Add(FFileChannel);

這裏對FileChannel作了點點調整,加入了固定的目錄「Logs」。get

constructor TFileChannel.Create(const AFileName: string; ChannelOptions: TFileChannelOptions);
const
  logDir = 'Logs';
begin
  if not DirectoryExists(logDir) then
    ForceDirectories(logDir);
  FShowPrefix := fcoShowPrefix in ChannelOptions;
  FShowTime   := fcoShowTime in ChannelOptions;
  FShowHeader := fcoShowHeader in ChannelOptions;
  Active      := True;
  FFileName   := logDir + DirectorySeparator + AFileName;
end;

FileChannel.Create 還帶一個ChannelOptions 參數,是個集合 ,默認是帶[fcoShowHeader, fcoShowTime],若是要顯示如故障、提示、警告須要加上fcoShowPrefix。string

with Logger do
  begin
    Channels.Add(LogTreeView1.Channel);
    Channels.Add(TIPCChannel.Create());
    Channels.Add(TFileChannel.Create('debug.log',[fcoShowHeader, fcoShowPrefix, fcoShowTime]));
    DefaultClasses := [lcDebug];
  end;

這也是MultiLog帶了一個綜合例子,Logger對Send作了多種overload,那麼能夠對Pascal語言的全部類型直接輸出,包括TStringList,「狀態」計數,SendError(’’)或者Send([lcError],’’),記錄調用狀況。

image

procedure TForm1.TestLogClick(Sender: TObject);
var
  AList:TStringList;
begin
  with Logger do
  begin
    ActiveClasses:=lcAll;
    EnterMethod(Sender,'TestLogClick');
    AList:=TStringList.Create;
    with AList do
    begin
      Add('aaaaaaa');
      Add('bbbbbbb');
      Add('ccccccc');
    end;
    Send('A Text Message');
    Send('Another Text Message');
    Send('A StringList', AList);
    AList.Destroy;
    SendError('A Error Message');
    SubLogClick(butSubLog);
    DefaultClasses := [lcWarning];
    ActiveClasses:=[lcDebug,lcInfo];
    Send('This Text Should NOT be logged');
    Send([lcDebug],'This Text Should be logged');
    ActiveClasses:=[];
    Send([lcWarning],'But This Text Should NOT');
    //Exitmethod is called even if not active if there's a unpaired EnterMethod
    ExitMethod(Sender,'TestLogClick');
    ActiveClasses:=lcAll;
  end;
end;

procedure TForm1.SubLogClick(Sender: TObject);
 var
   OldClasses: set of TDebugClass;
 begin
   with Logger do
   begin
     OldClasses:=ActiveClasses;
     ActiveClasses:=lcAll;
     EnterMethod(Sender,'SubLogClick');
     SendIf('Only show if called by TestLogClick',CalledBy('TestLogClick'));
     Send('AText inside DoIt');
     SendWarning('AWarning');
     SendCallStack('CallStack example');
     Send('A String','sadjfgadsfbmsandfb');
     Send('AInteger',4957);
     Send('A Boolean',True);
     ExitMethod(Sender,'SubLogClick');
     ActiveClasses:=OldClasses;
   end;
 end;


=== Log Session Started at 2019/5/12 9:19:41 by MultiLogDemo ===
09:19:45.201 >>ENTER METHOD: TButton(butTestLog).TestLogClick
09:19:45.205   INFO: A Text Message
09:19:45.209   INFO: Another Text Message
09:19:45.212   STRINGS: A StringList
                 aaaaaaa
                 bbbbbbb
                 ccccccc
09:19:45.215   ERROR: A Error Message
09:19:45.218   >>ENTER METHOD: TButton(butSubLog).SubLogClick
09:19:45.221     CONDITIONAL: Only show if called by TestLogClick
09:19:45.224     INFO: AText inside DoIt
09:19:45.227     WARNING: AWarning
09:19:45.336     CALL STACK: CallStack example
                     $000000010002DF2D line 329 of unit1.pas
                     $000000010002CF0F line 144 of unit1.pas
                     $0000000100126EF8 line 2913 of include/control.inc
                     $00000001001448FA line 55 of include/buttoncontrol.inc
                     $000000010014502F line 169 of include/buttons.inc
                     $00000001001447C2 line 21 of include/buttoncontrol.inc
                     $000000010000E1B5
                     $0000000100119B3C line 5419 of include/wincontrol.inc
                     $00000001001A9277 line 112 of lclmessageglue.pas
                     $00000001000FBEE8 line 2515 of win32/win32callback.inc
                     $00000001000FC6BC line 2677 of win32/win32callback.inc
                     $00000001001AEAFF line 105 of win32/win32pagecontrol.inc
                     $00007FFE5F37CA66
                     $0000000000090B9C
09:19:45.342     VALUE: A String = sadjfgadsfbmsandfb
09:19:45.346     VALUE: AInteger = 4957
09:19:45.349     VALUE: A Boolean = True
09:19:45.352   <<EXIT METHOD: TButton(butSubLog).SubLogClick
09:19:45.354   INFO: This Text Should be logged
09:19:45.356 <<EXIT METHOD: TButton(butTestLog).TestLogClick

經過監視進程MultiLog View監視到的數據,示例Log的輸出。

image

是否是很方便!

相關文章
相關標籤/搜索