-
Notifications
You must be signed in to change notification settings - Fork 6
Logging.pas
Each class type or object should instantiate its own copy of TLogDispatcher with a unique name. Log messages are sent using a TLogDispatcher.
An application can instantiate one or more copies of TLogListener (or it's descendant classes). TLogListener descendant are provided to log messages to a file(TLogFileListener), console (TLogCRTListener) or a string list (TLogStringsListener). You can also implement your own logging listener classes.
There can be multiple log listeners, each having different filter conditions.
| Class | Description |
|---|---|
| TLogDispatcher | Sends messages to the logging system |
| TLogListener | Receives messages from all of the log dispatchers and provides filtering capabilities |
| TLogFileListener | Logs messages to a text file |
| TLogCRTListener | Logs messages to the terminal using the writeln() method of the CRT unit |
| TLogStringsListener | Logs messages to a TStrings object or its descendants |
By default the listeners and dispatchers allow all message types to pass the filter. (i.e. The DEFAULT_LOG_MESSAGE_TYPES is misnamed. It used to be the default but this was changed to ALL_LOG_MESSAGE_TYPES.)
type TLogMessageType = (mtInfo, mtWarning, mtDebug, mtError);
type TLogMessageTypes = set of TLogMessageType;
const ALL_LOG_MESSAGE_TYPES = [mtInfo, mtWarning, mtDebug, mtError];
const DEFAULT_LOG_MESSAGE_TYPES = [mtInfo, mtWarning, mtError];
const MESSAGE_TYPE_STRINGS: array[TLogMessageType] of String = ('INFO','WARNING','DEBUG','ERROR');TLogMessageEvent = procedure (Dispatcher: TLogDispatcher; MessageType: TLogMessageType; Message: String) of object;
_LogMessageString() constructs a text string used by TLogFileListener and TLogCRTListener. Format string is Format('%-18s [%-8s] (%-12s) %s',[DateTimeToStr(Now),T,D,M]);
function LogMessageString(Dispatcher: TLogDispatcher; MessageType: TLogMessageType; Message: String): String;
TLogListener = class(TObject)`
procedure Message(Dispatcher: TLogDispatcher; MessageType: TLogMessageType; Message: String); virtual;
property Enabled: Boolean read FEnabled write FEnabled;
property TypeFilter: TLogMessageTypes read FTypeFilter write FTypeFilter;
property NameFilter: String read FNameFilter write FNameFilter;
property OnMessage: TLogMessageEvent read FOnMessage write FOnMessage;TLogFileListener = class(TLogListener)
constructor Create(Filename: String; Append: Boolean = True);Logs to the terminal. Uses colored syntax highlighting.
TLogCRTListener = class(TLogListener)
| MessageType | Text Color |
|---|---|
| mtInfo | LightCyan |
| mtDebug | LightGray |
| mtWarning | Yellow |
| mtError | Red |
You need to provide a TStrings descendant in the constructor. This is useful for logging to a TMemo.Lines property.
TLogStringsListener = class(TLogListener)
constructor Create(Strings: TStrings);
property Strings: TStrings read FStrings;The Name property identifies the class or object sending the log message. Its is up to the programmer to decide on meaningful names. This value is passed through to the TLogListener objects.
The Filter property is a set of log message types that this dispatcher will send. It is useful for turning off mtDebug messages for specific classes or objects.
The Enabled property can be set to false to turn off logging for specific classes or objects.
TLogDispatcher = class(TObject)
constructor Create(AName: String);
procedure Send(MessageType: TLogMessageType; Message: String); overload;
procedure Send(MessageType: TLogMessageType; Message: String; Args: array of const); overload;
property Enabled: Boolean read FEnabled write FEnabled;
property Name: String read FName write FName;
property Filter: TLogMessageTypes read FFilter write FFilter;A TObject class that provides a Log property, which is a TLogDispatcher instance. Provided as a convenience. The default Name used for log messages sent is the TObject.ClassName of the object being created.
TLogObject = class(TObject)
property Log: TLogDispatcher read FLog;