Skip to content

日志记录

¥Logging

logging 包为你提供了一种标准化方式,可记录和显示来自应用的各种控制台消息。额外的好处是,除了其他数据外,它还会向你显示触发日志的位置,这在调试期间很有用,可以快速找到消息的来源。

¥The logging package provides a standardised way for you to log and display in console various message from your application. The added benefit is that among other data it will show you the location where the log was fired, this is useful during debugging to quickly locate where the message is coming from.

首先安装包:

¥Start by installing the package:

bash
meteor add logging

然后,你可以将实用程序导入到代码中的任何位置,如下所示:

¥You can then import the utility anywhere in you code like this:

javascript
import { Log } from 'meteor/logging'

然后,你可以通过以下方式之一调用日志记录函数:

¥You can then call the logging functions in one of the following ways:

javascript
Log('starting up') // or Log.info('starting up')
Log.error('error message')
Log.warn('warning')
Log.debug('this will show only in development')

除了传入字符串,你还可以传入对象。这有一些例外和相关的特殊功能。首先,在对象的根目录中不允许使用以下键:

¥Besides passing in strings, you can also pass in objects. This has few exceptions and special functions associated. First in the root of the object the following keys are not allowed:

javascript
'time', 'timeInexact', 'level', 'file', 'line', 'program', 'originApp', 'satellite', 'stderr'

另一方面,还有 messageapp,它们也是保留的,但它们将以更突出的方式显示:

¥On the other hand there is message and app, which are also reserved, but they will display in more prominent manner:

javascript
Log.info({message: 'warning', app: 'DESKTOP', error: { property1: 'foo', property2: 'bar', property3: { foo: 'bar' }} })

将变为:

¥will turn into:

shell
E20200519-17:57:41.655(9) [DESKTOP] (main.js:36) warning {"error":{"property1":"foo","property2":"bar","property3":{"foo":"bar"}}}

每个日志的显示都采用颜色编码。信息是 blue,警告是 magenta,调试是 green,错误在 red 中。

¥The display of each log is color coded. Info is blue, warn is magenta, debug is green and error is in red.

Log.debug

Log.debug() 日志记录与其他调用不同,因为这些消息不会在生产中显示。

¥The Log.debug() logging is different from the other calls as these messages will not be displayed in production.