YOUSEFUL / DELPHI / COMPONENTS
TAtlasConsoleSink
Writes events to standard output, and to standard error from ErrorLevel upward.
Description
For console programs and services run from a console. Each event is one line in the sink's Layout and Pattern, written with Writeln to Output, or to ErrOutput when its level is at least ErrorLevel. In a program without a console (IsConsole is False) the sink writes nothing.
Members
| Member | Kind | What it does |
|---|---|---|
Create | constructor | Creates the sink with ErrorLevel = llError, Layout = loText and the default pattern. |
ErrorLevel | property | Events at this level and above go to standard error, the rest to standard output. |
Destroy | destructor from TAtlasTextSink | Leaves the logger first, so no event arrives during destruction, then frees the sink. |
Layout | property from TAtlasTextSink | loText (default) writes each event with Pattern; loJson writes one JSON object per event (see TAtlasJsonFormatter). |
Pattern | property from TAtlasTextSink | The text pattern for loText, ignored for loJson. |
Logger | property from TAtlasLogSink | The logger this sink receives events from. |
MinLevel | property from TAtlasLogSink | The sink's own threshold: events below it are ignored by this sink only. |
Write | method from TAtlasLogSink | Writes one event. |
Flush | method from TAtlasLogSink | Pushes buffered output out, under the sink's lock. |
Constructors
Create constructor
constructor Create(AOwner: TComponent); override;Creates the sink with ErrorLevel = llError, Layout = loText and the default pattern.
Destructors
Destroy destructorinherited from TAtlasTextSink
destructor Destroy; override;Leaves the logger first, so no event arrives during destruction, then frees the sink. Frees the formatters.
Properties
ErrorLevel property
property ErrorLevel: TAtlasLogLevel read FErrorLevel write FErrorLevel default llError;Events at this level and above go to standard error, the rest to standard output. llOff sends everything to standard output.
Console.ErrorLevel := llWarning;Layout propertyinherited from TAtlasTextSink
property Layout: TAtlasLogLayout read FLayout write FLayout default loText;loText (default) writes each event with Pattern; loJson writes one JSON object per event (see TAtlasJsonFormatter).
Sink.Layout := loJson;Pattern propertyinherited from TAtlasTextSink
property Pattern: string read GetPattern write SetPattern stored IsPatternStored;The text pattern for loText, ignored for loJson. Default {time:yyyy-mm-dd hh:nn:ss.zzz} [{level:short}] {category} {scope} {message}{exception}{props}; assigning an empty string restores it. Tokens are listed under TAtlasTextFormatter. An unknown token, an unterminated { or a single } raises EAtlasLogError and keeps the previous pattern. Line breaks in values are always escaped as \n in sinks. Set it before the sink starts receiving events.
Sink.Pattern := '{time:hh:nn:ss} {level:short} {category} {message}{exception}';Logger propertyinherited from TAtlasLogSink
property Logger: TAtlasLogger read FLogger write SetLogger;The logger this sink receives events from. Setting it registers the sink with that logger (and removes it from the previous one); nil detaches it. The same as calling TAtlasLogger.AddSink and RemoveSink. Either side may be destroyed first: the other is told and lets go.
Sink.Logger := Log;MinLevel propertyinherited from TAtlasLogSink
property MinLevel: TAtlasLogLevel read FMinLevel write FMinLevel default llTrace;The sink's own threshold: events below it are ignored by this sink only. It applies after the logger's threshold, so it can only narrow what the logger lets through. Default llTrace.
Files.MinLevel := llInfo; // the file skips debug and traceMethods
Write methodinherited from TAtlasLogSink
procedure Write(const Event: TAtlasLogEvent);Writes one event. The logger calls it; you may call it too, for example to replay events from a TAtlasMemorySink into another sink. Events below MinLevel and events at llOff are ignored; otherwise DoWrite runs under the sink's lock. Unlike the logger, Write itself may raise when the output fails.
Flush methodinherited from TAtlasLogSink
procedure Flush;Pushes buffered output out, under the sink's lock. TAtlasLogger.Flush calls it on every sink and counts failures; called directly it may raise.
Examples
A console with times
Short lines on the console; warnings and above on standard error.
uses Atlas.Log, Atlas.LogSinks;
procedure AddConsole(Log: TAtlasLogger);
var
Console: TAtlasConsoleSink;
begin
Console := TAtlasConsoleSink.Create(Log);
Console.Pattern := '{time:hh:nn:ss} {level:short} {category} {message}{exception}';
Console.ErrorLevel := llWarning;
Console.Logger := Log;
end;
Related
TAtlasTextSink · TAtlasDebugSink · TAtlasFileSink
Also in Atlas Log
TAtlasLogger · TAtlasMemorySink · TAtlasCallbackSink · TAtlasDebugSink · TAtlasFileSink · TAtlasAsyncSink · TAtlasLogView