TLoggerUnit.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. {
  2. Copyright 2005-2006 Log4Delphi Project
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. }
  13. {*----------------------------------------------------------------------------
  14. Contains the TLogger class.
  15. @version 0.5
  16. @author <a href="mailto:tcmiller@users.sourceforge.net">Trevor Miller</a>
  17. ----------------------------------------------------------------------------}
  18. unit TLoggerUnit;
  19. {$ifdef fpc}
  20. {$mode objfpc}
  21. {$h+}
  22. {$endif}
  23. interface
  24. uses
  25. SysUtils, Classes,
  26. TLogLogUnit, TLevelUnit, TAppenderUnit, TLoggingEventUnit;
  27. type
  28. {*----------------------------------------------------------------------------
  29. This is the central class in the log4delphi suite. Most logging operations,
  30. except configuration, are done through this class.
  31. ----------------------------------------------------------------------------}
  32. TLogger = class (TObject)
  33. private
  34. FAppenders : TAppendersCollection;
  35. FLevel : TLevel;
  36. FName : String;
  37. protected
  38. constructor Create(const AName : String);
  39. destructor Destroy; Reintroduce;
  40. public
  41. class function GetInstance() : TLogger; Overload;
  42. class function GetInstance(const AName : String) : TLogger; Overload;
  43. procedure SetLevel(ALevel : TLevel);
  44. procedure AddAppender(AAppender : IAppender);
  45. procedure RemoveAppender(const AName : String);
  46. procedure RemoveAllAppenders();
  47. function GetAppender(const AName : String) : IAppender;
  48. function GetAllAppenders() : TAppendersCollection;
  49. function GetLevel() : TLevel;
  50. function GetName() : String;
  51. procedure Log(AEvent : TLoggingEvent); Overload;
  52. procedure Log(ALevel : TLevel; const AMsg : String); Overload;
  53. procedure Log(ALevel : TLevel; const AMsg : String; AException
  54. : exception); Overload;
  55. procedure Fatal(const AMsg : String);
  56. procedure Error(const AMsg : String);
  57. procedure Warn(const AMsg : String);
  58. procedure Info(const AMsg : String);
  59. procedure Debug(const AMsg : String);
  60. procedure Trace(const AMsg : String);
  61. end;
  62. procedure setDefaultThreshold(ALevel : TLevel);
  63. implementation
  64. var
  65. instances : TStrings;
  66. defaultThreshold : TLevel;
  67. {*----------------------------------------------------------------------------
  68. Initailize the loggers.
  69. ----------------------------------------------------------------------------}
  70. procedure initialize();
  71. begin
  72. defaultThreshold := TLevelUnit.ALL;
  73. if not assigned(instances) then
  74. instances := TStringList.Create;
  75. if instances.IndexOf('ROOT') = -1 then
  76. instances.AddObject('ROOT', TLogger.Create('ROOT'));
  77. end;
  78. {*----------------------------------------------------------------------------
  79. Set the default threshold to all.
  80. ----------------------------------------------------------------------------}
  81. procedure SetDefaultThreshold(ALevel : TLevel);
  82. begin
  83. if (ALevel <> Nil) then
  84. defaultThreshold := ALevel;
  85. end;
  86. {*----------------------------------------------------------------------------
  87. Protected constuctor to prevent instantiation. The singleton instance
  88. should be accessed using the getInstance method.
  89. ----------------------------------------------------------------------------}
  90. constructor TLogger.Create(const AName: String);
  91. begin
  92. inherited Create;
  93. FAppenders := TAppendersCollection.Create;
  94. FLevel := defaultThreshold;
  95. FName := AName;
  96. TLogLog.debug('Logger created - name=' + FName + ', level=' + FLevel.toString);
  97. end;
  98. {*----------------------------------------------------------------------------
  99. Protected destructor to prevent destruction. The singleton instance should
  100. be destroyed upon application termination using the freeInstance method.
  101. ----------------------------------------------------------------------------}
  102. destructor TLogger.Destroy;
  103. begin
  104. FAppenders.Clear;
  105. FAppenders.Free;
  106. TLogLog.debug('Logger destroyed - name=' + FName);
  107. Inherited Destroy;
  108. end;
  109. {*----------------------------------------------------------------------------
  110. Destroy the all instances.
  111. ----------------------------------------------------------------------------}
  112. procedure FreeInstances;
  113. var
  114. i : Integer;
  115. begin
  116. if instances = nil then
  117. Exit;
  118. for i := 0 to instances.Count-1 do
  119. if instances.Objects[i] <> nil then
  120. TLogger(instances.Objects[i]).Destroy;
  121. instances.Free;
  122. instances := nil;
  123. TLogLogUnit.finalize;
  124. end;
  125. {*----------------------------------------------------------------------------
  126. Retrun a reference to the ROOT logger.
  127. @return Root Logger instance
  128. ----------------------------------------------------------------------------}
  129. class function TLogger.GetInstance() : TLogger;
  130. begin
  131. Result := TLogger(instances.Objects[0]);
  132. end;
  133. {*----------------------------------------------------------------------------
  134. Retrun a reference to the named logger.
  135. @param AName The name of the logger
  136. @return Named Logger instance
  137. ----------------------------------------------------------------------------}
  138. class function TLogger.GetInstance(const AName : String) : TLogger;
  139. var
  140. index : Integer;
  141. logger : TLogger;
  142. begin
  143. index := Instances.IndexOf(AName);
  144. if (index < 0) then begin
  145. logger := TLogger.Create(AName);
  146. instances.AddObject(AName, logger);
  147. Result := logger;
  148. end else
  149. Result := TLogger(instances.Objects[index]);
  150. end;
  151. {*----------------------------------------------------------------------------
  152. Set the level of this Logger.
  153. @param ALevel The level to set
  154. ----------------------------------------------------------------------------}
  155. procedure TLogger.SetLevel(ALevel : TLevel);
  156. begin
  157. Self.FLevel := ALevel;
  158. TLogLog.Debug('TLogger.SetLevel: ' + ALevel.ToString);
  159. end;
  160. {*----------------------------------------------------------------------------
  161. Add an appender to the list of appenders of this Logger.
  162. @param AAppender The appender to add
  163. ----------------------------------------------------------------------------}
  164. procedure TLogger.AddAppender(AAppender : IAppender);
  165. begin
  166. FAppenders.Add(AAppender);
  167. TLogLog.debug('Appender added to ' + FName + ', named ' + AAppender.getName);
  168. end;
  169. {*----------------------------------------------------------------------------
  170. Remove the appender from the list of appenders.
  171. @param AName The name of the appender to remove
  172. ----------------------------------------------------------------------------}
  173. procedure TLogger.RemoveAppender(const AName : String);
  174. begin
  175. FAppenders.Delete(AName);
  176. TLogLog.debug('Appender removed from ' + FName + ', named ' + AName);
  177. end;
  178. {*----------------------------------------------------------------------------
  179. Remove all the appenders from this logger.
  180. ----------------------------------------------------------------------------}
  181. procedure TLogger.RemoveAllAppenders();
  182. begin
  183. FAppenders.Clear;
  184. TLogLog.Debug('TLogger.RemoveAllAppenders');
  185. end;
  186. {*----------------------------------------------------------------------------
  187. Return the appender with that name if in the list. Return Nil otherwise.
  188. @param AName The name of the appender
  189. @return The appender or Nil if not found
  190. ----------------------------------------------------------------------------}
  191. function TLogger.GetAppender(const AName : String) : IAppender;
  192. begin
  193. Result := FAppenders.FindByName(AName);
  194. end;
  195. {*----------------------------------------------------------------------------
  196. Get the appenders contained in this Logger as a TStrings instance. The
  197. caller should not destroy the TStrings instance.
  198. @return All appenders in a TStrings instance
  199. ----------------------------------------------------------------------------}
  200. function TLogger.GetAllAppenders() : TAppendersCollection;
  201. begin
  202. getAllAppenders := self.FAppenders;
  203. end;
  204. {*----------------------------------------------------------------------------
  205. Returns the assigned Level, if any, for this Logger.
  206. @return The level of this Logger
  207. ----------------------------------------------------------------------------}
  208. function TLogger.GetLevel() : TLevel;
  209. begin
  210. Result := Self.FLevel;
  211. end;
  212. {*----------------------------------------------------------------------------
  213. Return the logger name.
  214. @return Logger name
  215. ----------------------------------------------------------------------------}
  216. function TLogger.GetName() : String;
  217. begin
  218. Result := Self.FName;
  219. end;
  220. {*----------------------------------------------------------------------------
  221. Send the event to all appender on condition that the event's level is
  222. greater or equal to this Logger's level.
  223. @param AEvent The logging event to log
  224. ----------------------------------------------------------------------------}
  225. procedure TLogger.Log(AEvent : TLoggingEvent);
  226. var
  227. i : Integer;
  228. begin
  229. if (AEvent.getLevel.isGreaterOrEqual(Self.FLevel)) then
  230. for i := 0 to FAppenders.Count-1 do
  231. FAppenders[i].doAppend(AEvent);
  232. end;
  233. {*----------------------------------------------------------------------------
  234. A generic form used to log a message. A new LoggingEvent is created and
  235. then destroyed upon logging the event.
  236. @param ALevel The level of the message to log
  237. @param AMsg The message to log
  238. ----------------------------------------------------------------------------}
  239. procedure TLogger.Log(ALevel : TLevel; const AMsg : String);
  240. var
  241. event : TLoggingEvent;
  242. begin
  243. event := TLoggingEvent.Create(ALevel, AMsg, FName);
  244. log(event);
  245. event.Destroy;
  246. end;
  247. {*----------------------------------------------------------------------------
  248. A generic form used to log a message. A new LoggingEvent is created and
  249. then destroyed upon logging the event.
  250. @param ALevel The level of the message to log
  251. @param AMsg The message to log
  252. @param AException The Exception
  253. ----------------------------------------------------------------------------}
  254. procedure TLogger.Log(ALevel : TLevel; const AMsg : String; AException
  255. : exception);
  256. var
  257. event : TLoggingEvent;
  258. begin
  259. event := TLoggingEvent.Create(ALevel, AMsg, FName, AException);
  260. log(event);
  261. event.Destroy;
  262. end;
  263. {*----------------------------------------------------------------------------
  264. A generic form used to log a fatal message.
  265. ----------------------------------------------------------------------------}
  266. procedure TLogger.Fatal(const AMsg : String);
  267. begin
  268. log(TLevelUnit.FATAL, AMsg);
  269. end;
  270. {*----------------------------------------------------------------------------
  271. A generic form used to log an error message.
  272. ----------------------------------------------------------------------------}
  273. procedure TLogger.Error(const AMsg : String);
  274. begin
  275. log(TLevelUnit.ERROR, AMsg);
  276. end;
  277. {*----------------------------------------------------------------------------
  278. A generic form used to log a warn message.
  279. ----------------------------------------------------------------------------}
  280. procedure TLogger.Warn(const AMsg : String);
  281. begin
  282. log(TLevelUnit.WARN, AMsg);
  283. end;
  284. {*----------------------------------------------------------------------------
  285. A generic form used to log an info message.
  286. ----------------------------------------------------------------------------}
  287. procedure TLogger.Info(const AMsg : String);
  288. begin
  289. log(TLevelUnit.INFO, AMsg);
  290. end;
  291. {*----------------------------------------------------------------------------
  292. A generic form used to log a debug message.
  293. ----------------------------------------------------------------------------}
  294. procedure TLogger.Debug(const AMsg : String);
  295. begin
  296. log(TLevelUnit.DEBUG, AMsg);
  297. end;
  298. {*----------------------------------------------------------------------------
  299. A generic form used to log a trace message.
  300. ----------------------------------------------------------------------------}
  301. procedure TLogger.Trace(const AMsg : String);
  302. begin
  303. log(TLevelUnit.TRACE, AMsg);
  304. end;
  305. initialization
  306. initialize();
  307. finalization
  308. freeInstances();
  309. end.