THTMLLayoutUnit.pas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 THTMLLayoutUnit;
  19. {$ifdef fpc}
  20. {$mode objfpc}
  21. {$h+}
  22. {$endif}
  23. interface
  24. uses
  25. TLayoutUnit, TLoggingEventUnit;
  26. type
  27. {*----------------------------------------------------------------------------
  28. Formats logging statements into an HTML document structuring it with
  29. a table. The resulting HTML is 4.01 compliant and should have no
  30. trouble rendering in any browser.
  31. ----------------------------------------------------------------------------}
  32. THTMLLayout = class (TLayout)
  33. private
  34. FTitle : String;
  35. public
  36. constructor Create;
  37. procedure SetTitle(ATitle : String);
  38. function GetTitle() : String;
  39. function Format(AEvent : TLoggingEvent) : String; Override;
  40. function GetContentType() : String; Override;
  41. function GetHeader() : String; Override;
  42. function GetFooter() : String; Override;
  43. function IgnoresException() : Boolean; Override;
  44. end;
  45. implementation
  46. uses
  47. SysUtils, TLogLogUnit;
  48. {*----------------------------------------------------------------------------
  49. Create a new instance.
  50. ----------------------------------------------------------------------------}
  51. constructor THTMLLayout.Create;
  52. begin
  53. inherited Create;
  54. Self.FTitle := 'Log4Delphi Log Messages';
  55. TLogLog.Debug('THTMLLayout.Create');
  56. end;
  57. {*----------------------------------------------------------------------------
  58. Set the title of the HTML document.
  59. @param title The title of the document
  60. ----------------------------------------------------------------------------}
  61. procedure THTMLLayout.SetTitle(ATitle : String);
  62. begin
  63. Self.FTitle := ATitle;
  64. TLogLog.Debug('THTMLLayout.SetTitle: ' + ATitle);
  65. end;
  66. {*----------------------------------------------------------------------------
  67. Returns the title of the HTML document.
  68. @return The title of the document
  69. ----------------------------------------------------------------------------}
  70. function THTMLLayout.GetTitle() : String;
  71. begin
  72. Result := Self.FTitle;
  73. end;
  74. {*----------------------------------------------------------------------------
  75. Returns the log statement in HTML format.
  76. @return The event formatted as an HTML string
  77. ----------------------------------------------------------------------------}
  78. function THTMLLayout.Format(AEvent : TLoggingEvent) : String;
  79. var
  80. excptn : String;
  81. begin
  82. if (AEvent.GetException <> Nil) then
  83. excptn := '; Exception:' + AEvent.GetException.Message
  84. else
  85. excptn := '';
  86. format :=
  87. ' <tr>' + LINE_SEP
  88. + ' <td title="Timestamp">'
  89. + IntToStr(DateTimeToFileDate(AEvent.GetStartTime)) + '</td>' + LINE_SEP
  90. + ' <td title="Level" class="' + AEvent.GetLevel.toString
  91. + '">' + AEvent.GetLevel.toString + '</td>' + LINE_SEP
  92. + ' <td title="Message">' + AEvent.GetMessage + excptn
  93. + '</td>' + LINE_SEP
  94. + ' </tr>' + LINE_SEP
  95. end;
  96. {*----------------------------------------------------------------------------
  97. Returns the content type output by this layout, "text/html".
  98. @return Content type
  99. ----------------------------------------------------------------------------}
  100. function THTMLLayout.GetContentType() : String;
  101. begin
  102. Result := 'text/html';
  103. end;
  104. {*----------------------------------------------------------------------------
  105. Returns the HTML header for the layout format.
  106. @return HTML Header
  107. ----------------------------------------------------------------------------}
  108. function THTMLLayout.GetHeader() : String;
  109. begin
  110. // NOTE: This method disobeys formatting by exceeding the 80 character right
  111. // margin in an attempt to make the html that it returns easier to read and
  112. // understand.
  113. getHeader :=
  114. '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"' + LINE_SEP
  115. + ' "http://www.w3.org/TR/html4/strict.dtd">' + LINE_SEP
  116. + '<html>' + LINE_SEP
  117. + ' <head>' + LINE_SEP
  118. + ' <title>Log4Delphi Log Messages</title>' + LINE_SEP
  119. + ' <style type="text/css">' + LINE_SEP
  120. + ' <!--' + LINE_SEP
  121. + ' body {background: XFFFFFF; margin: 6px; font-family: arial,sans-serif; font-size: small;}' + LINE_SEP
  122. + ' table {font-family: arial,sans-serif; font-size: 9pt;}' + LINE_SEP
  123. + ' th {background: #336699; color: #fdfdfd; text-align: left;}' + LINE_SEP
  124. + ' td.debug {color: #339933}' + LINE_SEP
  125. + ' td.warn {color: #FF9229}' + LINE_SEP
  126. + ' td.error {color: #CC0000}' + LINE_SEP
  127. + ' td.fatal {color: #FF0000}' + LINE_SEP
  128. + ' -->' + LINE_SEP
  129. + ' </style>' + LINE_SEP
  130. + ' </head>' + LINE_SEP
  131. + ' <body>' + LINE_SEP
  132. + ' <p>Log session start time Tue Sep 13 16:19:28 SAST 2005</p>' + LINE_SEP
  133. + ' <table cellspacing="0" cellpadding="4" width="100%">' + LINE_SEP
  134. + ' <thead>' + LINE_SEP
  135. + ' <tr>' + LINE_SEP
  136. + ' <th>Time</th>' + LINE_SEP
  137. + ' <th>Level</th>' + LINE_SEP
  138. + ' <th>Message</th>' + LINE_SEP
  139. + ' </tr>' + LINE_SEP
  140. + ' </thead>' + LINE_SEP
  141. + ' <tbody>' + LINE_SEP;
  142. end;
  143. {*----------------------------------------------------------------------------
  144. Returns the footer for the html layout format.
  145. @return HTML Footer
  146. ----------------------------------------------------------------------------}
  147. function THTMLLayout.GetFooter() : String;
  148. begin
  149. getFooter :=
  150. ' </tbody>' + LINE_SEP
  151. + ' </table>' + LINE_SEP
  152. + ' </body>' + LINE_SEP
  153. + '</html>' + LINE_SEP
  154. end;
  155. {*----------------------------------------------------------------------------
  156. If the layout handles the Exception object contained within LoggingEvent,
  157. then the layout should return false. Otherwise, if the layout ignores
  158. Exception object, then the layout should return true. This layout handles
  159. exceptions.
  160. @return Whether the exception is handled or not
  161. ----------------------------------------------------------------------------}
  162. function THTMLLayout.IgnoresException() : Boolean;
  163. begin
  164. Result := false;
  165. end;
  166. end.