日志服务
编辑日志服务
编辑允许插件提供状态和诊断信息。
日志服务仅在服务器端可用。
import type { PluginInitializerContext, CoreSetup, Plugin, Logger } from '@kbn/core/server'; export class MyPlugin implements Plugin { private readonly logger: Logger; constructor(initializerContext: PluginInitializerContext) { this.logger = initializerContext.logger.get(); } public setup(core: CoreSetup) { try { this.logger.debug('doing something...'); // … } catch (e) { this.logger.error('failed doing something...'); } } }
用法
编辑用法非常直接,只需获取特定上下文的日志记录器,并使用它以不同的日志级别记录消息即可。
const logger = kibana.logger.get('server'); logger.trace('Message with `trace` log level.'); logger.debug('Message with `debug` log level.'); logger.info('Message with `info` log level.'); logger.warn('Message with `warn` log level.'); logger.error('Message with `error` log level.'); logger.fatal('Message with `fatal` log level.'); const loggerWithNestedContext = kibana.logger.get('server', 'http'); loggerWithNestedContext.trace('Message with `trace` log level.'); loggerWithNestedContext.debug('Message with `debug` log level.');
假设使用了名称为 server
的日志记录器,带有 console
输出器和 trace
级别,控制台输出将如下所示
[2017-07-25T11:54:41.639-07:00][TRACE][server] Message with `trace` log level. [2017-07-25T11:54:41.639-07:00][DEBUG][server] Message with `debug` log level. [2017-07-25T11:54:41.639-07:00][INFO ][server] Message with `info` log level. [2017-07-25T11:54:41.639-07:00][WARN ][server] Message with `warn` log level. [2017-07-25T11:54:41.639-07:00][ERROR][server] Message with `error` log level. [2017-07-25T11:54:41.639-07:00][FATAL][server] Message with `fatal` log level. [2017-07-25T11:54:41.639-07:00][TRACE][server.http] Message with `trace` log level. [2017-07-25T11:54:41.639-07:00][DEBUG][server.http] Message with `debug` log level.
对于 server
日志记录器,使用 warn
级别时,日志将不那么详细
[2017-07-25T11:54:41.639-07:00][WARN ][server] Message with `warn` log level. [2017-07-25T11:54:41.639-07:00][ERROR][server] Message with `error` log level. [2017-07-25T11:54:41.639-07:00][FATAL][server] Message with `fatal` log level.