【发布时间】:2023-07-23 18:47:01
【问题描述】:
我有一个需要批量处理文件的程序。而不是在屏幕上的消息框中显示错误(这将暂停程序的执行),我需要在日志中显示那些用户可以在程序执行时看到的错误消息。
所以我不需要像这样Which logging library is better? 的程序执行日志
我现在使用的是从 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;
但我想让用户动态过滤上下文。例如,用户应该能够隐藏所有 Verbose 消息并仅保留警告和错误。如果用户改变了主意,就放回冗长的消息。
RichEdit 中的(现有)文本可以这样过滤吗?如果没有,我想获得一些关于如何重新实现它的指示。我正在考虑编写自己的格式来保留行/消息。例如:
无法打开文件,#msgErr,#Bold
我将有一个 TStringGrid 来仅显示有限数量的行(在屏幕上可见的行)。这样我就可以拥有数百万行,而无需在屏幕上实际渲染所有行。浪费时间的解析无关紧要,因为我只需要解析可见行。
要求:
- 支持 RichEdit 中的颜色(红色表示错误等)
- 轻量级
- 应该有两个类:一个可视类(基于 TStringGrid)和一个用于控制台程序的非可视类(登录到 RAM 并稍后保存日志或在控制台中将消息显示为简单文本)。
- 没有硬依赖(Delphi 版本、Indy、DB 引擎、第 3 方控件等)
- 小(TRichEdit 大大增加了 EXE 文件的大小)
- 一个 PAS 文件
另一种方法是不使用网格并自己绘制文本(例如在 TPanel 派生的组件中)。或者这种控制可能已经存在。
欢迎对我的想法提出任何建设性的批评。你有比使用网格更好的主意吗?
【问题讨论】:
-
与其重新发明*,不如再看看现有的解决方案。顺便说一句,当文本内容增长时,TRichEdit(作为 TMemo)变得非常缓慢。虚拟模式下的 TDrawGrid 要快得多。对于轻量级日志系统,请尝试我们的Open Source SynLog unit,恕我直言,它满足您的所有要求,并且具有更多功能,包括从 Delphi 5 及更高版本工作、堆栈跟踪异常、远程访问和nice high performance viewer
-
@ArnaudBouchez- 嗨 Arnaud。我查看了其他系统,但都是用于记录源代码/路径执行的复杂系统。所有这些都具有 DB/TCP 支持和其他东西。不是很轻。它们是面向程序员的,而不是面向用户的。
-
@ArnaudBouchez-我看到了你的编辑。我会仔细看看,但它似乎很复杂。至少我可以窃取一些想法:) 你的 SynLog 支持动态过滤吗?
-
恕我直言,任何类似日志的系统都是低级的。对于用户,我们通常谈论“审计跟踪”,而不是日志记录。在这种情况下,考虑使用具有高效第三方网格(具有搜索和排序能力)的数据库,正如 TMS、DevExpress 或任何其他建议的那样,值得一看。在我们的系统中,我们有低级日志和高级日志,存储在 SQLite3 或 MongoDB 数据库中,与 SOA 提供的服务相关联。在高级审计信息中使用 SQL 搜索是一大优势。
-
是的,它支持动态过滤,按组(不仅是按级别):您可以单独启用或禁用任何级别。
标签: delphi logging delphi-xe7