Skip to content

Commit

Permalink
Bug fix: log file on IIS application server
Browse files Browse the repository at this point in the history
Log Impovements
  • Loading branch information
viniciussanchez authored Feb 28, 2023
2 parents 8ba0322 + 9d0288d commit 0e9f773
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions src/Horse.Logger.Provider.LogFile.pas
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface
Classes,
{$ELSE}
System.Classes,
Winapi.Windows,
{$ENDIF}
Horse.Logger;

Expand All @@ -19,12 +20,15 @@ THorseLoggerLogFileConfig = class
private
FLogFormat: string;
FDir: string;
FLogName: string;
public
constructor Create;
function SetLogFormat(const ALogFormat: string): THorseLoggerLogFileConfig;
function SetDir(const ADir: string): THorseLoggerLogFileConfig;
function SetLogName(const ALogName: string): THorseLoggerLogFileConfig;
function GetLogFormat(out ALogFormat: string): THorseLoggerLogFileConfig;
function GetDir(out ADir: string): THorseLoggerLogFileConfig;
function GetLogName(out ALogName: string): THorseLoggerLogFileConfig;
class function New: THorseLoggerLogFileConfig;
end;

Expand Down Expand Up @@ -110,18 +114,20 @@ procedure THorseLoggerProviderLogFileManager.DispatchLogCache;
LLog: THorseLoggerLog;
LParams: TArray<string>;
LValue: {$IFDEF FPC}THorseLoggerLogItemString{$ELSE}string{$ENDIF};
LLogStr, LFilename: string;
LLogStr, LFilename, LLogName: string;
LTextFile: TextFile;
begin
if FConfig = nil then
FConfig := THorseLoggerLogFileConfig.New;
FConfig.GetLogFormat(LLogStr).GetDir(LFilename);
FConfig.GetLogFormat(LLogStr).GetLogName(LLogName);

if (LFilename <> EmptyStr) and (not DirectoryExists(LFilename)) then
ForceDirectories(LFilename);
{$IFDEF FPC}
LFilename := ConcatPaths([LFilename, 'access_' + FormatDateTime('yyyy-mm-dd', Now()) + '.log']);
LFilename := ConcatPaths([LFilename, LLogName + '_' + FormatDateTime('yyyy-mm-dd', Now()) + '.log']);
{$ELSE}
LFilename := TPath.Combine(LFilename, 'access_' + FormatDateTime('yyyy-mm-dd', Now()) + '.log');
LFilename := TPath.Combine(LFilename, LLogName + '_' + FormatDateTime('yyyy-mm-dd', Now()) + '.log');
{$ENDIF}
LLogCache := ExtractLogCache;
try
Expand Down Expand Up @@ -166,9 +172,24 @@ function THorseLoggerProviderLogFileManager.SetConfig(AConfig: THorseLoggerLogFi
{ THorseLoggerConfig }

constructor THorseLoggerLogFileConfig.Create;
{$IFNDEF FPC}
const
INVALID_PATH = '\\?\';
var
LPath: array[0..MAX_PATH - 1] of Char;
{$ENDIF}
begin
FLogFormat := DEFAULT_HORSE_LOG_FORMAT;
{$IFDEF FPC}
FDir := ExtractFileDir(ParamStr(0));
FLogName := 'access';
{$ELSE}
SetString(FDir, LPath, GetModuleFileName(HInstance, LPath, SizeOf(LPath)));
FDir := FDir.Replace(INVALID_PATH, EmptyStr);
FLogName := 'access_' + ExtractFileName(FDir).Replace(ExtractFileExt(FDir), EmptyStr);
FDir := ExtractFilePath(FDir) ;
{$ENDIF}
FDir := FDir + '\logs';
end;

function THorseLoggerLogFileConfig.GetDir(out ADir: string): THorseLoggerLogFileConfig;
Expand All @@ -183,6 +204,12 @@ function THorseLoggerLogFileConfig.GetLogFormat(out ALogFormat: string): THorseL
Result := Self;
end;

function THorseLoggerLogFileConfig.GetLogName(out ALogName: string): THorseLoggerLogFileConfig;
begin
ALogName := FLogName;
Result := Self;
end;

class function THorseLoggerLogFileConfig.New: THorseLoggerLogFileConfig;
begin
Result := THorseLoggerLogFileConfig.Create;
Expand All @@ -200,4 +227,10 @@ function THorseLoggerLogFileConfig.SetLogFormat(const ALogFormat: string): THors
Result := Self;
end;

function THorseLoggerLogFileConfig.SetLogName(const ALogName: string): THorseLoggerLogFileConfig;
begin
FLogName := ALogName;
Result := Self;
end;

end.

0 comments on commit 0e9f773

Please sign in to comment.