delphimvcframework/samples/articles_crud_server/CustomLoggerConfigU.pas

65 lines
1.9 KiB
ObjectPascal
Raw Normal View History

2024-04-23 23:19:45 +02:00
unit CustomLoggerConfigU;
interface
uses
LoggerPro; // loggerpro core
function GetLogger: ILogWriter;
implementation
uses
System.IOUtils
, LoggerPro.FileAppender // loggerpro file appender (logs to file)
, LoggerPro.SimpleConsoleAppender // loggerpro simple console appender (logs to console with colors)
, LoggerPro.DBAppender.FireDAC // loggerpro DB Appender (logs to database)
{$IFDEF MSWINDOWS}
, LoggerPro.OutputdebugStringAppender// loggerpro outputdebugstring appender (logs to the debugger)
{$ENDIF}
2024-05-04 16:36:31 +02:00
, Commons, System.SysUtils;
2024-04-23 23:19:45 +02:00
const
FailedDBWriteTag = 'FailedDBWrite';
var _FallbackLog: ILogWriter;
function GetFallBackLogger: ILogWriter;
begin
if _FallbackLog = nil then
begin
_FallbackLog := BuildLogWriter([
TLoggerProSimpleFileAppender.Create(10, 2048, 'logs', TLoggerProFileAppender.DEFAULT_FILENAME_FORMAT)
]);
end;
Result := _FallbackLog;
end;
function GetLogger: ILogWriter;
begin
Result := BuildLogWriter([
TLoggerProFileAppender.Create(10, 1000, TPath.Combine('MyFolder', 'MyLogs')),
TLoggerProDBAppenderFireDAC.Create(
CON_DEF_NAME,
'sp_loggerpro_writer',
// error handler, just write to disk on the server for later analysis
procedure(const Sender: TObject; const LogItem: TLogItem; const DBError: Exception; var RetryCount: Integer)
var
lIntf: ILogItemRenderer;
begin
lIntf := TLogItemRenderer.GetDefaultLogItemRenderer();
GetFallBackLogger.Error('DBAppender Is Failing (%d): %s %s', [RetryCount, DBError.ClassName, DBError.Message], FailedDBWriteTag);
GetFallBackLogger.Error(lIntf.RenderLogItem(LogItem), FailedDBWriteTag);
end),
TLoggerProSimpleConsoleAppender.Create
{$IFDEF MSWINDOWS}, TLoggerProOutputDebugStringAppender.Create{$ENDIF}
], nil, [
TLogType.Debug,
TLogType.Warning, {writes on DB only for WARNING+}
TLogType.Debug
{$IFDEF MSWINDOWS}, TLogType.Debug{$ENDIF}
]);
end;
end.