Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

1093 linhas
38 KiB

8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
11 anos atrás
9 anos atrás
10 anos atrás
10 anos atrás
10 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
9 anos atrás
8 anos atrás
9 anos atrás
7 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
8 anos atrás
9 anos atrás
9 anos atrás
9 anos atrás
9 anos atrás
9 anos atrás
9 anos atrás
  1. Overview
  2. --------
  3. Lager (as in the beer) is a logging framework for Erlang. Its purpose is
  4. to provide a more traditional way to perform logging in an erlang application
  5. that plays nicely with traditional UNIX logging tools like logrotate and
  6. syslog.
  7. [Travis-CI](http://travis-ci.org/erlang-lager/lager) :: [![Travis-CI](https://travis-ci.org/erlang-lager/lager.svg?branch=master)](http://travis-ci.org/erlang-lager/lager)
  8. Features
  9. --------
  10. * Finer grained log levels (debug, info, notice, warning, error, critical,
  11. alert, emergency)
  12. * Logger calls are transformed using a parse transform to allow capturing
  13. Module/Function/Line/Pid information
  14. * When no handler is consuming a log level (eg. debug) no event is sent
  15. to the log handler
  16. * Supports multiple backends, including console and file.
  17. * Supports multiple sinks
  18. * Rewrites common OTP error messages into more readable messages
  19. * Support for pretty printing records encountered at compile time
  20. * Tolerant in the face of large or many log messages, won't out of memory the node
  21. * Optional feature to bypass log size truncation ("unsafe")
  22. * Supports internal time and date based rotation, as well as external rotation tools
  23. * Syslog style log level comparison flags
  24. * Colored terminal output (requires R16+)
  25. * Map support (requires 17+)
  26. * Optional load shedding by setting a high water mark to kill (and reinstall)
  27. a sink after a configurable cool down timer
  28. Contributing
  29. ------------
  30. We welcome contributions from the community. We are always excited to get ideas
  31. for improving lager.
  32. If you are looking for an idea to help out, please take a look at our open
  33. issues - a number of them are tagged with [Help Wanted](https://github.com/erlang-lager/lager/issues?q=is%3Aopen+is%3Aissue+label%3A%22Help+Wanted%22)
  34. and [Easy](https://github.com/erlang-lager/lager/issues?q=is%3Aopen+is%3Aissue+label%3AEasy) - some
  35. of them are tagged as both! We are happy to mentor people get started with any
  36. of these issues, and they don't need prior discussion.
  37. That being said, before you send large changes please open an issue first to
  38. discuss the change you'd like to make along with an idea of your proposal to
  39. implement that change.
  40. ### PR guidelines ###
  41. * Large changes without prior discussion are likely to be rejected.
  42. * Changes without test cases are likely to be rejected.
  43. * Please use the style of the existing codebase when submitting PRs.
  44. We review PRs and issues at least once a month as described below.
  45. OTP Support Policy
  46. ------------------
  47. The lager maintainers intend to support the past three OTP releases from
  48. current on the main 3.x branch of the project. As of July 2017 that includes
  49. 20, 19, and 18.
  50. Lager may or may not run on older OTP releases but it will only be guaranteed
  51. tested on the previous three OTP releases. If you need a version of lager
  52. which runs on older OTP releases, we recommend you use either the 3.4.0 release
  53. or the 2.x branch.
  54. Monthly triage cadence
  55. ----------------------
  56. We have (at least) monthly issue and PR triage for lager in the #lager room on the
  57. [freenode](https://freenode.net) IRC network every third Thursday at 2 pm US/Pacific,
  58. 10 pm UTC. You are welcome to join us there to ask questions about lager or
  59. participate in the triage.
  60. Usage
  61. -----
  62. To use lager in your application, you need to define it as a rebar dep or have
  63. some other way of including it in Erlang's path. You can then add the
  64. following option to the erlang compiler flags:
  65. ```erlang
  66. {parse_transform, lager_transform}
  67. ```
  68. Alternately, you can add it to the module you wish to compile with logging
  69. enabled:
  70. ```erlang
  71. -compile([{parse_transform, lager_transform}]).
  72. ```
  73. Before logging any messages, you'll need to start the lager application. The
  74. lager module's `start` function takes care of loading and starting any dependencies
  75. lager requires.
  76. ```erlang
  77. lager:start().
  78. ```
  79. You can also start lager on startup with a switch to `erl`:
  80. ```erlang
  81. erl -pa path/to/lager/ebin -s lager
  82. ```
  83. Once you have built your code with lager and started the lager application,
  84. you can then generate log messages by doing the following:
  85. ```erlang
  86. lager:error("Some message")
  87. ```
  88. Or:
  89. ```erlang
  90. lager:warning("Some message with a term: ~p", [Term])
  91. ```
  92. The general form is `lager:Severity()` where `Severity` is one of the log levels
  93. mentioned above.
  94. Configuration
  95. -------------
  96. To configure lager's backends, you use an application variable (probably in
  97. your app.config):
  98. ```erlang
  99. {lager, [
  100. {log_root, "/var/log/hello"},
  101. {handlers, [
  102. {lager_console_backend, [{level, info}]},
  103. {lager_file_backend, [{file, "error.log"}, {level, error}]},
  104. {lager_file_backend, [{file, "console.log"}, {level, info}]}
  105. ]}
  106. ]}.
  107. ```
  108. ```log_root``` variable is optional, by default file paths are relative to CWD.
  109. The available configuration options for each backend are listed in their
  110. module's documentation.
  111. Sinks
  112. -----
  113. Lager has traditionally supported a single sink (implemented as a
  114. `gen_event` manager) named `lager_event` to which all backends were
  115. connected.
  116. Lager now supports extra sinks; each sink can have different
  117. sync/async message thresholds and different backends.
  118. ### Sink configuration
  119. To use multiple sinks (beyond the built-in sink of lager and lager_event), you
  120. need to:
  121. 1. Setup rebar.config
  122. 2. Configure the backends in app.config
  123. #### Names
  124. Each sink has two names: one atom to be used like a module name for
  125. sending messages, and that atom with `_lager_event` appended for backend
  126. configuration.
  127. This reflects the legacy behavior: `lager:info` (or `critical`, or
  128. `debug`, etc) is a way of sending a message to a sink named
  129. `lager_event`. Now developers can invoke `audit:info` or
  130. `myCompanyName:debug` so long as the corresponding `audit_lager_event` or
  131. `myCompanyName_lager_event` sinks are configured.
  132. #### rebar.config
  133. In `rebar.config` for the project that requires lager, include a list
  134. of sink names (without the `_lager_event` suffix) in `erl_opts`:
  135. `{lager_extra_sinks, [audit]}`
  136. #### Runtime requirements
  137. To be useful, sinks must be configured at runtime with backends.
  138. In `app.config` for the project that requires lager, for example,
  139. extend the lager configuration to include an `extra_sinks` tuple with
  140. backends (aka "handlers") and optionally `async_threshold` and
  141. `async_threshold_window` values (see **Overload Protection**
  142. below). If async values are not configured, no overload protection
  143. will be applied on that sink.
  144. ```erlang
  145. [{lager, [
  146. {log_root, "/tmp"},
  147. %% Default handlers for lager/lager_event
  148. {handlers, [
  149. {lager_console_backend, [{level, info}]},
  150. {lager_file_backend, [{file, "error.log"}, {level, error}]},
  151. {lager_file_backend, [{file, "console.log"}, {level, info}]}
  152. ]},
  153. %% Any other sinks
  154. {extra_sinks,
  155. [
  156. {audit_lager_event,
  157. [{handlers,
  158. [{lager_file_backend,
  159. [{file, "sink1.log"},
  160. {level, info}
  161. ]
  162. }]
  163. },
  164. {async_threshold, 500},
  165. {async_threshold_window, 50}]
  166. }]
  167. }
  168. ]
  169. }
  170. ].
  171. ```
  172. Custom Formatting
  173. -----------------
  174. All loggers have a default formatting that can be overriden. A formatter is any module that
  175. exports `format(#lager_log_message{},Config#any())`. It is specified as part of the configuration
  176. for the backend:
  177. ```erlang
  178. {lager, [
  179. {handlers, [
  180. {lager_console_backend, [{level, info}, {formatter, lager_default_formatter},
  181. {formatter_config, [time," [",severity,"] ", message, "\n"]}]},
  182. {lager_file_backend, [{file, "error.log"}, {level, error}, {formatter, lager_default_formatter},
  183. {formatter_config, [date, " ", time," [",severity,"] ",pid, " ", message, "\n"]}]},
  184. {lager_file_backend, [{file, "console.log"}, {level, info}]}
  185. ]}
  186. ]}.
  187. ```
  188. Included is `lager_default_formatter`. This provides a generic, default
  189. formatting for log messages using a structure similar to Erlang's
  190. [iolist](http://learnyousomeerlang.com/buckets-of-sockets#io-lists) which we
  191. call "semi-iolist":
  192. * Any traditional iolist elements in the configuration are printed verbatim.
  193. * Atoms in the configuration are treated as placeholders for lager metadata and
  194. extracted from the log message.
  195. * The placeholders `date`, `time`, `message`, `sev` and `severity` will always exist.
  196. * `sev` is an abbreviated severity which is interpreted as a capitalized
  197. single letter encoding of the severity level (e.g. `'debug'` -> `$D`)
  198. * The placeholders `pid`, `file`, `line`, `module`, `function`, and `node`
  199. will always exist if the parse transform is used.
  200. * The placeholder `application` may exist if the parse transform is used.
  201. It is dependent on finding the applications `app.src` file.
  202. * If the error logger integration is used, the placeholder `pid`
  203. will always exist and the placeholder `name` may exist.
  204. * Applications can define their own metadata placeholder.
  205. * A tuple of `{atom(), semi-iolist()}` allows for a fallback for
  206. the atom placeholder. If the value represented by the atom
  207. cannot be found, the semi-iolist will be interpreted instead.
  208. * A tuple of `{atom(), semi-iolist(), semi-iolist()}` represents a
  209. conditional operator: if a value for the atom placeholder can be
  210. found, the first semi-iolist will be output; otherwise, the
  211. second will be used.
  212. Examples:
  213. ```
  214. ["Foo"] -> "Foo", regardless of message content.
  215. [message] -> The content of the logged message, alone.
  216. [{pid,"Unknown Pid"}] -> "<?.?.?>" if pid is in the metadata, "Unknown Pid" if not.
  217. [{pid, ["My pid is ", pid], ["Unknown Pid"]}] -> if pid is in the metadata print "My pid is <?.?.?>", otherwise print "Unknown Pid"
  218. [{server,{pid, ["(", pid, ")"], ["(Unknown Server)"]}}] -> user provided server metadata, otherwise "(<?.?.?>)", otherwise "(Unknown Server)"
  219. ```
  220. Universal time
  221. --------------
  222. By default, lager formats timestamps as local time for whatever computer
  223. generated the log message.
  224. To make lager use UTC timestamps, you can set the `sasl` application's
  225. `utc_log` configuration parameter to `true` in your application configuration
  226. file.
  227. Example:
  228. ```
  229. %% format log timestamps as UTC
  230. [{sasl, [{utc_log, true}]}].
  231. ```
  232. Error logger integration
  233. ------------------------
  234. Lager is also supplied with a `error_logger` handler module that translates
  235. traditional erlang error messages into a friendlier format and sends them into
  236. lager itself to be treated like a regular lager log call. To disable this, set
  237. the lager application variable `error_logger_redirect` to `false`.
  238. You can also disable reformatting for OTP and Cowboy messages by setting variable
  239. `error_logger_format_raw` to `true`.
  240. The `error_logger` handler will also log more complete error messages (protected
  241. with use of `trunc_io`) to a "crash log" which can be referred to for further
  242. information. The location of the crash log can be specified by the `crash_log`
  243. application variable. If set to `false` it is not written at all.
  244. Messages in the crash log are subject to a maximum message size which can be
  245. specified via the `crash_log_msg_size` application variable.
  246. Messages from `error_logger` will be redirected to `error_logger_lager_event` sink
  247. if it is defined so it can be redirected to another log file.
  248. For example:
  249. ```
  250. [{lager, [
  251. {extra_sinks,
  252. [
  253. {error_logger_lager_event,
  254. [{handlers, [
  255. {lager_file_backend, [{file, "error_logger.log"}, {level, info}]}]
  256. }]
  257. }]
  258. }]
  259. }].
  260. ```
  261. will send all `error_logger` messages to `error_logger.log` file.
  262. Overload Protection
  263. -------------------
  264. ### Asynchronous mode
  265. Prior to lager 2.0, the `gen_event` at the core of lager operated purely in
  266. synchronous mode. Asynchronous mode is faster, but has no protection against
  267. message queue overload. As of lager 2.0, the `gen_event` takes a hybrid
  268. approach. it polls its own mailbox size and toggles the messaging between
  269. synchronous and asynchronous depending on mailbox size.
  270. ```erlang
  271. {async_threshold, 20},
  272. {async_threshold_window, 5}
  273. ```
  274. This will use async messaging until the mailbox exceeds 20 messages, at which
  275. point synchronous messaging will be used, and switch back to asynchronous, when
  276. size reduces to `20 - 5 = 15`.
  277. If you wish to disable this behaviour, simply set `async_threshold` to `undefined`. It defaults
  278. to a low number to prevent the mailbox growing rapidly beyond the limit and causing
  279. problems. In general, lager should process messages as fast as they come in, so getting
  280. 20 behind should be relatively exceptional anyway.
  281. If you want to limit the number of messages per second allowed from `error_logger`,
  282. which is a good idea if you want to weather a flood of messages when lots of
  283. related processes crash, you can set a limit:
  284. ```erlang
  285. {error_logger_hwm, 50}
  286. ```
  287. It is probably best to keep this number small.
  288. ### Sink Killer
  289. In some high volume situations, it may be preferable to drop all pending log
  290. messages instead of letting them drain over time.
  291. If you prefer, you may choose to use the sink killer to shed load. In this
  292. operational mode, if the `gen_event` mailbox exceeds a configurable
  293. high water mark, the sink will be killed and reinstalled after a
  294. configurable cool down time.
  295. You can configure this behavior by using these configuration directives:
  296. ```erlang
  297. {killer_hwm, 1000},
  298. {killer_reinstall_after, 5000}
  299. ```
  300. This means if the sink's mailbox size exceeds 1000 messages, kill the
  301. entire sink and reload it after 5000 milliseconds. This behavior can
  302. also be installed into alternative sinks if desired.
  303. By default, the manager killer *is not installed* into any sink. If
  304. the `killer_reinstall_after` cool down time is not specified it defaults
  305. to 5000.
  306. "Unsafe"
  307. --------
  308. The unsafe code pathway bypasses the normal lager formatting code and uses the
  309. same code as error_logger in OTP. This provides a marginal speedup to your logging
  310. code (we measured between 0.5-1.3% improvement during our benchmarking; others have
  311. reported better improvements.)
  312. This is a **dangerous** feature. It *will not* protect you against
  313. large log messages - large messages can kill your application and even your
  314. Erlang VM dead due to memory exhaustion as large terms are copied over and
  315. over in a failure cascade. We strongly recommend that this code pathway
  316. only be used by log messages with a well bounded upper size of around 500 bytes.
  317. If there's any possibility the log messages could exceed that limit, you should
  318. use the normal lager message formatting code which will provide the appropriate
  319. size limitations and protection against memory exhaustion.
  320. If you want to format an unsafe log message, you may use the severity level (as
  321. usual) followed by `_unsafe`. Here's an example:
  322. ```erlang
  323. lager:info_unsafe("The quick brown ~s jumped over the lazy ~s", ["fox", "dog"]).
  324. ```
  325. Runtime loglevel changes
  326. ------------------------
  327. You can change the log level of any lager backend at runtime by doing the
  328. following:
  329. ```erlang
  330. lager:set_loglevel(lager_console_backend, debug).
  331. ```
  332. Or, for the backend with multiple handles (files, mainly):
  333. ```erlang
  334. lager:set_loglevel(lager_file_backend, "console.log", debug).
  335. ```
  336. Lager keeps track of the minimum log level being used by any backend and
  337. suppresses generation of messages lower than that level. This means that debug
  338. log messages, when no backend is consuming debug messages, are effectively
  339. free. A simple benchmark of doing 1 million debug log messages while the
  340. minimum threshold was above that takes less than half a second.
  341. Syslog style loglevel comparison flags
  342. --------------------------------------
  343. In addition to the regular log level names, you can also do finer grained masking
  344. of what you want to log:
  345. ```
  346. info - info and higher (>= is implicit)
  347. =debug - only the debug level
  348. !=info - everything but the info level
  349. <=notice - notice and below
  350. <warning - anything less than warning
  351. ```
  352. These can be used anywhere a loglevel is supplied, although they need to be either
  353. a quoted atom or a string.
  354. Internal log rotation
  355. ---------------------
  356. Lager can rotate its own logs or have it done via an external process. To
  357. use internal rotation, use the `size`, `date` and `count` values in the file
  358. backend's config:
  359. ```erlang
  360. [{file, "error.log"}, {level, error}, {size, 10485760}, {date, "$D0"}, {count, 5}]
  361. ```
  362. This tells lager to log error and above messages to `error.log` and to
  363. rotate the file at midnight or when it reaches 10mb, whichever comes first,
  364. and to keep 5 rotated logs in addition to the current one. Setting the
  365. count to 0 does not disable rotation, it instead rotates the file and keeps
  366. no previous versions around. To disable rotation set the size to 0 and the
  367. date to "".
  368. The `$D0` syntax is taken from the syntax newsyslog uses in newsyslog.conf.
  369. The relevant extract follows:
  370. ```
  371. Day, week and month time format: The lead-in character
  372. for day, week and month specification is a `$'-sign.
  373. The particular format of day, week and month
  374. specification is: [Dhh], [Ww[Dhh]] and [Mdd[Dhh]],
  375. respectively. Optional time fields default to
  376. midnight. The ranges for day and hour specifications
  377. are:
  378. hh hours, range 0 ... 23
  379. w day of week, range 0 ... 6, 0 = Sunday
  380. dd day of month, range 1 ... 31, or the
  381. letter L or l to specify the last day of
  382. the month.
  383. Some examples:
  384. $D0 rotate every night at midnight
  385. $D23 rotate every day at 23:00 hr
  386. $W0D23 rotate every week on Sunday at 23:00 hr
  387. $W5D16 rotate every week on Friday at 16:00 hr
  388. $M1D0 rotate on the first day of every month at
  389. midnight (i.e., the start of the day)
  390. $M5D6 rotate on every 5th day of the month at
  391. 6:00 hr
  392. ```
  393. To configure the crash log rotation, the following application variables are
  394. used:
  395. * `crash_log_size`
  396. * `crash_log_date`
  397. * `crash_log_count`
  398. See the `.app.src` file for further details.
  399. Syslog Support
  400. --------------
  401. Lager syslog output is provided as a separate application:
  402. [lager_syslog](https://github.com/erlang-lager/lager_syslog). It is packaged as a
  403. separate application so lager itself doesn't have an indirect dependency on a
  404. port driver. Please see the `lager_syslog` README for configuration information.
  405. Older Backends
  406. --------------
  407. Lager 2.0 changed the backend API, there are various 3rd party backends for
  408. lager available, but they may not have been updated to the new API. As they
  409. are updated, links to them can be re-added here.
  410. Exception Pretty Printing
  411. ----------------------
  412. ```erlang
  413. try
  414. foo()
  415. catch
  416. Class:Reason ->
  417. lager:error(
  418. "~nStacktrace:~s",
  419. [lager:pr_stacktrace(erlang:get_stacktrace(), {Class, Reason})])
  420. end.
  421. ```
  422. Record Pretty Printing
  423. ----------------------
  424. Lager's parse transform will keep track of any record definitions it encounters
  425. and store them in the module's attributes. You can then, at runtime, print any
  426. record a module compiled with the lager parse transform knows about by using the
  427. `lager:pr/2` function, which takes the record and the module that knows about the record:
  428. ```erlang
  429. lager:info("My state is ~p", [lager:pr(State, ?MODULE)])
  430. ```
  431. Often, `?MODULE` is sufficent, but you can obviously substitute that for a literal module name.
  432. `lager:pr` also works from the shell.
  433. Colored terminal output
  434. -----------------------
  435. If you have Erlang R16 or higher, you can tell lager's console backend to be colored. Simply
  436. add to lager's application environment config:
  437. ```erlang
  438. {colored, true}
  439. ```
  440. If you don't like the default colors, they are also configurable; see
  441. the `.app.src` file for more details.
  442. The output will be colored from the first occurrence of the atom color
  443. in the formatting configuration. For example:
  444. ```erlang
  445. {lager_console_backend, [{level, info}, {formatter, lager_default_formatter},
  446. {formatter_config, [time, color, " [",severity,"] ", message, "\e[0m\r\n"]}]]}
  447. ```
  448. This will make the entire log message, except time, colored. The
  449. escape sequence before the line break is needed in order to reset the
  450. color after each log message.
  451. Tracing
  452. -------
  453. Lager supports basic support for redirecting log messages based on log message
  454. attributes. Lager automatically captures the pid, module, function and line at the
  455. log message callsite. However, you can add any additional attributes you wish:
  456. ```erlang
  457. lager:warning([{request, RequestID},{vhost, Vhost}], "Permission denied to ~s", [User])
  458. ```
  459. Then, in addition to the default trace attributes, you'll be able to trace
  460. based on request or vhost:
  461. ```erlang
  462. lager:trace_file("logs/example.com.error", [{vhost, "example.com"}], error)
  463. ```
  464. To persist metadata for the life of a process, you can use `lager:md/1` to store metadata
  465. in the process dictionary:
  466. ```erlang
  467. lager:md([{zone, forbidden}])
  468. ```
  469. Note that `lager:md` will *only* accept a list of key/value pairs keyed by atoms.
  470. You can also omit the final argument, and the loglevel will default to
  471. `debug`.
  472. Tracing to the console is similar:
  473. ```erlang
  474. lager:trace_console([{request, 117}])
  475. ```
  476. In the above example, the loglevel is omitted, but it can be specified as the
  477. second argument if desired.
  478. You can also specify multiple expressions in a filter, or use the `*` atom as
  479. a wildcard to match any message that has that attribute, regardless of its
  480. value. You may also use the special value `!` to mean, only select if this
  481. key is **not** present.
  482. Tracing to an existing logfile is also supported (but see **Multiple
  483. sink support** below):
  484. ```erlang
  485. lager:trace_file("log/error.log", [{module, mymodule}, {function, myfunction}], warning)
  486. ```
  487. To view the active log backends and traces, you can use the `lager:status()`
  488. function. To clear all active traces, you can use `lager:clear_all_traces()`.
  489. To delete a specific trace, store a handle for the trace when you create it,
  490. that you later pass to `lager:stop_trace/1`:
  491. ```erlang
  492. {ok, Trace} = lager:trace_file("log/error.log", [{module, mymodule}]),
  493. ...
  494. lager:stop_trace(Trace)
  495. ```
  496. Tracing to a pid is somewhat of a special case, since a pid is not a
  497. data-type that serializes well. To trace by pid, use the pid as a string:
  498. ```erlang
  499. lager:trace_console([{pid, "<0.410.0>"}])
  500. ```
  501. ### Filter expressions
  502. As of lager 3.3.1, you can also use a 3 tuple while tracing where the second
  503. element is a comparison operator. The currently supported comparison operators
  504. are:
  505. * `<` - less than
  506. * `=<` - less than or equal
  507. * `=` - equal to
  508. * `!=` - not equal to
  509. * `>` - greater than
  510. * `>=` - greater than or equal
  511. ```erlang
  512. lager:trace_console([{request, '>', 117}, {request, '<', 120}])
  513. ```
  514. Using `=` is equivalent to the 2-tuple form.
  515. ### Filter composition
  516. As of lager 3.3.1 you may also use the special filter composition keys of
  517. `all` or `any`. For example the filter example above could be
  518. expressed as:
  519. ```erlang
  520. lager:trace_console([{all, [{request, '>', 117}, {request, '<', 120}]}])
  521. ```
  522. `any` has the effect of "OR style" logical evaluation between filters; `all`
  523. means "AND style" logical evaluation between filters. These compositional filters
  524. expect a list of additional filter expressions as their values.
  525. ### Null filters
  526. The `null` filter has a special meaning. A filter of `{null, false}` acts as
  527. a black hole; nothing is passed through. A filter of `{null, true}` means
  528. *everything* passes through. No other values for the null filter are valid and
  529. will be rejected.
  530. ### Multiple sink support
  531. If using multiple sinks, there are limitations on tracing that you
  532. should be aware of.
  533. Traces are specific to a sink, which can be specified via trace
  534. filters:
  535. ```erlang
  536. lager:trace_file("log/security.log", [{sink, audit_event}, {function, myfunction}], warning)
  537. ```
  538. If no sink is thus specified, the default lager sink will be used.
  539. This has two ramifications:
  540. * Traces cannot intercept messages sent to a different sink.
  541. * Tracing to a file already opened via `lager:trace_file` will only be
  542. successful if the same sink is specified.
  543. The former can be ameliorated by opening multiple traces; the latter
  544. can be fixed by rearchitecting lager's file backend, but this has not
  545. been tackled.
  546. ### Traces from configuration
  547. Lager supports starting traces from its configuration file. The keyword
  548. to define them is `traces`, followed by a proplist of tuples that define
  549. a backend handler and zero or more filters in a required list,
  550. followed by an optional message severity level.
  551. An example looks like this:
  552. ```erlang
  553. {lager, [
  554. {handlers, [...]},
  555. {traces, [
  556. %% handler, filter, message level (defaults to debug if not given)
  557. {lager_console_backend, [{module, foo}], info },
  558. {{lager_file_backend, "trace.log"}, [{request, '>', 120}], error},
  559. {{lager_file_backend, "event.log"}, [{module, bar}] } %% implied debug level here
  560. ]}
  561. ]}.
  562. ```
  563. In this example, we have three traces. One using the console backend, and two
  564. using the file backend. If the message severity level is left out, it defaults
  565. to `debug` as in the last file backend example.
  566. The `traces` keyword works on alternative sinks too but the same limitations
  567. and caveats noted above apply.
  568. **IMPORTANT**: You **must** define a severity level in all lager releases
  569. up to and including 3.1.0 or previous. The 2-tuple form wasn't added until
  570. 3.2.0.
  571. Setting dynamic metadata at compile-time
  572. ----------------------------------------
  573. Lager supports supplying metadata from external sources by registering a
  574. callback function. This metadata is also persistent across processes even if
  575. the process dies.
  576. In general use you won't need to use this feature. However it is useful in
  577. situations such as:
  578. * Tracing information provided by
  579. [seq_trace](http://erlang.org/doc/man/seq_trace.html)
  580. * Contextual information about your application
  581. * Persistent information which isn't provided by the default placeholders
  582. * Situations where you would have to set the metadata before every logging call
  583. You can add the callbacks by using the `{lager_parse_transform_functions, X}`
  584. option. It is only available when using `parse_transform`. In rebar, you can
  585. add it to `erl_opts` as below:
  586. ```erlang
  587. {erl_opts, [{parse_transform, lager_transform},
  588. {lager_function_transforms,
  589. [
  590. %% Placeholder Resolve type Callback tuple
  591. {metadata_placeholder, on_emit, {module_name, function_name}},
  592. {other_metadata_placeholder, on_log, {module_name, function_name}}
  593. ]}]}.
  594. ```
  595. The first atom is the placeholder atom used for the substitution in your custom
  596. formatter. See [Custom Formatting](#custom-formatting) for more information.
  597. The second atom is the resolve type. This specify the callback to resolve at
  598. the time of the message being emitted or at the time of the logging call. You
  599. have to specify either the atom `on_emit` or `on_log`. There is not a 'right'
  600. resolve type to use, so please read the uses/caveats of each and pick the option
  601. which fits your requirements best.
  602. `on_emit`:
  603. * The callback functions are not resolved until the message is emitted by the
  604. backend.
  605. * If the callback function cannot be resolved, not loaded or produces
  606. unhandled errors then `undefined` will be returned.
  607. * Since the callback function is dependent on a process, there is the
  608. chance that message will be emitted after the dependent process has died
  609. resulting in `undefined` being returned. This process can also be your own
  610. process
  611. `on_log`:
  612. * The callback functions are resolved regardless whether the message is
  613. emitted or not
  614. * If the callback function cannot be resolved or not loaded the errors are
  615. not handled by lager itself.
  616. * Any potential errors in callback should be handled in the callback function
  617. itself.
  618. * Because the function is resolved at log time there should be less chance
  619. of the dependent process dying before you can resolve it, especially if
  620. you are logging from the app which contains the callback.
  621. The third element is the callback to your function consisting of a tuple in the
  622. form `{Module Function}`. The callback should look like the following
  623. regardless if using `on_emit` or `on_log`:
  624. * It should be exported
  625. * It should takes no arguments e.g. has an arity of 0
  626. * It should return any traditional iolist elements or the atom `undefined`
  627. * For errors generated within your callback see the resolve type documentation
  628. above.
  629. If the callback returns `undefined` then it will follow the same fallback and
  630. conditional operator rules as documented in the
  631. [Custom Formatting](#custom-formatting) section.
  632. This example would work with `on_emit` but could be unsafe to use with
  633. `on_log`. If the call failed in `on_emit` it would default to `undefined`,
  634. however with `on_log` it would error.
  635. ```erlang
  636. -export([my_callback/0]).
  637. my_callback() ->
  638. my_app_serv:call('some options').
  639. ```
  640. This example would be to safe to work with both `on_emit` and `on_log`
  641. ```erlang
  642. -export([my_callback/0]).
  643. my_callback() ->
  644. try my_app_serv:call('some options') of
  645. Result ->
  646. Result
  647. catch
  648. _ ->
  649. %% You could define any traditional iolist elements you wanted here
  650. undefined
  651. end.
  652. ```
  653. Note that the callback can be any Module:Function/0. It does not have be part
  654. of your application. For example you could use `cpu_sup:avg1/0` as your
  655. callback function like so `{cpu_avg1, on_emit, {cpu_sup, avg1}}`
  656. Examples:
  657. ```erlang
  658. -export([reductions/0]).
  659. reductions() ->
  660. proplists:get_value(reductions, erlang:process_info(self())).
  661. ```
  662. ```erlang
  663. -export([seq_trace/0]).
  664. seq_trace() ->
  665. case seq_trace:get_token(label) of
  666. {label, TraceLabel} ->
  667. TraceLabel;
  668. _ ->
  669. undefined
  670. end.
  671. ```
  672. **IMPORTANT**: Since `on_emit` relies on function calls injected at the
  673. point where a log message is emitted, your logging performance (ops/sec)
  674. will be impacted by what the functions you call do and how much latency they
  675. may introduce. This impact will even greater with `on_log` since the calls
  676. are injected at the point a message is logged.
  677. Setting the truncation limit at compile-time
  678. --------------------------------------------
  679. Lager defaults to truncating messages at 4096 bytes, you can alter this by
  680. using the `{lager_truncation_size, X}` option. In rebar, you can add it to
  681. `erl_opts`:
  682. ```erlang
  683. {erl_opts, [{parse_transform, lager_transform}, {lager_truncation_size, 1024}]}.
  684. ```
  685. You can also pass it to `erlc`, if you prefer:
  686. ```
  687. erlc -pa lager/ebin +'{parse_transform, lager_transform}' +'{lager_truncation_size, 1024}' file.erl
  688. ```
  689. Suppress applications and supervisors start/stop logs
  690. -----------------------------------------------------
  691. If you don't want to see supervisors and applications start/stop logs in debug
  692. level of your application, you can use these configs to turn it off:
  693. ```erlang
  694. {lager, [{suppress_application_start_stop, true},
  695. {suppress_supervisor_start_stop, true}]}
  696. ```
  697. Elixir Support
  698. --------------
  699. There are 2 ways in which Lager can be leveraged in an Elixir project:
  700. 1. Lager Backend for Elixir Logger
  701. 2. Directly
  702. ### Lager Backend for Elixir Logger
  703. [Elixir's Logger](https://hexdocs.pm/logger/Logger.html) is the idiomatic way
  704. to add logging into elixir code. Logger has a plug-in model,
  705. allowing for different logging [Backends](https://hexdocs.pm/logger/Logger.html#module-backends)
  706. to be used without the need to change the logging code within your project.
  707. This approach will benefit from the fact that most elixir libs and frameworks
  708. are likely to use the elixir Logger and as such logging will all flow via the
  709. same logging mechanism.
  710. In [elixir 2.0 support for parse transforms will be deprecated](https://github.com/elixir-lang/elixir/issues/5762).
  711. Taking the "Lager as a Logger Backend" approach is likely bypass any related
  712. regression issues that would be introduced into a project which is using lager
  713. directly when updating to elixir 2.0.
  714. There are open source elixir Logger backends for Lager available:
  715. - [LagerLogger](https://github.com/PSPDFKit-labs/lager_logger)
  716. - [LoggerLagerBackend](https://github.com/jonathanperret/logger_lager_backend)
  717. ### Directly
  718. It is fully possible prior to elixir 2.0 to use lager and all its features
  719. directly.
  720. After elixir 2.0 there will be no support for parse transforms, and it would be
  721. recommended to use an elixir wrapper for the lager api that provides compile time
  722. log level exclusion via elixir macros when opting for direct use of lager.
  723. Including Lager as a dependency:
  724. ``` elixir
  725. # mix.exs
  726. def application do
  727. [
  728. applications: [:lager],
  729. erl_opts: [parse_transform: "lager_transform"]
  730. ]
  731. end
  732. defp deps do
  733. [{:lager, "~> 3.2"}]
  734. end
  735. ```
  736. Example Configuration:
  737. ``` elixir
  738. # config.exs
  739. use Mix.Config
  740. # Stop lager writing a crash log
  741. config :lager, :crash_log, false
  742. config :lager,
  743. log_root: '/var/log/hello',
  744. handlers: [
  745. lager_console_backend: :info,
  746. lager_file_backend: [file: "error.log", level: :error],
  747. lager_file_backend: [file: "console.log", level: :info]
  748. ]
  749. ```
  750. There is a known issue where Elixir's Logger and Lager both contest for the
  751. Erlang `error_logger` handle if used side by side.
  752. If using both add the following to your `config.exs`:
  753. ```elixir
  754. # config.exs
  755. use Mix.Config
  756. # Stop lager redirecting :error_logger messages
  757. config :lager, :error_logger_redirect, false
  758. # Stop lager removing Logger's :error_logger handler
  759. config :lager, :error_logger_whitelist, [Logger.ErrorHandler]
  760. ```
  761. Example Usage:
  762. ``` elixir
  763. :lager.error('Some message')
  764. :lager.warning('Some message with a term: ~p', [term])
  765. ```
  766. 3.x Changelog
  767. -------------
  768. 3.5.2 - 19 October 2017
  769. * Bugfix: Properly check for unicode characters in potentially deep
  770. character list. (#417)
  771. 3.5.1 - 15 June 2017
  772. * Doc fix: Missed a curly brace in an example. (#412)
  773. * Feature: Dynamic metadata functions (#392) - It is now possible to
  774. dynamically add metadata to lager messages. See the "dynamic
  775. metadata" section above for more information.
  776. * Doc fix: Add information about the "application" placeholder. (#414)
  777. 3.5.0 - 28 May 2017
  778. * Bugfix: Support OTP 20 gen_event messages (#410)
  779. * Feature: Enable console output to standard_error.
  780. Convert to proplist configuration style (like file handler)
  781. Deprecate previous configuration directives (#409)
  782. * Bugfix: Enable the event shaper to filter messages before they're
  783. counted; do not count application/supervisor start/stops
  784. toward high water mark. (#411)
  785. * Docs: Add PR guidelines; add info about the #lager chat room on freenode.
  786. 3.4.2 - 26 April 2017
  787. * Docs: Document how to make lager use UTC timestamps (#405)
  788. * Docs: Add a note about our triage cadence.
  789. * Docs: Update lager_syslog URL
  790. * Docs: Document placeholders for error_logger integration (#404)
  791. * Feature: Add hex.pm metadata and full rebar3 support.
  792. 3.4.1 - 28 March 2017
  793. * Docs: Added documentation around using lager in the context of elixir applications (#398)
  794. * Bugfix: Properly expand paths when log_root is set. (#386)
  795. * Policy: Removed R15 from Travis configuration
  796. 3.4.0 - 16 March 2017
  797. * Policy: Adopt official OTP support policy. (This is the **last** lager 3.x release
  798. that will support R15.)
  799. * Test: Fix timeouts, R15 missing functions on possibly long-running tests in Travis. (#394, #395)
  800. * Feature: capture and log metadata from error_logger messages (#397)
  801. * Feature: Expose new trace filters and enable filter composition (#389)
  802. * Feature: Log crashes from gen_fsm and gen_statem correctly (#391)
  803. * Docs: Typo in badge URL (#390)
  804. 3.3.0 - 16 February 2017
  805. * Docs: Fix documentation to make 'it' unambiguous when discussing asychronous
  806. operation. (#387)
  807. * Test: Fix test flappiness due to insufficient sanitation between test runs (#384, #385)
  808. * Feature: Allow metadata only logging. (#380)
  809. * Feature: Add an upper case severity formatter (#372)
  810. * Feature: Add support for suppressing start/stop messages from supervisors (#368)
  811. * Bugfix: Fix ranch crash messages (#366)
  812. * Test: Update Travis config for 18.3 and 19.0 (#365)
  813. 3.2.4 - 11 October 2016
  814. * Test: Fix dialyzer warnings.
  815. 3.2.3 - 29 September 2016
  816. * Dependency: Update to goldrush 0.19
  817. 3.2.2 - 22 September 2016
  818. * Bugfix: Backwards-compatibility fix for `{crash_log, undefined}` (#371)
  819. * Fix documentation/README to reflect the preference for using `false`
  820. as the `crash_log` setting value rather than `undefined` to indicate
  821. that the crash log should not be written (#364)
  822. * Bugfix: Backwards-compatibility fix for `lager_file_backend` "legacy"
  823. configuration format (#374)
  824. 3.2.1 - 10 June 2016
  825. * Bugfix: Recent `get_env` changes resulted in launch failure (#355)
  826. * OTP: Support typed records for Erlang 19.0 (#361)
  827. 3.2.0 - 08 April 2016
  828. * Feature: Optional sink killer to shed load when mailbox size exceeds a
  829. configurable high water mark (#346)
  830. * Feature: Export `configure_sink/2` so users may dynamically configure
  831. previously setup and parse transformed sinks from their own code. (#342)
  832. * Feature: Re-enable Travis CI and update .travis.yml (#340)
  833. * Bugfix: Fix test race conditions for Travis CI (#344)
  834. * Bugfix: Add the atom 'none' to the log_level() type so downstream
  835. users won't get dialyzer failures if they use the 'none' log level. (#343)
  836. * Bugfix: Fix typo in documentation. (#341)
  837. * Bugfix: Fix OTP 18 test failures due to `warning_map/0` response
  838. change. (#337)
  839. * Bugfix: Make sure traces that use the file backend work correctly
  840. when specified in lager configuration. (#336)
  841. * Bugfix: Use `lager_app:get_env/3` for R15 compatibility. (#335)
  842. * Bugfix: Make sure lager uses `id` instead of `name` when reporting
  843. supervisor children failures. (The atom changed in OTP in 2014.) (#334)
  844. * Bugfix: Make lager handle improper iolists (#327)
  845. 3.1.0 - 27 January 2016
  846. * Feature: API calls to a rotate handler, sink or all. This change
  847. introduces a new `rotate` message for 3rd party lager backends; that's
  848. why this is released as a new minor version number. (#311)
  849. 3.0.3 - 27 January 2016
  850. * Feature: Pretty printer for human readable stack traces (#298)
  851. * Feature: Make error reformatting optional (#305)
  852. * Feature: Optional and explicit sink for error_logger messages (#303)
  853. * Bugfix: Always explicitly close a file after its been rotated (#316)
  854. * Bugfix: If a relative path already contains the log root, do not add it again (#317)
  855. * Bugfix: Configure and start extra sinks before traces are evaluated (#307)
  856. * Bugfix: Stop and remove traces correctly (#306)
  857. * Bugfix: A byte value of 255 is valid for Unicode (#300)
  858. * Dependency: Bump to goldrush 0.1.8 (#313)