您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

224 行
7.8 KiB

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