Skip to content

Latest commit

 

History

History
1035 lines (753 loc) · 34.3 KB

README.en.md

File metadata and controls

1035 lines (753 loc) · 34.3 KB

datalogger


| Português Brasileiro | English |

DataLogger

DataLogger was designed to be a simple logging library with support for many providers.

Support: developer.dlio@gmail.com

⚙️ Installation

To install in your project using boss:

$ boss install github.com/dliocode/datalogger

Manual Installation

Add the following folders to your project, under Project > Options > Delphi Compiler > Search path

../src/Core
../src/Providers

📌 Index

How to use

There are two different ways to use DataLogger: directly through Logger or instantiating your own TDataLogger.

Samples

The first is just intended to be a convenient shared log to use across your application if you choose.

  • Uses required:
uses DataLogger;

Providers

One provider it essentially serves to store your logs. Each instance of a TDataLogger can have several providers configured.

Here we have a list with 60 providers available:

Name Uses Examples
AWS CloudWatch DataLogger.Provider.AWS.CloudWatch AWS CloudWatch
Axiom DataLogger.Provider.Axiom Axiom
CallMeBot DataLogger.Provider.CallMeBot.WhatsApp CallMeBot
Console DataLogger.Provider.Console Console
Console Simple
Coralogix DataLogger.Provider.Coralogix Coralogix
CSV DataLogger.Provider.CSV CSV
Datadog DataLogger.Provider.Datadog Datadog
Datalust DataLogger.Provider.Datalust Datalust
Discord DataLogger.Provider.Discord.WebHook DiscordHook
Dynatrace Data Logger.Provider.Dynatrace Dynatrace
ElasticSearch DataLogger.Provider.ElasticSearch ElasticSearch
Teach him DataLogger.Provider.Elmah Teach him
Email DataLogger.Provider.Email Email
event log DataLogger.Provider.EventLog EventLog
Events DataLogger.Provider.Events Events
Events - With DataSet
Events - With SQLite
Firebase Data Logger.Provider.Firebase.Realtime Database Firebase
Grafana DataLogger.Provider.Grafana.Loki
datalogger.provider.graphana.oncall.webhook
Grafana
GraphJSON DataLogger.Provider.GraphJSON GraphJSON
Graylog DataLogger.Provider.Graylog Graylog
HTML DataLogger.Provider.HTML HTML
ListBox DataLogger.Provider.ListBox ListBox
ListView DataLogger.Provider.ListView ListView
Logentries DataLogger.Provider.Logentries Logentries
Logflare DataLogger.Provider.Logflare Logflare
Loggly DataLogger.Provider.Loggly Loggly
Logstash DataLogger.Provider.Logstash Logstash
Logtail DataLogger.Provider.Logtail Logtail
Logz DataLogger.Provider.Logz Logz
Mailgun DataLogger.Provider.Mailgun.Api Mailgun
Mailjet DataLogger.Provider.Mailjet.Api Mailjet
Mattermost DataLogger.Provider.Mattermost
DataLogger.Provider.Mattermost.WebHooks
Mattermost
MattermostHook
Memo DataLogger.Provider.Memo Memo
Memo and Text File
Memo - Only Error/VCL
Memory DataLogger.Provider.Memory Memory
mezma DataLogger.Provider.Mezmo mezma
MongoDB DataLogger.Provider.MongoDB.Cloud MongoDB
Ntfy DataLogger.Provider.Ntfy Ntfy
NewRelic DataLogger.Provider.NewRelic NewRelic
Notification DataLogger.Provider.Notification Notification
New datalogger.provider.pain New
OutputDebugString DataLogger.Provider.OutputDebugString OutputDebugString
PaperTrail DataLogger.Provider.PaperTrail PaperTrail
Postmark DataLogger.Provider.Postmark.Api Postmark
RabbitMQ DataLogger.Provider.RabbitMQ RabbitMQ
Redis DataLogger.Provider.Redis Redis
Rest DataLogger.Provider.REST.HTTPClient
DataLogger.Provider.REST.Indy
DataLogger.Provider.REST.NetHTTPClient
Rest
RichEdit DataLogger.Provider.RichEdit RichEdit
Sematext DataLogger.Provider.Sematext.Logs Sematext-Logs
SendChamp DataLogger.Provider.SendChamp.SMS
DataLogger.Provider.SendChamp.WhatsApp
SendEmail
SendEmail DataLogger.Provider.SendEmail SendEmail
SendGrid DataLogger.Provider.SendGrid.WebApi SendGridWebApi
Slack DataLogger.Provider.Slack.WebApi
DataLogger.Provider.Slack.WebHook
Slack
Splunk DataLogger.Provider.Splunk Splunk
Socket DataLogger.Provider.Socket Socket
SumoLogic DataLogger.Provider.SumoLogic SumoLogic
SysLog DataLogger.Provider.SysLog
DataLogger.Provider.SysLog.Indy
SysLog
SysLog Indy
Telegram DataLogger.Provider.Telegram Telegram
TextFile DataLogger.Provider.TextFile TextFile
UltraMSG DataLogger.Provider.UltraMSG.WhatsApp UltraMSG
Twilio DataLogger.Provider.Twilio.SMS
DataLogger.Provider.Twilio.WhatsApp
Twilio
Z-API DataLogger.Provider.ZAPI.WhatsApp Z-API

Dependencies

These dependencies occur when using some providers

Provider Dependency
DataLogger.Provider.RabbitMQ RabbitMQ
DataLogger.Provider.Redis Redis
DataLogger.Provider.SendEmail SendEmail

Extra Information

Plataforma Android:

Permission is required ACCESS_WIFI_STATE: Used to capture the MAC Address of the device.

Examples

Standard Use

uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create);

  Logger
    .Trace('My Trace')
    .Debug('My Debug')
    .Info('My Info')
    .Warn('My Warn')
    .Error('My Error')
    .Success('My Success')
    .Fatal('My Fatal')
    .Custom('Custom Level', 'My Custom')
    ;

  // or

  Logger
    .T('My Trace')
    .D('My Debug')
    .I('My Info')
    .W('My Warn')
    .E('My Error')
    .S('My Success')
    .F('My Fatal')
    .C('Custom Level', 'My Custom')
    ;

  Readln;

end.

Create a new DataLogger instance

uses
  DataLogger,
  DataLogger.Provider.Console;

var
  LCustomLogger: TDataLogger;
begin
  LCustomLogger := TDataLogger.Builder;
  LCustomLogger.AddProvider(TProviderConsole.Create);

  LCustomLogger
    .Trace('My Trace')
    .Debug('My Debug')
    .Info('My Info')
    .Warn('My Warn')
    .Error('My Error')
    .Success('My Success')
    .Fatal('My Fatal')
    .Custom('Custom Level', 'My message with custom level');
  Readln;
end.

DataLogger Simple

uses
  DataLogger,
  DataLogger.Simple,
  DataLogger.Provider.Console;

begin
  // Defini o provider
  Logger.AddProvider(TProviderConsole.Create);

  //Defini no DataloggerSimple a instância do log a ser utilizado
  TDataLoggerSimple.SetDataLogger(Logger);

  // Só usar o modo simples;
  Trace('My message Trace');
  Debug('My message debug');
  Info('My message info');
  Success('My message success');
  Warn('My message warn');
  Error('My message error');
  Fatal('My message fatal');
  Custom('My Type', 'My message custom');

  Readln;
end.

Custom

O Custom is how to define a name for your own level.

uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create);
  Logger.SetTemplate('${timestamp} [${level}] ${message}');

  Logger.Custom('Custom Level', 'My message with custom level!');

  // Output: 2022-12-01 09:00:05.500 [Custom Level] My message with custom level!

  Readln;
end.

Specials

Functions that operate on some providers.

  // Pula uma linha
  Logger.SlineBreak;

  // Desfaz o último log registrado
  Logger.UndoLast;

SlineBreak

The function SlineBreak using to make the line break.

uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create);

  // Definindo o template
  Logger.SetTemplate('${timestamp} [${level}] ${message}');

  // Gerando os logs
  Logger.Info('My message with level INFO');
  Logger.SlineBreak;
  Logger.Error('My message with level ERROR');

  // Output: 2022-12-01 09:00:05.500 [INFO] My message with level INFO
  // Output:
  // Output: 2022-12-01 09:00:05.600 [ERROR] My message with level ERROR

  Readln;
end.

UndoLast

The function UndoLast using to undo the last record made.

  • Some Providers do not have some feature that allows you to undo the last record sent.
uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create);

  // Definindo o template
  Logger.SetTemplate('${timestamp} [${level}] ${message}');

  // Gerando os logs
  Logger.Info('My message with level INFO');

  // Output: 2022-12-01 09:00:05.500 [INFO] My message with level INFO

  Sleep(4000);
  Logger.UndoLast;
  Logger.Error('My message with level ERROR');

  // Output: 2022-12-01 09:00:05.600 [ERROR] My message with level ERROR

  Readln;
end.

Template (Log Format)

The template is the format in which the log will be generated.

default template:

${timestamp} [TID ${thread_id}] [PID ${process_id}] [SEQ ${sequence}] [${level}] [${tag}] ${message}

Constants

There are some constants that can be used to facilitate the creation of the template.

  • Common
// Exibe o id que foi gerado o log, no formato GUID
TLoggerTemplate.LOG_ID = '${id}';

// Exibe o nome do log. ex: Logger.SetName('SERVICE_REMOTE')
TLoggerTemplate.LOG_NAME = '${name}';

// Exibe a sequencia que o log foi gerado.
TLoggerTemplate.LOG_SEQUENCE = '${sequence}';

// Exibe a data e hora que foi gerado, usado o SetFormatTimestamp
TLoggerTemplate.LOG_TIMESTAMP = '${timestamp}';

// Exibe a data e hora que foi gerado, no formato ISO8601.
TLoggerTemplate.LOG_TIMESTAMP_ISO8601 = '${timestamp_iso8601}';

// Exibe a data e hora que foi gerado, no formato UNIX.
TLoggerTemplate.LOG_TIMESTAMP_UNIX = '${timestamp_unix}';

// Exibe o Id da thread que foi gerado o log.
TLoggerTemplate.LOG_THREADID = '${thread_id}';

// Exibe o id do processo do app.
TLoggerTemplate.LOG_PROCESSID = '${process_id}';

// Exibe o level do log, sendo eles: TRACE / DEBUG / INFO / SUCCESS / WARN / ERROR / FATAL
TLoggerTemplate.LOG_LEVEL = '${level}';

// Exibe o level do log no formato numérico, sendo eles: 1=TRACE / 2=DEBUG / 3=INFO / 4=SUCCESS / 5=WARN / 6=ERROR / 7=FATAL / 8=CUSTOM
TLoggerTemplate.LOG_LEVEL_VALUE = '${level_value}';

// Exibe a tag do log, essa informação é preenchida a após a mensagem; Ex: Logger.Debug('Minha mensagem','Minha Tag');
TLoggerTemplate.LOG_TAG = '${tag}';

// Exibe a mensagem do log, sem essa tag a mensagem não é exibida. Ex: Logger.Debug('Minha mensagem');
TLoggerTemplate.LOG_MESSAGE = '${message}';
  • Specials:
// Exibe o nome do app.
TLoggerTemplate.LOG_APPNAME = '${app_name}';

// Exibe o diretório do app.
TLoggerTemplate.LOG_APPPATH = '${app_path}';

// Exibe a versão do app.
TLoggerTemplate.LOG_APPVERSION = '${app_version}';

// Exibe o tamanho do app em MB.
TLoggerTemplate.LOG_APPSIZE = '${app_size}';

// Exibe o nome do computador.
TLoggerTemplate.LOG_COMPUTERNAME = '${computer_name}';

// Exibe o nome do usuário do Sistema Operacional.
TLoggerTemplate.LOG_USERNAME = '${username}';

// Exibe as informações do Sistema Operacional.
TLoggerTemplate.LOG_OSVERSION = '${os_version}';

// Exibe o IP Local.
TLoggerTemplate.LOG_IP_LOCAL = '${ip_local}';

// Exibe o MAC Address.
TLoggerTemplate.LOG_MAC_ADDRESS = '${mac_address}';

SetTemplate

Defines the format in which the log will be recorded

uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create);

  // Definindo o template
  Logger.SetTemplate('${timestamp} [${level}] ${message}');

  // Definindo o template com constante
  Logger.SetTemplate(Format('%s [%s] %s', [TLoggerTemplate.LOG_TIMESTAMP, TLoggerTemplate.LOG_LEVEL, LoggerTemplate.LOG_MESSAGE]));

  // Gerando os logs
  Logger.Info('My message with level INFO');
  Logger.Error('My message with level ERROR');

  // Output: 2022-12-01 09:00:05.500 [INFO] My message with level INFO
  // Output: 2022-12-01 09:00:05.600 [ERROR] My message with level ERROR

  Readln;
end.

How to define a template in providers specific

  • It is possible to define several specific settings in each provider separate.
uses
  DataLogger,
  DataLogger.Provider.Console,
  DataLogger.Provider.TextFile;

begin
  // Formato do console '${timestamp} [${level}] ${message}'
  Logger.AddProvider(
    TProviderConsole.Create
    .SetTemplate('${timestamp} [${level}] ${message}')
  );

  // Formato do text file '${timestamp} - ${message}'
  Logger.AddProvider(
    TProviderTextFile.Create
    .SetTemplate('${timestamp} - ${message}')
  );

  // Gerando os logs
  Logger.Info('My message with level INFO');
  Logger.Error('My message with level ERROR');

  // Output Console:
  // 2022-12-01 09:00:05.500 [INFO] My message with level INFO
  // 2022-12-01 09:00:05.600 [ERROR] My message with level ERROR

  // Output TextFile:
  // 2022-12-01 09:00:05.500 - My message with level INFO
  // 2022-12-01 09:00:05.600 - My message with level ERROR

  Readln;
end.

SetFormatTimestamp

Changes the format of the TimeStamp.

  • Default TimeStamp format: yyyy-mm-dd hh:mm:ss.zzz
uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create);

  // Definindo o formato do Timestamp
  Logger.SetFormatTimestamp('dd/mm/yyyy hh:mm:ss')

  // Definindo o template
  Logger.SetTemplate('${timestamp} [${level}] ${message}');

  // Definindo o template com constante
  Logger.SetTemplate(Format('%s [%s] %s', [TLoggerTemplate.LOG_TIMESTAMP, TLoggerTemplate.LOG_LEVEL, LoggerTemplate.LOG_MESSAGE]));

  // Gerando os logs
  Logger.Info('My message with level INFO');
  Logger.Error('My message with level ERROR');

  // Output: 01/12/2022 09:00:05 [INFO] My message with level INFO
  // Output: 01/12/2022 09:00:05 [ERROR] My message with level ERROR

  Readln;
end.

Level

The DataLogger has these levels to generate the logs:

  Logger.Trace('');
  Logger.Debug('');
  Logger.Info('');
  Logger.Success('');
  Logger.Warn('');
  Logger.Error('');
  Logger.Fatal('');
  Logger.Custom('');

  // Modo simplificado
  Logger.T(''); // TRACE
  Logger.D(''); // DEBUG
  Logger.I(''); // INFO
  Logger.S(''); // SUCCESS
  Logger.W(''); // WARN
  Logger.E(''); // ERROR
  Logger.F(''); // FATAL
  Logger.C(''); // CUSTOM

SetLevelName

It is possible to change the level description to another description.

uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create);

  // Definindo o template
  Logger.SetTemplate('${timestamp} [${level}] ${message}');

  // Definindo o Level
  Logger.SetLevelName(TLoggerLevel.Info, 'INFORMATION');
  Logger.SetLevelName(TLoggerLevel.Warn, '  WARNING  ');

  // Gerando os logs
  Logger.Info('My message with level INFO');
  Logger.Warn('My message with level WARN');

  // Output: 2022-12-01 09:00:05.600 [INFORMATION] My message with level INFO
  // Output: 2022-12-01 09:00:05.600 [  WARNING  ] My message with level WARN

  Readln;
end.

SetLevel

It is possible to show only the logs from a level defined, based on TLoggerLevel.

SetLevel default value = TLoggerLevel.All

TLoggerLevel

  • When defining a level, only the chosen option and its superior types will be displayed.
  • Ex: Logger.SetLevel(TLoggerLevel.Warn); - Will be registered only the logs with the type Warn / Error / Fatal / Custom.
  TLoggerLevel.All = 'Utilizado para operações internas'
  TLoggerLevel.Trace = 'Level 1'
  TLoggerLevel.Debug = 'Level 2'
  TLoggerLevel.Info = 'Level 3'
  TLoggerLevel.Success = 'Level 4'
  TLoggerLevel.Warn = 'Level 5'
  TLoggerLevel.Error = 'Level 6'
  TLoggerLevel.Fatal = 'Level 7'
  TLoggerLevel.Custom = 'Level 8'
uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create);

  // Definindo o template
  Logger.SetTemplate('${timestamp} [${level}] ${message}');

  // Definindo o Level
  Logger.SetLevel(TLoggerLevel.Warn);

  // Gerando os logs
  Logger.Info('My message with level INFO');
  Logger.Error('My message with level ERROR');

  // Output: 2022-12-01 09:00:05.600 [ERROR] My message with level ERROR

  Readln;
end.

SetDisableLevel

It is possible to disable some levels do log, com base no TLoggerLevel.

Set Disable Level default value = []

  • When disabled, only options that are not disabled will be displayed.
  • Ex: Logger.SetDisableLevel([TLoggerLevel.Info, TLoggerLevel.Warn]); - Will be registered only the logs with the type Tracer / Debug / Success / Error / Fatal / Custom.
uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create);

  // Definindo o template
  Logger.SetTemplate('${timestamp} [${level}] ${message}');

  // Definindo o DisableLevel
  Logger.SetDisableLevel([TLoggerLevel.Info, TLoggerLevel.Warn]);

  // Gerando os logs
  Logger.Debug('My message with level DEBUG');
  Logger.Info('My message with level INFO');
  Logger.Warn('My message with level WARN');
  Logger.Error('My message with level ERROR');

  // Output:
  // 2022-12-01 09:00:05.500 [DEBUG] My message with level DEBUG
  // 2022-12-01 09:00:05.600 [ERROR] My message with level ERROR

  Readln;
end.

SetOnlyLevel

It is possible to show only some levels do log, com base no TLoggerLevel.

SetOnlyLevel default value = [TLoggerLevel.All]

  • When defined, only registered options will be displayed.
  • Ex: Logger.SetOnlyLevel([TLoggerLevel.Error]); - Will be registered only the logs with the type Error.
uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create);

  // Definindo o template
  Logger.SetTemplate('${timestamp} [${level}] ${message}');

  // Definindo o OnlyLevel
  Logger.SetOnlyLevel([TLoggerLevel.Error]);

  // Gerando os logs
  Logger.Debug('My message with level DEBUG');
  Logger.Info('My message with level INFO');
  Logger.Warn('My message with level WARN');
  Logger.Error('My message with level ERROR');

  // Output:
  // 2022-12-01 09:00:05.600 [ERROR] My message with level ERROR

  Readln;
end.

SetLogException

It is possible to catch exceptions generated by providers

SetLogException default value = nil

uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create);

  // Definindo o template
  Logger.SetTemplate('${timestamp} [${level}] ${message}');

  // Definindo o LogException
  Logger.SetLogException(
    procedure(const Sender: TObject; const LogItem: TLoggerItem; const E: Exception; var RetriesCount: Integer)
    begin
      // Sender - Provider que gerou a exceção, para visualizar - Sender.ClassName

      // LogItem - Contém todas as informações do log

      // E - Contém as informações da exceção

      // RetriesCount - Contém o número da tentativa realizada
      // Caso seja alterado o valor para -1, o processo é interrompido
    end
  );

  // Gerando o log
  Logger.Error('My message with level ERROR');

  // Output:
  // 2022-12-01 09:00:05.600 [ERROR] My message with level ERROR

  Readln;
end.

SetMaxRetries

It is possible to set the number of attempts that the Provider should try when saving the log.

Set Max Retries default value = 5

uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create);

  // Definindo o template
  Logger.SetTemplate('${timestamp} [${level}] ${message}');

  // Definindo o máximo de tentativas
  Logger.SetMaxRetries(5);

  // Gerando o log
  Logger.Error('My message with level ERROR');

  // Output:
  // 2022-12-01 09:00:05.600 [ERROR] My message with level ERROR

  Readln;
end.

SetIgnoreTemplate

It is possible to ignore the Template and save all data generated by DataLogger;

  SetIgnoreTemplate({1}, {2}, {3}, {4});

  Parâmetros:
    {1} = (Boolean) = Defini se deve ignorar o Template.
    {2} = (string) = Defini qual texto vai fazer a separação das informações, semelhante ao CSV.
    {3} = (Boolean) = Defini se deve mostrar as palavras chaves de cada valor.
    {4} = (string) = Defini qual texto deve separar a palavra chave do valor.

  Logger.SetIgnoreTemplate(True, '|', True, ' -> ');

  {palavra_chave}           = "timestamp"
  {palavra_chave_separador} = " -> "
  {valor}                   = "2022-09-15T14:39:38.896-03:00"
  {separator}               = " | "

  // output timestamp -> 2022-09-15T14:39:38.896-03:00 | timestamp_format -> 2022-09-15 14:39:38.896
uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create);

  // Definindo o template
  Logger.SetTemplate('${timestamp} [${level}] ${message}');

  // Ignorando o log format
  Logger.SetIgnoreTemplate(True, '|', True, ' -> ');

  // Gerando o log
  Logger.Error('My message with level ERROR');

  // Output:
  // timestamp -> 2022-09-15T14:39:38.896-03:00 | timestamp_format -> 2022-09-15 14:39:38.896 | name -> | sequence -> 1 | thread_id -> 3804 | level -> Trace | level_value -> 1 | tag -> | message -> My Trace | app_name -> ProviderTextFile | app_version -> 1.0.0.0 | app_path -> C:\Github\DataLogger\Samples\TextFile\Win32\Debug | app_size -> 13,24 MB | computer_name -> DESKTOP-7RP1H3K | username -> danil | os_version -> Windows 10 (Version 21H2, OS Build 19044.1889, 64-bit Edition) | process_id -> 13608 | ip_local -> 192.168.56.1

  Readln;
end.

SetName

It is possible to define a name for the DataLogger which can be displayed in the log record. This name can be used to differentiate the DataLogger when more than one instance.

SetName default value = EmptyStr

uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create);

  // Definindo o template
  Logger.SetTemplate('${name} ${timestamp} [${level}] ${message}');

  // Definindo o name
  Logger.SetName('MyLogger');

  // Gerando o log
  Logger.Error('My message with level ERROR');

  // Output:
  // MyLogger 2022-12-01 09:00:05.600 [ERROR] My message with level ERROR

  Readln;
end.

SetLiveMode

It is possible to save the logs in real time, so the next statement of your code will only continue after the log is saved!

Currently, the logs are recorded in memory and then saved without crashing the application.

SetLiveMode default value = false

uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create);

  // Definindo o template
  Logger.SetTemplate('${name} ${timestamp} [${level}] ${message}');

  // Definindo o LiveMode
  Logger.SetLiveMode(True);

  // Gerando o log
  Logger.Error('My message with level ERROR');

  // Output:
  // MyLogger 2022-12-01 09:00:05.600 [ERROR] My message with level ERROR

  Readln;
end.

Transaction

Is it possible to work with Transaction, just as it is used in other database-connected components.

The use of this procedure can be applied to the following situation;

Vamos contar uma pequena história:

we have a procedure that is doing an execution, in each step several log information is generated, by habit we always save this information, this makes our text file, for example, too big.
Now imagine being able to save the data only if there was an error during execution or when it was really necessary to save the data.

how to enable

Enabling the use of Transaction deve ser feita por Provider with the function UseTransaction(True).

Ex:

Logger.AddProvider(
  TProviderConsole.Create
  .UseTransaction(True) // Enable transaction only on this provider
);

StartTransaction

Starts a new transaction.

CommitTransaction

Commit the record of all logs in the transaction.

RollbackTransaction

Cancels all logs generated in the transaction.

Example with Simple Transaction

Sample com Transaction

uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create.UseTransaction(True));

  // Definindo o template
  Logger.SetTemplate('${timestamp} [${level}] ${message}');

  // Iniciando a transaction
  Logger.StartTransaction;

  Writeln('Iniciou a transaction');

  // Gerando o log
  Logger.Info('My message with level INFO 1 ');
  Logger.Info('My message with level INFO 2');
  Logger.Info('My message with level INFO 3');
  Logger.Info('My message with level INFO 4');
  Logger.Info('My message with level INFO 5');
  Logger.Info('My message with level INFO 6');
  Logger.Info('My message with level INFO 7');
  Logger.Info('My message with level INFO 8');
  Logger.Info('My message with level INFO 9');

  Writeln('Terminou os Logs');

  // Fazendo o commit
  Logger.CommitTransaction;
  Writeln('Fez os commits');

  Readln;
end.

example with TRANSTO nested

Sample with Nested Transaction

As transactions nested data gives the possibility of greater management when saving information.

  • Only one observation must be remembered, the final record will only be saved if there was commit the parent transaction, that is, the transaction that started the whole process;
    Caso a transação pai, tenha feito rollback, as transações filhas que foram feitas commit will be ignored!
uses
  DataLogger,
  DataLogger.Provider.Console;

begin
  Logger.AddProvider(TProviderConsole.Create.UseTransaction(True));

  // Definindo o template
  Logger.SetTemplate('${timestamp} [${level}] ${message}');

  // Iniciando a transaction
  Logger.StartTransaction;
  try
    // Gerando o log
    Logger.Info('My message with level INFO 1 ');
    Logger.Info('My message with level INFO 2');
    Logger.Info('My message with level INFO 3');
    Logger.Info('My message with level INFO 4');

    // Iniciando 2 transaction
    Logger.StartTransaction;
    try
      Logger.Info('My message with level INFO 5');
      Logger.Info('My message with level INFO 6');
    finally
      // fazendo Rollback da segunda transaction
      Logger.RollbackTransaction;
    end;

    Logger.Info('My message with level INFO 7');
    Logger.Info('My message with level INFO 8');
    Logger.Info('My message with level INFO 9');
  finally
    // Fazendo o commit
    Logger.CommitTransaction;
  end;

  Readln;
end.