YOUSEFUL / DELPHI / COMPONENTS

TAtlasConsoleSink

Writes events to standard output, and to standard error from ErrorLevel upward.

Non-visual component Palette: Atlas Log Unit Atlas.LogSinks · Inherits TAtlasTextSink · Product Atlas Log

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

MemberKindWhat it does
CreateconstructorCreates the sink with ErrorLevel = llError, Layout = loText and the default pattern.
ErrorLevelpropertyEvents at this level and above go to standard error, the rest to standard output.
Destroydestructor from TAtlasTextSinkLeaves the logger first, so no event arrives during destruction, then frees the sink.
Layoutproperty from TAtlasTextSinkloText (default) writes each event with Pattern; loJson writes one JSON object per event (see TAtlasJsonFormatter).
Patternproperty from TAtlasTextSinkThe text pattern for loText, ignored for loJson.
Loggerproperty from TAtlasLogSinkThe logger this sink receives events from.
MinLevelproperty from TAtlasLogSinkThe sink's own threshold: events below it are ignored by this sink only.
Writemethod from TAtlasLogSinkWrites one event.
Flushmethod from TAtlasLogSinkPushes 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.

pascal
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).

pascal
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.

pascal
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.

pascal
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.

pascal
Files.MinLevel := llInfo;   // the file skips debug and trace

Methods

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.

pascal
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;

TAtlasTextSink · TAtlasDebugSink · TAtlasFileSink

Also in Atlas Log

TAtlasLogger · TAtlasMemorySink · TAtlasCallbackSink · TAtlasDebugSink · TAtlasFileSink · TAtlasAsyncSink · TAtlasLogView