TDBXLogInserterUnit.pas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 TDBXLogInserter class.
  15. @version 0.5
  16. @author <a href="mailto:Bartosz.Tomasik@gmail.com">Bartosz Tomasik</a>
  17. @author <a href="mailto:tcmiller@users.sourceforge.net">Trevor Miller</a>
  18. ----------------------------------------------------------------------------}
  19. unit TDBXLogInserterUnit;
  20. interface
  21. uses
  22. SqlExpr, TDBLogInserterUnit, TLoggingEventUnit;
  23. type
  24. TDBXLogInserter = class (TDBLogInserter)
  25. protected
  26. FSQLConn : TSQLConnection;
  27. FSQLQuery : TSQLQuery;
  28. public
  29. constructor Create(ASqlConnection: TSQLConnection; ASQL : String);
  30. destructor Destroy; Override;
  31. procedure Insert(AEvent : TLoggingEvent); Override;
  32. end;
  33. implementation
  34. uses
  35. SysUtils, SqlTimSt, TLogLogUnit;
  36. constructor TDBXLogInserter.Create(ASqlConnection: TSQLConnection; ASQL : String);
  37. begin
  38. inherited Create;
  39. FSqlConn := ASqlConnection;
  40. FSQLQuery := TSQLQuery.Create(Nil);
  41. FSQLQuery.SQLConnection:=self.FsqlConn;
  42. FSQLQuery.ParamCheck:=true;
  43. FSQLQuery.SQL.Add(ASQL);
  44. end;
  45. destructor TDBXLogInserter.Destroy;
  46. begin
  47. FSQLQuery.Free;
  48. inherited Destroy;
  49. end;
  50. procedure TDBXLogInserter.Insert(AEvent : TLoggingEvent);
  51. begin
  52. if not Assigned(FSqlConn) then
  53. begin
  54. TLogLog.debug('TDBXLogInserter#Insert: SqlConnection not Assigned');
  55. exit;
  56. end;
  57. try
  58. FSqlConn.Open;
  59. except
  60. on e : Exception do
  61. begin
  62. TLogLog.fatal('TDBXLogInserter#Insert: SqlConnection.Open raised an expeption: '
  63. + e.message);
  64. exit;
  65. end;
  66. end;
  67. if Assigned(FSQLQuery.Params.FindParam('_msg')) then
  68. begin
  69. FSQLQuery.ParamByName('_msg').AsString:=AEvent.getMessage;
  70. end;
  71. if (AEvent.getException<>nil) then
  72. begin
  73. if Assigned(FSQLQuery.Params.FindParam('_exception')) then
  74. FSQLQuery.ParamByName('_exception').AsString:=AEvent.getException.Message;
  75. if Assigned(FSQLQuery.Params.FindParam('_exceptionclass')) then
  76. FSQLQuery.ParamByName('_exceptionclass').AsString :=
  77. AEvent.getException.ClassType.ClassName;
  78. end;
  79. if Assigned(FSQLQuery.Params.FindParam('_startTime')) then
  80. FSQLQuery.ParamByName('_startTime').AsSQLTimeStamp :=
  81. DateTimeToSQLTimeStamp(AEvent.getStartTime);
  82. if Assigned(FSQLQuery.Params.FindParam('_level')) then
  83. FSQLQuery.ParamByName('_level').AsString:=AEvent.getLevel.toString;
  84. if Assigned(FSQLQuery.Params.FindParam('_levelCode')) then
  85. FSQLQuery.ParamByName('_levelCode').AsInteger:=AEvent.getLevel.intValue;
  86. try
  87. FSQLQuery.ExecSQL();
  88. except
  89. on e : Exception do
  90. begin
  91. TLogLog.fatal('TDbXLogInserter#insert: FSQLQuery.ExecSql raised an expeption: '
  92. + e.message);
  93. end;
  94. end;
  95. FSQLQuery.Close;
  96. end;
  97. end.