设计日志系统

WeG*_*ars -2 delphi logging delphi-xe7

我有一个需要批量处理文件的程序。而不是在屏幕上的消息框中显示错误(这将暂停程序的执行),我需要在用户可以在程序执行时看到的日志中显示这些错误消息。

所以我不需要这样的程序执行日志哪个日志库更好?

我现在正在使用从 TRichEdit 派生的东西。基本上,一个带有一些额外方法的丰富编辑,如 AddError(s)、AddWarn(s)、AddVerbose(s) 等。

  TRichLog = class(TMyRichEdit)
   private
   protected
     Indent: Integer;   { Indent new added lines x spaces }
   public
     constructor Create(AOwner: TComponent);   override;
     procedure AddBold     (CONST Mesaj: string);
     procedure AddMsg      (CONST Mesaj: string);
     procedure AddMsgLvl   (CONST Mesaj: string; MsgType: Integer);
     procedure AddColorMsg (CONST Mesaj: string; Culoare: TColor);
     procedure AddVerb     (CONST Mesaj: string);
     procedure AddHint     (CONST Mesaj: string);
     procedure AddInfo     (CONST Mesaj: string);
     procedure AddPath     (CONST Mesaj: string);
     procedure AddWarn     (CONST Mesaj: string);
     procedure AddError    (CONST Mesaj: string);
     procedure AddMsgInt   (CONST Mesaj: string; i: Integer);          { Adds a message text followed by an integer }
     procedure AddInteger  (CONST i: Integer);
     procedure AddFromFile (CONST FileName: string; MsgType: Integer);                             { Reads lines from the specified file and adds them to the log using the specified color. }
     procedure AddEmptyRow;
     procedure AddDateStamp;

     procedure Append (RamLog: TObject);       { RamLog will be typecased to TRamLog }
     procedure SaveAsRtf   (CONST FullPath: string);
     procedure AppendToFile(CONST FullPath: string);
     function  VerbosityAsString: string;
   published
     property InsertTime: Boolean      read FInsertTime write FInsertTime default FALSE;
     property InsertDate: Boolean      read FInsertDate write FInsertDate default FALSE;
     property AutoScroll: Boolean      read FAutoScroll write FAutoScroll default TRUE;            { Automatically scroll to show the last line }
     property Verbosity : Integer      read FVerbosity  write FVerbosity  default vHints;

     property OnLogError: TNotifyEvent read FLogError   write FLogError;                           { Can be used to inform the application to automatically switch to log when an error is listed }
     property OnLogWarn : TNotifyEvent read FLogWarn    write FLogWarn;
Run Code Online (Sandbox Code Playgroud)

但我想让用户动态过滤上下文。例如,用户应该能够隐藏所有详细消息并仅保留警告和错误。如果用户改变主意,则将冗长的消息放回原处。

可以通过这种方式过滤 RichEdit 中的(现有)文本吗?如果没有,我想得到一些关于如何重新实现它的指示。我正在考虑编写自己的格式来保留行/消息。例如:

无法打开文件,#msgErr,#Bold

I 将有一个 TStringGrid 来仅显示有限数量的行(在屏幕上可见的行)。这样我就可以拥有数百万行,而无需在屏幕上实际渲染所有行。浪费的解析时间无关紧要,因为我只需要解析可见的行。

要求:

  • 支持 RichEdit 中的颜色(红色表示错误等)
  • 轻的
  • 应该有两个类:一个可视的(基于 TStringGrid)和一个非可视的控制台程序(登录到 RAM 并稍后保存日志或在控制台中将消息显示为简单文本)。
  • 没有硬依赖(Delphi 版、Indy、DB 引擎、3rd 方控件等)
  • 小(TRichEdit 大大增加了 EXE 文件的大小)
  • 一个 PAS 文件

另一种方法是不使用 Grid 并自己绘制文本(例如在 TPanel 派生的组件中)。或者也许这种控制已经存在。

欢迎对我的想法提出任何建设性的批评。你有比使用网格更好的主意吗?

Arn*_*hez 5

恕我直言,我们可能会有所不同:

  • 低级日志记录,面向开发人员和支持人员;
  • 针对最终用户的高级日志记录。

根据您的评论,听起来您是否需要第二种,通常也称为“审计跟踪”,尤其是在条款或法规方面。

我们通常通过将事件存储在数据库中来实现高级“审计跟踪”。例如,本地高性能 SQLite3 数据库,或 MongoDB 集中式实例。

使用 RDBMS(或 NoSQL DB)有几个优点:

  • 它的结构既可以是固定的(例如通过定义类别),也可以是进化的(通过一些文本字段,或者甚至是其他表的一些外键);
  • 很容易与您现有的 UI 交互,例如使用强大的第三方网格,具有排序和过滤功能;
  • 您可以使用 SQL 在日志内容中进行搜索,然后甚至为最有用的情况定义一个专用的 UI;
  • 它易于维护(DELETE FROM ... 将删除较旧的未删除条目,并可能在数据库中保留错误)。

我们通常在生产中使用我们的开源 SOA 框架执行此操作:所有服务调用都可以直接编写在 SQlite3 或 MongoDB 实例中,无需编写任何代码!然后您甚至可以使用 JSON 查询在参数内进行搜索。而且您仍然可以使用集成的低级日志记录