2016-06-22 17:49:16 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2021-08-15 18:39:55 +02:00
|
|
|
// Copyright (c) 2010-2021 Daniele Teti and the DMVCFramework Team
|
2016-06-22 17:49:16 +02:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
2015-12-22 12:38:17 +01:00
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
unit MVCFramework.Logger;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2017-03-23 18:51:25 +01:00
|
|
|
System.Classes,
|
|
|
|
System.SysUtils,
|
|
|
|
MVCFramework.Commons,
|
2016-09-06 10:30:52 +02:00
|
|
|
LoggerPro,
|
2017-03-23 18:51:25 +01:00
|
|
|
LoggerPro.FileAppender;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
2016-09-06 10:30:52 +02:00
|
|
|
const
|
|
|
|
LOGGERPRO_TAG = 'dmvcframework';
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
type
|
2017-09-23 15:03:07 +02:00
|
|
|
TLogLevel = (levDebug = 0, levNormal = 1, levWarning = 2, levError = 3, levException = 4);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
|
|
function LogLevelAsString(ALogLevel: TLogLevel): string;
|
2016-11-27 23:17:20 +01:00
|
|
|
procedure Log(AMessage: string); overload;
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure Log(AObject: TObject); overload;
|
|
|
|
|
2016-11-27 23:17:20 +01:00
|
|
|
procedure LogD(AMessage: string); overload;
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure LogD(AMessage: TObject); overload;
|
|
|
|
|
|
|
|
procedure LogI(AMessage: string); overload;
|
|
|
|
procedure LogI(AObject: TObject); overload;
|
|
|
|
|
|
|
|
procedure LogW(AMessage: string); overload;
|
|
|
|
procedure LogW(AObject: TObject); overload;
|
|
|
|
|
2016-11-27 23:17:20 +01:00
|
|
|
procedure LogE(AMessage: string);
|
2013-10-30 00:48:23 +01:00
|
|
|
procedure Log(LogLevel: TLogLevel; const AMessage: string); overload;
|
2021-12-27 10:13:23 +01:00
|
|
|
|
|
|
|
procedure LogException(const E: Exception; const AMessage: String);
|
|
|
|
|
2016-09-27 14:22:17 +02:00
|
|
|
procedure LogEnterMethod(const AMethodName: string);
|
|
|
|
procedure LogExitMethod(const AMethodName: string);
|
2016-09-06 10:30:52 +02:00
|
|
|
|
|
|
|
// direct access to loggerpro logger
|
|
|
|
function Log: ILogWriter; overload;
|
|
|
|
|
|
|
|
procedure SetDefaultLogger(const aLogWriter: ILogWriter);
|
|
|
|
procedure InitializeDefaultLogger;
|
2016-09-27 14:33:51 +02:00
|
|
|
{ @abstract(Use only inside DLL because dll unloading is not a safe place to shutdown threads, so call this before unload DLL)
|
|
|
|
Use this also in ISAPI dll. Check the @code(loggerproisapisample.dll) sample
|
|
|
|
}
|
|
|
|
procedure ReleaseGlobalLogger;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
|
|
var
|
|
|
|
LogLevelLimit: TLogLevel = TLogLevel.levNormal;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
uses
|
2019-05-02 17:38:57 +02:00
|
|
|
System.IOUtils,
|
|
|
|
MVCFramework.Serializer.JsonDataObjects,
|
|
|
|
MVCFramework.DuckTyping;
|
2019-03-05 20:55:37 +01:00
|
|
|
|
2016-09-06 10:30:52 +02:00
|
|
|
var
|
|
|
|
_lock: TObject;
|
|
|
|
_DefaultLogger: ILogWriter;
|
2019-03-05 20:55:37 +01:00
|
|
|
_LevelsMap: array [TLogLevel.levDebug .. TLogLevel.levException] of LoggerPro.TLogType = (
|
2016-09-06 10:30:52 +02:00
|
|
|
(
|
|
|
|
TLogType.Debug
|
|
|
|
),
|
|
|
|
(
|
|
|
|
TLogType.Info
|
|
|
|
),
|
|
|
|
(
|
|
|
|
TLogType.Warning
|
|
|
|
),
|
2016-11-27 23:17:20 +01:00
|
|
|
(
|
|
|
|
TLogType.Error
|
|
|
|
),
|
2016-09-06 10:30:52 +02:00
|
|
|
(
|
|
|
|
TLogType.Error
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
function Log: ILogWriter;
|
|
|
|
begin
|
2016-11-27 23:17:20 +01:00
|
|
|
if _DefaultLogger = nil then
|
|
|
|
begin
|
|
|
|
SetDefaultLogger(nil);
|
|
|
|
end;
|
|
|
|
|
2016-09-06 10:30:52 +02:00
|
|
|
Result := _DefaultLogger;
|
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
|
|
function LogLevelAsString(ALogLevel: TLogLevel): string;
|
|
|
|
begin
|
|
|
|
case ALogLevel of
|
|
|
|
levNormal:
|
|
|
|
Result := ''; // normal is '' because is more readable
|
2017-09-23 15:03:07 +02:00
|
|
|
levWarning:
|
2013-10-30 00:48:23 +01:00
|
|
|
Result := 'WARNING';
|
|
|
|
levError:
|
|
|
|
Result := 'ERROR';
|
|
|
|
levException:
|
|
|
|
Result := 'EXCEPTION';
|
2016-09-06 10:30:52 +02:00
|
|
|
else
|
|
|
|
Result := 'UNKNOWN';
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
function ObjectToJSON(const AObject: TObject): String;
|
|
|
|
var
|
|
|
|
lSer: TMVCJsonDataObjectsSerializer;
|
|
|
|
begin
|
|
|
|
lSer := TMVCJsonDataObjectsSerializer.Create;
|
|
|
|
try
|
|
|
|
if TDuckTypedList.CanBeWrappedAsList(AObject) then
|
|
|
|
begin
|
|
|
|
Result := '[' + AObject.QualifiedClassName + '] ' + lSer.SerializeCollection(AObject);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Result := '[' + AObject.QualifiedClassName + '] ' + lSer.SerializeObject(AObject);
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lSer.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
procedure LogW(AMessage: string);
|
|
|
|
begin
|
2016-11-27 23:17:20 +01:00
|
|
|
Log.Warn(AMessage, LOGGERPRO_TAG);
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure LogE(AMessage: string);
|
|
|
|
begin
|
2016-11-27 23:17:20 +01:00
|
|
|
Log.Error(AMessage, LOGGERPRO_TAG);
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
2021-12-27 10:13:23 +01:00
|
|
|
procedure LogException(const E: Exception; const AMessage: String);
|
|
|
|
begin
|
|
|
|
LogE(E.ClassName + ': ' + AMessage);
|
|
|
|
end;
|
|
|
|
|
2017-09-23 15:03:07 +02:00
|
|
|
// procedure LogException(
|
|
|
|
// const AException: Exception;
|
|
|
|
// const AMessage: string);
|
|
|
|
// begin
|
|
|
|
// Log.Error(Format('[%s] %s (Custom message: "%s")', [AException.ClassName,
|
|
|
|
// AException.Message, AMessage]), LOGGERPRO_TAG);
|
|
|
|
// end;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
2016-09-27 14:22:17 +02:00
|
|
|
procedure LogEnterMethod(const AMethodName: string);
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
2016-11-27 23:17:20 +01:00
|
|
|
LogI('>> ' + AMethodName);
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
2016-09-27 14:22:17 +02:00
|
|
|
procedure LogExitMethod(const AMethodName: string);
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
2016-11-27 23:17:20 +01:00
|
|
|
LogI('<< ' + AMethodName);
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure Log(LogLevel: TLogLevel; const AMessage: string);
|
|
|
|
begin
|
2016-09-06 10:30:52 +02:00
|
|
|
case _LevelsMap[LogLevel] of
|
|
|
|
TLogType.Debug:
|
2016-11-27 23:17:20 +01:00
|
|
|
Log.Debug(AMessage, LOGGERPRO_TAG);
|
2016-09-06 10:30:52 +02:00
|
|
|
TLogType.Info:
|
2016-11-27 23:17:20 +01:00
|
|
|
Log.Info(AMessage, LOGGERPRO_TAG);
|
2016-09-06 10:30:52 +02:00
|
|
|
TLogType.Warning:
|
2016-11-27 23:17:20 +01:00
|
|
|
Log.Warn(AMessage, LOGGERPRO_TAG);
|
2016-09-06 10:30:52 +02:00
|
|
|
TLogType.Error:
|
2016-11-27 23:17:20 +01:00
|
|
|
Log.Error(AMessage, LOGGERPRO_TAG);
|
2016-09-06 10:30:52 +02:00
|
|
|
else
|
|
|
|
raise Exception.Create('Invalid LOG LEVEL! Original message was: ' + AMessage);
|
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
2016-09-06 10:30:52 +02:00
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
2016-09-06 10:30:52 +02:00
|
|
|
procedure Log(AMessage: string); overload;
|
2016-11-27 23:17:20 +01:00
|
|
|
begin
|
|
|
|
LogI(AMessage);
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure Log(AObject: TObject); overload;
|
|
|
|
begin
|
|
|
|
Log(ObjectToJSON(AObject));
|
|
|
|
end;
|
|
|
|
|
2016-11-27 23:17:20 +01:00
|
|
|
procedure LogI(AMessage: string); overload;
|
2016-09-06 10:30:52 +02:00
|
|
|
begin
|
|
|
|
Log.Info(AMessage, LOGGERPRO_TAG);
|
|
|
|
end;
|
|
|
|
|
2016-11-27 23:17:20 +01:00
|
|
|
procedure LogD(AMessage: string); overload;
|
|
|
|
begin
|
|
|
|
Log.Debug(AMessage, LOGGERPRO_TAG);
|
|
|
|
end;
|
|
|
|
|
2019-03-05 20:55:37 +01:00
|
|
|
procedure LogD(AMessage: TObject); overload;
|
|
|
|
begin
|
|
|
|
LogD(ObjectToJSON(AMessage));
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure LogI(AObject: TObject); overload;
|
|
|
|
begin
|
|
|
|
LogI(ObjectToJSON(AObject));
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure LogW(AObject: TObject); overload;
|
|
|
|
begin
|
|
|
|
LogW(ObjectToJSON(AObject));
|
|
|
|
end;
|
|
|
|
|
2016-09-06 10:30:52 +02:00
|
|
|
procedure SetDefaultLogger(const aLogWriter: ILogWriter);
|
|
|
|
begin
|
2016-11-27 23:17:20 +01:00
|
|
|
if _DefaultLogger = nil then
|
|
|
|
begin
|
|
|
|
TMonitor.Enter(_lock); // double check here
|
|
|
|
try
|
|
|
|
if _DefaultLogger = nil then
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
2016-11-27 23:17:20 +01:00
|
|
|
if aLogWriter <> nil then
|
|
|
|
begin
|
|
|
|
_DefaultLogger := aLogWriter;
|
|
|
|
Log.Info('Custom Logger initialized', LOGGERPRO_TAG);
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
InitializeDefaultLogger;
|
|
|
|
Log.Info('Default Logger initialized', LOGGERPRO_TAG);
|
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
2016-11-27 23:17:20 +01:00
|
|
|
finally
|
|
|
|
TMonitor.Exit(_lock);
|
2016-09-06 10:30:52 +02:00
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2016-09-06 10:30:52 +02:00
|
|
|
procedure InitializeDefaultLogger;
|
2019-05-02 17:38:57 +02:00
|
|
|
var
|
|
|
|
lLogsFolder: String;
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
2016-11-27 23:17:20 +01:00
|
|
|
{ This procedure must be called in a synchronized context
|
|
|
|
(Normally only SetDefaultLogger should be the caller) }
|
|
|
|
if not Assigned(_DefaultLogger) then
|
|
|
|
begin
|
2019-05-02 17:38:57 +02:00
|
|
|
{$IF NOT DEFINED(MOBILE)}
|
|
|
|
lLogsFolder := AppPath + 'logs';
|
|
|
|
{$ELSE}
|
|
|
|
lLogsFolder := TPath.Combine(TPath.GetDocumentsPath, 'logs');
|
|
|
|
{$ENDIF}
|
|
|
|
_DefaultLogger := BuildLogWriter([TLoggerProFileAppender.Create(5, 2000, lLogsFolder)]);
|
2016-09-06 10:30:52 +02:00
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
2016-09-27 14:33:51 +02:00
|
|
|
procedure ReleaseGlobalLogger;
|
|
|
|
begin
|
|
|
|
if _DefaultLogger <> nil then
|
|
|
|
begin
|
2016-10-02 17:44:20 +02:00
|
|
|
TMonitor.Enter(_lock);
|
2016-09-27 14:33:51 +02:00
|
|
|
try
|
|
|
|
if _DefaultLogger <> nil then // double check
|
|
|
|
begin
|
|
|
|
_DefaultLogger := nil;
|
|
|
|
end;
|
|
|
|
finally
|
2016-10-02 17:44:20 +02:00
|
|
|
TMonitor.Exit(_lock);
|
2016-09-27 14:33:51 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2016-09-06 10:30:52 +02:00
|
|
|
initialization
|
|
|
|
|
|
|
|
_lock := TObject.Create;
|
|
|
|
|
|
|
|
{ The TLoggerProFileAppender has its defaults defined as follows:
|
|
|
|
DEFAULT_LOG_FORMAT = '%0:s [TID %1:-8d][%2:-10s] %3:s [%4:s]';
|
|
|
|
DEFAULT_MAX_BACKUP_FILE_COUNT = 5;
|
|
|
|
DEFAULT_MAX_FILE_SIZE_KB = 1000;
|
|
|
|
|
|
|
|
You can override these dafaults passing parameters to the constructor.
|
|
|
|
Here's some configuration examples:
|
|
|
|
@longcode(#
|
|
|
|
// Creates log in the same exe folder without PID in the filename
|
|
|
|
_Log := BuildLogWriter([TLoggerProFileAppender.Create(10, 5,
|
|
|
|
[TFileAppenderOption.LogsInTheSameFolder])]);
|
|
|
|
|
|
|
|
// Creates log in the AppData/Roaming with PID in the filename
|
|
|
|
_Log := BuildLogWriter([TLoggerProFileAppender.Create(10, 5,
|
|
|
|
[TFileAppenderOption.IncludePID])]);
|
|
|
|
|
|
|
|
// Creates log in the same folder with PID in the filename
|
|
|
|
_Log := BuildLogWriter([TLoggerProFileAppender.Create(10, 5,
|
|
|
|
[TFileAppenderOption.IncludePID])]);
|
|
|
|
#)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates log in the ..\..\ folder without PID in the filename
|
|
|
|
|
|
|
|
// DefaultDMVCFrameworkLogger := BuildLogWriter([TLoggerProFileAppender.Create(10, 5)]);
|
|
|
|
// Create logs in the exe' same folder
|
|
|
|
// _Log := BuildLogWriter([TLoggerProFileAppender.Create(10, 5)]);
|
|
|
|
|
|
|
|
// Creates log in the AppData/Roaming with PID in the filename
|
|
|
|
// _Log := BuildLogWriter([TLoggerProFileAppender.Create(10, 5,
|
|
|
|
// [TFileAppenderOption.IncludePID])]);
|
|
|
|
|
|
|
|
// Creates log in the same folder with PID in the filename
|
|
|
|
// _Log := BuildLogWriter([TLoggerProFileAppender.Create(10, 5,
|
|
|
|
// [TFileAppenderOption.IncludePID])]);
|
|
|
|
|
|
|
|
finalization
|
|
|
|
|
|
|
|
_lock.Free;
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
end.
|