日志服务
编辑日志服务编辑
允许插件提供状态和诊断信息。
日志服务仅在服务器端可用。
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.