You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
7.7 KiB

14 years ago
14 years ago
13 years ago
14 years ago
14 years ago
14 years ago
14 years ago
13 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
13 years ago
  1. * Overview
  2. Lager (as in the beer) is a logging framework for Erlang. Its purpose is
  3. to provide a more traditional way to perform logging in an erlang application
  4. that plays nicely with traditional UNIX logging tools like logrotate and
  5. syslog.
  6. * Features
  7. - Finer grained log levels (debug, info, notice, warning, error, critical,
  8. alert, emergency)
  9. - Logger calls are transformed using a parse transform to allow capturing
  10. Module/Function/Line/Pid information
  11. - When no handler is consuming a log level (eg. debug) no event is even sent
  12. to the log handler
  13. - Supports multiple backends, including console and file. More are planned.
  14. * Usage
  15. To use lager in your application, you need to define it as a rebar dep or have
  16. some other way of including it in erlang's path. You can then add the
  17. following option to the erlang compiler flags
  18. #+BEGIN_EXAMPLE
  19. {parse_transform, lager_transform}
  20. #+END_EXAMPLE
  21. Alternately, you can add it to the module you wish to compile with logging
  22. enabled:
  23. #+BEGIN_EXAMPLE
  24. -compile([{parse_transform, lager_transform}]).
  25. #+END_EXAMPLE
  26. Once you have built your code with lager, you can then generate log messages
  27. by doing the following:
  28. #+BEGIN_EXAMPLE
  29. lager:error("Some message")
  30. #+END_EXAMPLE
  31. Or:
  32. #+BEGIN_EXAMPLE
  33. lager:warning("Some message with a term: ~p", [Term])
  34. #+END_EXAMPLE
  35. The general form is lager:Severity() where Severity is one of the log levels
  36. mentioned above.
  37. * Configuration
  38. To configure lager's backends, you use an application variable (probably in
  39. your app.config):
  40. #+BEGIN_EXAMPLE
  41. {lager, [
  42. {handlers, [
  43. {lager_console_backend, info},
  44. {lager_file_backend, [
  45. {"error.log", error, 10485760, "$D0", 5},
  46. {"console.log", info, 10485760, "$D0", 5}
  47. ]}
  48. ]}
  49. ]}.
  50. #+END_EXAMPLE
  51. The available configuration options for each backend are listed in their
  52. module's documentation.
  53. * Error logger integration
  54. Lager is also supplied with a error_logger handler module that translates
  55. traditional erlang error messages into a friendlier format and sends them into
  56. lager itself to be treated like a regular lager log call. To disable this, set
  57. the lager application variable `error_logger_redirect' to `false'.
  58. The error_logger handler will also log more complete error messages (protected
  59. with use of trunc_io) to a "crash log" which can be referred to for further
  60. information. The location of the crash log can be specified by the crash_log
  61. application variable. If undefined it is not written at all.
  62. Messages in the crash log are subject to a maximum message size which can be
  63. specified via the crash_log_msg_size application variable.
  64. * Runtime loglevel changes
  65. You can change the log level of any lager backend at runtime by doing the
  66. following:
  67. #+BEGIN_EXAMPLE
  68. lager:set_loglevel(lager_console_backend, debug).
  69. #+END_EXAMPLE
  70. Or, for the backend with multiple handles (files, mainly):
  71. #+BEGIN_EXAMPLE
  72. lager:set_loglevel(lager_file_backend, "console.log", debug).
  73. #+END_EXAMPLE
  74. Lager keeps track of the minium log level being used by any backend and
  75. supresses generation of messages lower than that level. This means that debug
  76. log messages, when no backend is consuming debug messages, are effectively
  77. free. A simple benchmark of doing 1 million debug log messages while the
  78. minimum threshold was above that takes less than half a second.
  79. * Internal log rotation
  80. Lager can rotate its own logs or have it done via an external process. To
  81. use internal rotation, use the last 3 values in the file backend's
  82. configuration tuple. For example
  83. #+BEGIN_EXAMPLE
  84. {"error.log", error, 10485760, "$D0", 5}
  85. #+END_EXAMPLE
  86. This tells lager to log error and above messages to "error.log" and to
  87. rotate the file at midnight or when it reaches 10mb, whichever comes first
  88. and to keep 5 rotated logs, in addition to the current one. Setting the
  89. count to 0 does not disable rotation, it instead rotates the file and keeps
  90. no previous versions around. To disable rotation set the size to 0 and the
  91. date to "".
  92. The "$D0" syntax is taken from the syntax newsyslog uses in newsyslog.conf.
  93. The relevant extract follows:
  94. #+BEGIN_EXAMPLE
  95. Day, week and month time format: The lead-in character
  96. for day, week and month specification is a `$'-sign.
  97. The particular format of day, week and month
  98. specification is: [Dhh], [Ww[Dhh]] and [Mdd[Dhh]],
  99. respectively. Optional time fields default to
  100. midnight. The ranges for day and hour specifications
  101. are:
  102. hh hours, range 0 ... 23
  103. w day of week, range 0 ... 6, 0 = Sunday
  104. dd day of month, range 1 ... 31, or the
  105. letter L or l to specify the last day of
  106. the month.
  107. Some examples:
  108. $D0 rotate every night at midnight
  109. $D23 rotate every day at 23:00 hr
  110. $W0D23 rotate every week on Sunday at 23:00 hr
  111. $W5D16 rotate every week on Friday at 16:00 hr
  112. $M1D0 rotate on the first day of every month at
  113. midnight (i.e., the start of the day)
  114. $M5D6 rotate on every 5th day of the month at
  115. 6:00 hr
  116. #+END_EXAMPLE
  117. To configure the crash log rotation, the following application variables are
  118. used:
  119. - crash_log_size
  120. - crash_log_date
  121. - crash_log_count
  122. See the .app.src file for further details.
  123. * Syslog Support
  124. Lager syslog output is provided as a separate application;
  125. [[https://github.com/basho/lager_syslog][lager_syslog]]. It is packaged as a
  126. separate application so Lager itself doesn't have an indirect dependancy on a
  127. port driver. Please see the lager_syslog README for configuration information.
  128. * AMQP Support
  129. Jon Brisbin has written a lager backend to send lager messages into AMQP, so
  130. you can aggregate logs from a cluster into a central point. You can find it
  131. under the [[https://github.com/jbrisbin/lager_amqp_backend][lager_amqp_backend]]
  132. project on github.
  133. * Tracing
  134. Lager supports basic support for redirecting log messages based on log message
  135. attributes. Lager automatically captures the pid, module, function and line at the
  136. log message callsite. However, you can add any additional attributes you wish:
  137. #+BEGIN_EXAMPLE
  138. lager:warning([{request, RequestID},{vhost, Vhost}], "Permission denied to ~s", [User])
  139. #+END_EXAMPLE
  140. Then, in addition to the default trace attributes, you'll be able to trace
  141. based on request or vhost:
  142. #+BEGIN_EXAMPLE
  143. lager:trace_file("logs/example.com.error", [{vhost, "example.com"}], error)
  144. #+END_EXAMPLE
  145. You can also omit the final argument, and the loglevel will default to
  146. 'debug'.
  147. Tracing to the console is similar:
  148. #+BEGIN_EXAMPLE
  149. lager:trace_console([{request, 117}])
  150. #+END_EXAMPLE
  151. In the above example, the loglevel is omitted, but it can be specified as the
  152. second argument if desired.
  153. You can also specify multiple expressions in a filter, or use the '*' atom as
  154. a wildcard to match any message that has that attribute, regardless of its
  155. value.
  156. Tracing to an existing logfile is also supported, if you wanted to log
  157. warnings from a particular module to the default error.log:
  158. #+BEGIN_EXAMPLE
  159. lager:trace_file("log/error.log", [{module, mymodule}], warning)
  160. #+END_EXAMPLE
  161. To view the active log backends and traces, you can use the lager:status()
  162. function. To clear all active traces, you can use lager:clear_all_traces().
  163. To delete a specific trace, store a handle for the trace when you create it,
  164. that you later pass to lager:stop_trace/1:
  165. #+BEGIN_EXAMPLE
  166. {ok, Trace} = lager:trace_file("log/error.log", [{module, mymodule}]),
  167. ...
  168. lager:stop_trace(Trace)
  169. #+END_EXAMPLE
  170. Tracing to a pid is somewhat of a special case, since a pid is not a
  171. data-type that serializes well. To trace by pid, use the pid as a string:
  172. #+BEGIN_EXAMPLE
  173. lager:trace_console([{pid, "<0.410.0>"}])
  174. #+END_EXAMPLE