TXMLLayoutUnit.pas 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 TXMLLayout class.
  15. @version 0.5
  16. @author <a href="mailto:tcmiller@users.sourceforge.net">Trevor Miller</a>
  17. ----------------------------------------------------------------------------}
  18. unit TXMLLayoutUnit;
  19. {$ifdef fpc}
  20. {$mode objfpc}
  21. {$h+}
  22. {$endif}
  23. interface
  24. uses
  25. TLayoutUnit, TLoggingEventUnit;
  26. type
  27. {*----------------------------------------------------------------------------
  28. Format logging events as XML.
  29. ----------------------------------------------------------------------------}
  30. TXMLLayout = class (TLayout)
  31. private
  32. public
  33. function Format(AEvent : TLoggingEvent) : String; Override;
  34. function GetContentType() : String; Override;
  35. function IgnoresException() : Boolean; Override;
  36. end;
  37. implementation
  38. uses
  39. SysUtils;
  40. {*----------------------------------------------------------------------------
  41. Returns the log statement in an XML format.
  42. @param AEvent the event to format
  43. @return The event formatted as an XML string
  44. ----------------------------------------------------------------------------}
  45. function TXMLLayout.Format(AEvent : TLoggingEvent) : String;
  46. var
  47. tmp : String;
  48. begin
  49. tmp := '<log4delphi:event timestamp="'
  50. + IntToStr(DateTimeToFileDate(AEvent.getStartTime))
  51. + '" level="' + AEvent.GetLevel.toString + '">' + LINE_SEP
  52. + ' <log4delphi:message><![CDATA[' + AEvent.GetMessage
  53. + ']]></log4delphi:message>' + LINE_SEP;
  54. if (AEvent.GetException <> Nil) then begin
  55. tmp := tmp + ' <log4delphi:exception class="'
  56. + AEvent.GetException.ClassName + '"><![CDATA['
  57. + AEvent.GetException.Message + ']]></log4delphi:exception>' + LINE_SEP;
  58. end;
  59. tmp := tmp + '</log4delphi:event>' + LINE_SEP;
  60. format := tmp;
  61. end;
  62. {*----------------------------------------------------------------------------
  63. Returns the content type output by this layout, "text/xml".
  64. @return Content type
  65. ----------------------------------------------------------------------------}
  66. function TXMLLayout.GetContentType() : String;
  67. begin
  68. Result := 'text/xml';
  69. end;
  70. {*----------------------------------------------------------------------------
  71. If the layout handles the Exception object contained within LoggingEvent,
  72. then the layout should return false. Otherwise, if the layout ignores
  73. Exception object, then the layout should return true. This layout does not
  74. ignore exceptions.
  75. @return Whether the exception is handled or not
  76. ----------------------------------------------------------------------------}
  77. function TXMLLayout.IgnoresException() : Boolean;
  78. begin
  79. Result := false;
  80. end;
  81. end.