YOUSEFUL / DELPHI / COMPONENTS
TAtlasLogChannel
A logger bound to one category; cheap to copy and to keep in a field.
Description
Returned by TAtlasLogger.Channel. It has the same logging methods as the logger and adds its category to every event, which SetCategoryLevel thresholds and the {category} token use. It holds a reference to the logger and the category string, nothing else, so create channels freely and keep them in fields. A channel that was never assigned (a zeroed field) does nothing and reports IsEnabled as False. Do not use a channel after its logger is freed.
Members
| Member | Kind | What it does |
|---|---|---|
IsEnabled | method | True when an event at Level in the channel's category would be logged (the logger's IsEnabled with this category). |
Log | method | Logs Template at Level in the channel's category. |
Log | method | Logs at Level in the channel's category, filling the template from Args as TAtlasLogger.Log does. |
Trace | method | Logs Template at llTrace with the channel's category. |
Trace | method | Logs at llTrace with the channel's category, filling the {name} holes of Template from Args in order and recording each as a property. |
Debug | method | Logs Template at llDebug with the channel's category. |
Debug | method | Logs at llDebug with the channel's category, filling the {name} holes of Template from Args in order and recording each as a property. |
Info | method | Logs Template at llInfo with the channel's category. |
Info | method | Logs at llInfo with the channel's category, filling the {name} holes of Template from Args in order and recording each as a property. |
Warning | method | Logs Template at llWarning with the channel's category. |
Warning | method | Logs at llWarning with the channel's category, filling the {name} holes of Template from Args in order and recording each as a property. |
Error | method | Logs Template at llError with the channel's category. |
Error | method | Logs at llError with the channel's category, filling the {name} holes of Template from Args in order and recording each as a property. |
Fatal | method | Logs Template at llFatal with the channel's category. |
Fatal | method | Logs at llFatal with the channel's category, filling the {name} holes of Template from Args in order and recording each as a property. |
Failure | method | Logs an exception in the channel's category, as TAtlasLogger.Failure does: class and message go into separate event fields; holes in Template are not filled. |
Category | property | The channel's category, as given to TAtlasLogger.Channel. |
Properties
Category property
property Category: string read FCategory;The channel's category, as given to TAtlasLogger.Channel.
Methods
IsEnabled method
function IsEnabled(Level: TAtlasLogLevel): Boolean;True when an event at Level in the channel's category would be logged (the logger's IsEnabled with this category). False for a channel that was never assigned.
Log method
procedure Log(Level: TAtlasLogLevel; const Template: string); overload;Logs Template at Level in the channel's category.
Log method
procedure Log(Level: TAtlasLogLevel; const Template: string; const Args: array of const); overload;Logs at Level in the channel's category, filling the template from Args as TAtlasLogger.Log does.
Trace method
procedure Trace(const Template: string); overload;Logs Template at llTrace with the channel's category. Text without holes is used as it is.
Orders.Trace('Entering validation');Trace method
procedure Trace(const Template: string; const Args: array of const); overload;Logs at llTrace with the channel's category, filling the {name} holes of Template from Args in order and recording each as a property. See Log for the template rules.
Orders.Trace('Validating field {field}', ['email']);Debug method
procedure Debug(const Template: string); overload;Logs Template at llDebug with the channel's category. Text without holes is used as it is.
Orders.Debug('Cache rebuilt');Debug method
procedure Debug(const Template: string; const Args: array of const); overload;Logs at llDebug with the channel's category, filling the {name} holes of Template from Args in order and recording each as a property. See Log for the template rules.
Orders.Debug('Cache holds {n} entries', [128]);Info method
procedure Info(const Template: string); overload;Logs Template at llInfo with the channel's category. Text without holes is used as it is.
Orders.Info('Application started');Info method
procedure Info(const Template: string; const Args: array of const); overload;Logs at llInfo with the channel's category, filling the {name} holes of Template from Args in order and recording each as a property. See Log for the template rules.
Orders.Info('Order {id} placed by {customer}', [1042, 'Contoso']);Warning method
procedure Warning(const Template: string); overload;Logs Template at llWarning with the channel's category. Text without holes is used as it is.
Orders.Warning('Disk space is low');Warning method
procedure Warning(const Template: string; const Args: array of const); overload;Logs at llWarning with the channel's category, filling the {name} holes of Template from Args in order and recording each as a property. See Log for the template rules.
Orders.Warning('Stock for {sku} is low: {left} left', ['AX-7', 3]);Error method
procedure Error(const Template: string); overload;Logs Template at llError with the channel's category. Text without holes is used as it is.
Orders.Error('Could not save the settings');Error method
procedure Error(const Template: string; const Args: array of const); overload;Logs at llError with the channel's category, filling the {name} holes of Template from Args in order and recording each as a property. See Log for the template rules.
Orders.Error('Payment {id} was declined', [77]);Fatal method
procedure Fatal(const Template: string); overload;Logs Template at llFatal with the channel's category. Text without holes is used as it is.
Orders.Fatal('Shutting down');Fatal method
procedure Fatal(const Template: string; const Args: array of const); overload;Logs at llFatal with the channel's category, filling the {name} holes of Template from Args in order and recording each as a property. See Log for the template rules.
Orders.Fatal('Database {name} is unreachable; stopping', ['orders']);Failure method
procedure Failure(E: Exception; const Template: string = ''; Level: TAtlasLogLevel = llError);Logs an exception in the channel's category, as TAtlasLogger.Failure does: class and message go into separate event fields; holes in Template are not filled.
on E: Exception do Orders.Failure(E, 'Could not archive the order');Examples
A channel per service
A class keeps its channel in a field and guards an expensive debug message.
uses System.SysUtils, Atlas.Log;
type
TOrderService = class
private
FLog: TAtlasLogChannel;
public
constructor Create(Logger: TAtlasLogger);
procedure Place(Id: Integer; const Customer: string);
end;
constructor TOrderService.Create(Logger: TAtlasLogger);
begin
inherited Create;
FLog := Logger.Channel('orders');
end;
procedure TOrderService.Place(Id: Integer; const Customer: string);
begin
if FLog.IsEnabled(llDebug) then FLog.Debug('Validating order {id}', [Id]);
FLog.Info('Order {id} placed by {customer}', [Id, Customer]);
end;
Related
Also in Atlas Log
TAtlasLogger · TAtlasMemorySink · TAtlasCallbackSink · TAtlasConsoleSink · TAtlasDebugSink · TAtlasFileSink · TAtlasAsyncSink · TAtlasLogView