1 {*********************************************************}
3 { Zeos Database Objects }
4 { Database Logging Classes and Interfaces }
6 { Originally written by Sergey Seroukhov }
8 {*********************************************************}
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
13 { License Agreement: }
15 { This library is distributed in the hope that it will be }
16 { useful, but WITHOUT ANY WARRANTY; without even the }
17 { implied warranty of MERCHANTABILITY or FITNESS FOR }
18 { A PARTICULAR PURPOSE. See the GNU Lesser General }
19 { Public License for more details. }
21 { The source code of the ZEOS Libraries and packages are }
22 { distributed under the Library GNU General Public }
23 { License (see the file COPYING / COPYING.ZEOS) }
24 { with the following modification: }
25 { As a special exception, the copyright holders of this }
26 { library give you permission to link this library with }
27 { independent modules to produce an executable, }
28 { regardless of the license terms of these independent }
29 { modules, and to copy and distribute the resulting }
30 { executable under terms of your choice, provided that }
31 { you also meet, for each linked independent module, }
32 { the terms and conditions of the license of that module. }
33 { An independent module is a module which is not derived }
34 { from or based on this library. If you modify this }
35 { library, you may extend this exception to your version }
36 { of the library, but you are not obligated to do so. }
37 { If you do not wish to do so, delete this exception }
38 { statement from your version. }
41 { The project web site is located on: }
42 { http://zeos.firmos.at (FORUM) }
43 { http://sourceforge.net/p/zeoslib/tickets/ (BUGTRACKER)}
44 { svn://svn.code.sf.net/p/zeoslib/code-0/trunk (SVN) }
46 { http://www.sourceforge.net/projects/zeoslib. }
49 { Zeos Development Group. }
50 {********************************************************@}
58 uses SysUtils, ZClasses;
62 {** Defines a time or the message. }
63 TZLoggingCategory = (lcConnect, lcDisconnect, lcTransaction, lcExecute, lcOther,
64 lcPrepStmt, lcBindPrepStmt, lcExecPrepStmt, lcUnprepStmt);
66 {** Defines a object for logging event. }
67 TZLoggingEvent = class;
69 {** Defines an interface to format logging events. }
70 IZLoggingFormatter = interface (IZInterface)
71 // ['{53559F5F-AC22-4DDC-B2EA-45D21ADDD2D5}']
72 function Format(LoggingEvent: TZLoggingEvent) : string;
75 { TZLoggingFormatter }
76 {** Defines a object for logging event. }
77 TZLoggingFormatter = class (TInterfacedObject, IZLoggingFormatter)
80 function Format(LoggingEvent: TZLoggingEvent) : string; virtual;
83 {** Defines a object for logging event. }
84 TZLoggingEvent = class (TObject)
86 FCategory: TZLoggingCategory;
91 FTimestamp: TDateTime;
93 constructor Create(Category: TZLoggingCategory; Protocol: string;
94 Msg: string; ErrorCode: Integer; Error: string);
96 function AsString(LoggingFormatter:IZLoggingFormatter = nil): string;
98 property Category: TZLoggingCategory read FCategory;
99 property Protocol: string read FProtocol;
100 property Message: string read FMessage;
101 property ErrorCode: Integer read FErrorCode;
102 property Error: string read FError;
103 property Timestamp: TDateTime read FTimestamp;
106 {** Defines an interface to accept logging events. }
107 IZLoggingListener = interface (IZInterface)
108 ['{53559F5F-AC22-4DDC-B2EA-45D21ADDD2D4}']
110 procedure LogEvent(Event: TZLoggingEvent);
114 var DefaultLoggingFormatter: TZLoggingFormatter;
116 { TZLoggingFormatter }
118 function TZLoggingFormatter.Format(LoggingEvent: TZLoggingEvent): string;
120 Result := FormatDateTime('yyyy-mm-dd hh:mm:ss', LoggingEvent.Timestamp) + ' cat: ';
121 case LoggingEvent.Category of
122 lcConnect: Result := Result + 'Connect';
123 lcDisconnect: Result := Result + 'Disconnect';
124 lcTransaction: Result := Result + 'Transaction';
125 lcExecute: Result := Result + 'Execute';
126 lcPrepStmt: Result := Result + 'Prepare';
127 lcBindPrepStmt: Result := Result + 'Bind prepared';
128 lcExecPrepStmt: Result := Result + 'Execute prepared';
129 lcUnprepStmt: Result := Result + 'Unprepare prepared';
131 Result := Result + 'Other';
133 if LoggingEvent.Protocol <> '' then
134 Result := Result + ', proto: ' + LoggingEvent.Protocol;
135 Result := Result + ', msg: ' + LoggingEvent.Message;
136 if (LoggingEvent.ErrorCode <> 0) or (LoggingEvent.Error <> '') then
138 Result := Result + ', errcode: ' + IntToStr(LoggingEvent.ErrorCode)
139 + ', error: ' + LoggingEvent.Error;
146 Constructs this logging event.
147 @param Protocol a DBC protocol.
148 @param Msg a description message.
149 @param ErrorCode an error code.
150 @param Error an error message.
152 constructor TZLoggingEvent.Create(Category: TZLoggingCategory;
153 Protocol: string; Msg: string; ErrorCode: Integer; Error: string);
155 FCategory := Category;
156 FProtocol := Protocol;
158 FErrorCode := ErrorCode;
164 Gets a string representation for this event.
165 @returns a string representation.
167 function TZLoggingEvent.AsString(LoggingFormatter:IZLoggingFormatter = nil): string;
169 If Assigned(LoggingFormatter) then
170 Result := LoggingFormatter.Format(Self)
172 Result := DefaultLoggingFormatter.Format(Self);
176 DefaultLoggingFormatter := TZLoggingFormatter.Create;
179 DefaultLoggingFormatter.Free;