Logging
TMS Business includes a logging system to help you log data. It's specially useful for server applications, but you can use it anywhere you want. Writing log messages is clean, simple and straightforward. When logging messages, you don't care what the output will be.
uses {...}, Bcl.Logging;
var
Logger: ILogger;
begin
Logger := LogManager.GetLogger;
Logger.Error('This is an error!!!');
end;
At the beginning of the application, you simply configure the output, which can use the simple built-in debugger, or use the full TMS Logging framework, which can output to several places like Text files, CSV file, TCP/IP listener, Browser, Windows Event Log, among others.
You can also use the Logging Middleware to automatically log HTTP requests and responses processed by Sparkle servers.
Related topics:
Writing Log Messages
Writing log messages (generating log information) with TMS Business is very straightforward. You just need to retrieve the ILogger interface (using LogManager.GetLogger method) and use one of its methods:
uses {...}, Bcl.Logging;
var
Logger: ILogger;
begin
Logger := LogManager.GetLogger('MyLogger');
Logger.Error('This is an error!!!');
Logger.Warning('Pay attention to this');
Logger.Info('Relevant information');
Logger.Debug('Some debug output');
Logger.Trace('Tracing information');
end;
Naming loggers
When retrieving loggers using GetLogger, you can give names to your logger. In the example above, the logger will be called MyLogger. You can simply call GetLogger without any name:
Logger := LogManager.GetLogger;
Which creates a logger with empty name. You can also pass a TClass parameter:
Logger := LogManager.GetLogger(TMyClass);
It will create a logger which name is the fully qualified name of the class (for example, MyUnit.TMyClass).
Naming loggers is useful to properly redirect log messages to different outputs. By using names you can redirect messages from specific loggers to one output, ignore messages from other loggers, etc.. More info in topic Log Output Handlers and TMS Logging.
Output
That's pretty much about it for logging data. You don't worry about where the data will be logged to, just log information from where you need. By default, messages won't be output to anywhere. You can quickly see your log messages by calling RegisterDebugLogger anywhere in your application before you start logging:
RegisterDebugLogger;
This will enable a basic logger that outputs data to the default debugger. You can see log messages in Output tab of Delphi Messages window:
Of course you can choose to output your log messages to a wide range of other options. That is accomplished by using the integration with TMS Logging framework, which provides several output handlers you can choose from.
As a final note, here is the full reference for the ILogger interface:
ILogger = interface
procedure Error(const AValue: TValue);
procedure Warning(const AValue: TValue);
procedure Info(const AValue: TValue);
procedure Trace(const AValue: TValue);
procedure Debug(const AValue: TValue);
end;
Note that the methods accept a TValue parameter, meaning you are not restricted to pass a string to it, but also any other value, like boolean, integer, double, or even complex ones like an object. The way such data will be output will depend on the output handler. For example, the basic Debug Logger just calls TValue.ToString method for getting representation of logged data, which doesn't provide much info for objects. But output handlers in TMS Logging handle objects better, by listing its properties. Thus you can be confident data of primitive types will be properly logging, but for complex data that depends on the output handler.
Log Output Handlers and TMS Logging
You can easily integrate TMS Logging to
write your log messages to
different targets. TMS Logging is a full-featured logging framework that
works independently from TMS Sparkle. To plug TMS Logging as the logging
framework, use unit Bcl.TMSLogging
and call RegisterTMSLogger event.
Using global default logger
uses {...}, Bcl.TMSLogging;
RegisterTMSLogger;
Once you do the above, any log message you write will be logged using the TMSDefaultLogger global logger of TMS Logging framework, regardless of the logger name. You simply need to configure the output handlers in TMS Logging. In the following example, we configure add a Windows event output handler to TMS Logging:
uses
Bcl.Logging,
Bcl.TMSLogging,
TMSLoggingCore,
TMSLoggingEventLogOutputHandler;
var
Logger: ILogger;
begin
RegisterTMSLogger;
// Output log to Windows Event Log
TMSDefaultLogger.RegisterOutputHandlerClass(
TTMSLoggerEventLogOutputHandler, ['TMS Logging App']);
Logger := LogManager.GetLogger;
Logger.Error('This is an error!!!');
Logger.Warning('Pay attention to this');
Logger.Info('Relevant information');
Logger.Debug('Some debug output');
Logger.Trace('Tracing information');
end;
If you run the sample code above, you will see the messages logged in the Windows Event Log. Just press Windows+R, type "eventvwr" and press Enter. Or alternatively, press search for "Event Viewer" desktop application in Windows search.
Once Event Viewer is open, choose "Windows Logs" and select "Application". You should see your log messages there:
Using multiple loggers
The mode above use the single global logger. In more complex scenarios when you use multiple loggers to write log messages, you can call RegisterTMSLogger passing a anonymous function.
uses {...}, TMSLogging, VCL.TMSLoggingMemoOutputHandler;
RegisterTMSLogger(procedure(const Name: string; Logger: TTMSCustomLogger)
begin
if Pos('Foo', Name) > 0 then
Logger.RegisterOutputHandlerClass(
TTMSLoggerMemoOutputHandler, [Memo1]);
if Pos('Bar', Name) > 0 then
Logger.RegisterOutputHandlerClass(
TTMSLoggerMemoOutputHandler, [Memo2]);
end
);
The code above will change logging behavior and create a different TTMSLogger instance for each different named logger you use when calling GetLogger. This is useful to route different output handlers to different loggers. In the example above, if the logger name contains "Foo", any log messages generated by it will be redirected to Memo1. If the name contains Bar, log messages will be redirected to Memo2. For all other loggers, messages will be ignored.
This is very useful for you to ignore and organize log messages from different loggers, for example, you can ignore log messages coming from TMS Business loggers.
TMS Logging allows you to output the log to Text file, CSV file, TCP/IP listener, Browser, Windows Event Log, among others. For full documentation, including how to configure and use all the other available output handlers, please refer to TMS Logging documentation.