Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

253 Zeilen
8.9 KiB

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