Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New generic Dataset logger and VCL Sample #120

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 33 additions & 57 deletions Quick.Logger.Provider.ADODB.pas
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,11 @@ interface
Data.Win.ADODB,
Winapi.ActiveX,
Quick.Commons,
Quick.Logger;
Quick.Logger,
Quick.Logger.Provider.BaseDB;

type

TFieldsMapping = class
private
fEventDate : string;
fEventType : string;
fMsg : string;
fEnvironment : string;
fPlatformInfo : string;
fOSVersion : string;
fAppName : string;
fUserName : string;
fHost : string;
public
property EventDate : string read fEventDate write fEventDate;
property EventType : string read fEventType write fEventType;
property Msg : string read fMsg write fMsg;
property Environment : string read fEnvironment write fEnvironment;
property PlatformInfo : string read fPlatformInfo write fPlatformInfo;
property OSVersion : string read fOSVersion write fOSVersion;
property AppName : string read fAppName write fAppName;
property UserName : string read fUserName write fUserName;
property Host : string read fHost write fHost;
end;

TDBProvider = (dbMSAccess2000, dbMSAccess2007, dbMSSQL, dbMSSQLnc10, dbMSSQLnc11, dbAS400);

const
Expand Down Expand Up @@ -93,13 +71,12 @@ TDBConfig = class
end;
{$M-}

TLogADODBProvider = class (TLogProviderBase)
TLogADODBProvider = class (TLogBaseDBProvider)
private
fDBConnection : TADOConnection;
fDBQuery : TADOQuery;
fConnectionString : string;
fDBConfig : TDBConfig;
fFieldsMapping : TFieldsMapping;
function CreateConnectionString : string;
function CreateTable : Boolean;
procedure AddColumnToTable(const aColumnName, aDataType : string);
Expand All @@ -109,8 +86,7 @@ TLogADODBProvider = class (TLogProviderBase)
constructor Create; override;
destructor Destroy; override;
property ConnectionString : string read fConnectionString write fConnectionString;
property DBConfig : TDBConfig read fDBConfig write fDBConfig;
property FieldsMapping : TFieldsMapping read fFieldsMapping write fFieldsMapping;
property DBConfig : TDBConfig read fDBConfig ;
procedure Init; override;
procedure Restart; override;
procedure WriteLog(cLogItem : TLogItem); override;
Expand All @@ -127,16 +103,16 @@ constructor TLogADODBProvider.Create;
CoInitialize(nil);
LogLevel := LOG_ALL;
//set default db fields mapping
fFieldsMapping := TFieldsMapping.Create;
fFieldsMapping.EventDate := 'EventDate';
fFieldsMapping.EventType := 'EventType';
fFieldsMapping.Msg := 'Msg';
fFieldsMapping.Environment := 'Environment';
fFieldsMapping.PlatformInfo := 'PlaftormInfo';
fFieldsMapping.OSVersion := 'OSVersion';
fFieldsMapping.AppName := 'AppName';
fFieldsMapping.UserName := 'UserName';
fFieldsMapping.Host := 'Host';
FieldsMapping.EventDate := 'EventDate';
FieldsMapping.EventType := 'EventType';
FieldsMapping.Msg := 'Msg';
FieldsMapping.Environment := 'Environment';
FieldsMapping.PlatformInfo := 'PlatformInfo';
FieldsMapping.OSVersion := 'OSVersion';
FieldsMapping.AppName := 'AppName';
FieldsMapping.UserName := 'UserName';
FieldsMapping.Host := 'Host';
FieldsMapping.ThreadId := 'ThreadId';
//set default db config
fDBConfig := TDBConfig.Create;
fDBConfig.Provider := dbMSSQL;
Expand Down Expand Up @@ -174,26 +150,26 @@ function TLogADODBProvider.CreateTable: Boolean;
fDBQuery.SQL.Clear;
fDBQuery.SQL.Add('IF NOT EXISTS (SELECT name FROM sys.tables WHERE name = :LOGTABLE)');
fDBQuery.SQL.Add(Format('CREATE TABLE %s (',[fDBConfig.Table]));
fDBQuery.SQL.Add(Format('%s DateTime,',[fFieldsMapping.EventDate]));
fDBQuery.SQL.Add(Format('%s varchar(20),',[fFieldsMapping.EventType]));
fDBQuery.SQL.Add(Format('%s varchar(max));',[fFieldsMapping.Msg]));
fDBQuery.SQL.Add(Format('%s DateTime,',[FieldsMapping.EventDate]));
fDBQuery.SQL.Add(Format('%s varchar(20),',[FieldsMapping.EventType]));
fDBQuery.SQL.Add(Format('%s varchar(max));',[FieldsMapping.Msg]));
fDBQuery.Parameters.ParamByName('LOGTABLE').Value := fDBConfig.Table;
if fDBQuery.ExecSQL = 0 then raise Exception.Create('Error creating table!');

if iiEnvironment in IncludedInfo then AddColumnToTable(fFieldsMapping.Environment,'varchar(50)');
if iiPlatform in IncludedInfo then AddColumnToTable(fFieldsMapping.PlatformInfo,'varchar(50)');
if iiOSVersion in IncludedInfo then AddColumnToTable(fFieldsMapping.OSVersion,'varchar(70)');
if iiHost in IncludedInfo then AddColumnToTable(fFieldsMapping.Host,'varchar(20)');
if iiUserName in IncludedInfo then AddColumnToTable(fFieldsMapping.UserName,'varchar(50)');
if iiAppName in IncludedInfo then AddColumnToTable(fFieldsMapping.AppName,'varchar(70)');
if iiEnvironment in IncludedInfo then AddColumnToTable(FieldsMapping.Environment,'varchar(50)');
if iiPlatform in IncludedInfo then AddColumnToTable(FieldsMapping.PlatformInfo,'varchar(50)');
if iiOSVersion in IncludedInfo then AddColumnToTable(FieldsMapping.OSVersion,'varchar(70)');
if iiHost in IncludedInfo then AddColumnToTable(FieldsMapping.Host,'varchar(20)');
if iiUserName in IncludedInfo then AddColumnToTable(FieldsMapping.UserName,'varchar(50)');
if iiAppName in IncludedInfo then AddColumnToTable(FieldsMapping.AppName,'varchar(70)');
if iiThreadId in IncludedInfo then AddColumnToTable(FieldsMapping.ThreadId,'varchar(30)');
Result := True;
end;

destructor TLogADODBProvider.Destroy;
begin
if Assigned(fDBQuery) then fDBQuery.Free;
if Assigned(fDBConnection) then fDBConnection.Free;
fFieldsMapping.Free;
fDBConfig.Free;
CoUninitialize;
inherited;
Expand Down Expand Up @@ -239,16 +215,16 @@ procedure TLogADODBProvider.WriteLog(cLogItem : TLogItem);
values : string;
begin
//prepare fields and values for insert query
fields := fFieldsMapping.EventDate;
fields := FieldsMapping.EventDate;
values := Format('''%s''',[DateTimeToStr(cLogItem.EventDate,FormatSettings)]);
AddToQuery(fields,values,fFieldsMapping.EventType,EventTypeName[cLogItem.EventType]);
if iiHost in IncludedInfo then AddToQuery(fields,values,fFieldsMapping.Host,SystemInfo.HostName);
if iiAppName in IncludedInfo then AddToQuery(fields,values,fFieldsMapping.AppName,SystemInfo.AppName);
if iiEnvironment in IncludedInfo then AddToQuery(fields,values,fFieldsMapping.Environment,Environment);
if iiOSVersion in IncludedInfo then AddToQuery(fields,values,fFieldsMapping.OSVersion,SystemInfo.OsVersion);
if iiPlatform in IncludedInfo then AddToQuery(fields,values,fFieldsMapping.PlatformInfo,PlatformInfo);
if iiUserName in IncludedInfo then AddToQuery(fields,values,fFieldsMapping.UserName,SystemInfo.UserName);
AddToQuery(fields,values,fFieldsMapping.Msg,StringReplace(cLogItem.Msg,'''','"',[rfReplaceAll]));
AddToQuery(fields,values,FieldsMapping.EventType,EventTypeName[cLogItem.EventType]);
if iiHost in IncludedInfo then AddToQuery(fields,values,FieldsMapping.Host,SystemInfo.HostName);
if iiAppName in IncludedInfo then AddToQuery(fields,values,FieldsMapping.AppName,SystemInfo.AppName);
if iiEnvironment in IncludedInfo then AddToQuery(fields,values,FieldsMapping.Environment,Environment);
if iiOSVersion in IncludedInfo then AddToQuery(fields,values,FieldsMapping.OSVersion,SystemInfo.OsVersion);
if iiPlatform in IncludedInfo then AddToQuery(fields,values,FieldsMapping.PlatformInfo,PlatformInfo);
if iiUserName in IncludedInfo then AddToQuery(fields,values,FieldsMapping.UserName,SystemInfo.UserName);
AddToQuery(fields,values,FieldsMapping.Msg,StringReplace(cLogItem.Msg,'''','"',[rfReplaceAll]));

fDBQuery.SQL.Clear;
fDBQuery.SQL.Add(Format('INSERT INTO %s (%s)',[fDBConfig.Table,fields]));
Expand Down
91 changes: 91 additions & 0 deletions Quick.Logger.Provider.BaseDB.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{ ***************************************************************************

Copyright (c) 2023 Jens Fudickar

Unit : Quick.Logger.Provider.BaseDB
Description : Base classes for all database log providers
Author : Jens Fudickar
Version : 1.0

This file is part of QuickLogger: https://github.com/exilon/QuickLogger

***************************************************************************

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 Quick.Logger.Provider.BaseDB;

{$i QuickLib.inc}

interface

uses
Classes,
SysUtils,
Quick.Logger;

type

TLogDbFieldsMapping = class
private
fEventDate : string;
fEventType : string;
fMsg : string;
fEnvironment : string;
fPlatformInfo : string;
fOSVersion : string;
fAppName : string;
fUserName : string;
fHost : string;
FThreadId: string;
public
property EventDate : string read fEventDate write fEventDate;
property EventType : string read fEventType write fEventType;
property Msg : string read fMsg write fMsg;
property Environment : string read fEnvironment write fEnvironment;
property PlatformInfo : string read fPlatformInfo write fPlatformInfo;
property OSVersion : string read fOSVersion write fOSVersion;
property AppName : string read fAppName write fAppName;
property UserName : string read fUserName write fUserName;
property Host : string read fHost write fHost;
property ThreadId: string read FThreadId write FThreadId;
end;


TLogBaseDBProvider = class (TLogProviderBase)
private
FFieldsMapping: TLogDbFieldsMapping;
public
constructor Create; override;
destructor Destroy; override;
property FieldsMapping: TLogDbFieldsMapping read FFieldsMapping;
end;

implementation

constructor TLogBaseDBProvider.Create;
begin
inherited;
//create default db fields mapping
fFieldsMapping := TLogDbFieldsMapping.Create;
end;


destructor TLogBaseDBProvider.Destroy;
begin
fFieldsMapping.Free;
inherited;
end;

end.
Loading