mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-17 08:45:55 +01:00
b22a41ca91
0b6857bd Updated unit tests and automatic tasks 35d130d6 VERSION_1_4_4 2e4a80f4 Removed PasDoc documentation; Added unit tests for LogLayoutToLogFormat 8f61bc22 Removed TLogAppenderOptions, DEFAULT_LOG_FILENAME_FORMAT is now TLogLayout.* (there many possibilities); FileName layout use placeholders instead of indices (as format function does). be43f781 Merge branch 'master' into v2.0 git-subtree-dir: lib/loggerpro git-subtree-split: 0b6857bd8d51f5acb246561564b3c3bbd963d192
106 lines
2.9 KiB
ObjectPascal
106 lines
2.9 KiB
ObjectPascal
// *************************************************************************** }
|
|
//
|
|
// LoggerPro
|
|
//
|
|
// Copyright (c) 2010-2023 Daniele Teti
|
|
//
|
|
// https://github.com/danieleteti/loggerpro
|
|
//
|
|
// ***************************************************************************
|
|
//
|
|
// 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.
|
|
//
|
|
// ***************************************************************************
|
|
|
|
unit LoggerPro.RedisAppender;
|
|
|
|
interface
|
|
|
|
uses
|
|
LoggerPro, System.Classes, System.DateUtils,
|
|
Redis.Commons {Redis.Commons is included in DelphiRedisClient, also available with GETIT} ,
|
|
Redis.NetLib.INDY {Redis.NetLib.INDY is included in DelphiRedisClient, also available with GETIT};
|
|
|
|
type
|
|
{
|
|
@abstract(Logs to a Redis instance)
|
|
To learn how to use this appender, check the sample @code(remote_redis_appender.dproj)
|
|
@author(Daniele Teti - d.teti@bittime.it)
|
|
}
|
|
TLoggerProRedisAppender = class(TLoggerProAppenderBase)
|
|
private
|
|
FRedis: IRedisClient;
|
|
FLogKeyPrefix: string;
|
|
FMaxSize: Int64;
|
|
public
|
|
constructor Create(aRedis: IRedisClient; aMaxSize: Int64 = 5000; aKeyPrefix: string = 'loggerpro'; aLogLayout: string = TLogLayout.LOG_LAYOUT_0); reintroduce;
|
|
procedure Setup; override;
|
|
procedure TearDown; override;
|
|
procedure WriteLog(const aLogItem: TLogItem); override;
|
|
procedure TryToRestart(var Restarted: Boolean); override;
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
System.SysUtils;
|
|
|
|
constructor TLoggerProRedisAppender.Create(aRedis: IRedisClient; aMaxSize: Int64; aKeyPrefix: string; aLogLayout: string);
|
|
begin
|
|
inherited Create(aLogLayout);
|
|
FRedis := aRedis;
|
|
FLogKeyPrefix := aKeyPrefix;
|
|
FMaxSize := aMaxSize;
|
|
end;
|
|
|
|
procedure TLoggerProRedisAppender.Setup;
|
|
begin
|
|
inherited;
|
|
FRedis.Connect;
|
|
end;
|
|
|
|
procedure TLoggerProRedisAppender.TearDown;
|
|
begin
|
|
// do nothing
|
|
end;
|
|
|
|
procedure TLoggerProRedisAppender.TryToRestart(var Restarted: Boolean);
|
|
begin
|
|
inherited;
|
|
Restarted := False;
|
|
try
|
|
FRedis.Disconnect
|
|
except
|
|
end;
|
|
try
|
|
FRedis.Connect;
|
|
Restarted := True;
|
|
except
|
|
end;
|
|
end;
|
|
|
|
procedure TLoggerProRedisAppender.WriteLog(const aLogItem: TLogItem);
|
|
var
|
|
lText: string;
|
|
lKey: string;
|
|
begin
|
|
lText := FormatLog(aLogItem);
|
|
lKey := FLogKeyPrefix + '::logs'; // + aLogItem.LogTypeAsString.ToLower;
|
|
// Push the log item to the right of the list (logs:info, logs:warning, log:error)
|
|
FRedis.RPUSH(lKey, [lText]);
|
|
// Trim the list to the FMaxSize last elements
|
|
FRedis.LTRIM(lKey, -FMaxSize, -1);
|
|
end;
|
|
|
|
end.
|