@17media/node-logger 中文文档教程

发布于 4年前 浏览 24 项目主页 更新于 3年前

node-logger CircleCI[npm (scoped)]() Coverage Status

17.Media Node.JS 项目

的集中式记录器……在末日火山的烈火中,黑暗领主索伦秘密打造了一个主记录器来控制所有其他记录器。
“一个记录器将它们全部记录下来!”

Usage

首先你必须设置你的配置:

import { Level } from '@17media/node-logger';

const loggerConfig = {
  // configs shared by all log services
  base: {
    // minimum level to trigger logger [ERROR|WARN|INFO|DEBUG]
    // levels lower than this will not be logged
    // it can be overridden in each service specific config
    logLevel: Level.INFO,

    // project name, preferably 'name' from package.json
    project: require('~/package.json').name,

    // environment [production|stage|development]
    environment: 'production',
  },

  // configs for slack
  Slack: {
    // override minimum log level (optional)
    logLevel: Level.WARN,

    // slack bot access token
    slackToken: SLACK_BOT_TOKEN,

    // slack channel to log messages to
    slackChannel: SLACK_BOT_ALERT_CHANNEL,
  },

  // configs for log collecting service (fluentD)
  Fluentd: {
    // override minimum log level (optional)
    logLevel: Level.INFO,

    // log collector URL
    collectorUrl: LOG_COLLECTOR_URL,
  },

  // configs for logging to console
  Console: {
    // override minimum log level (optional)
    logLevel: Level.ERROR,
  },
};

然后,有两种方法可以继续,简单的方法完整的方法

Easy Way

这是简单的方法,应该涵盖 ~90% 的用例。

const logger = require('@17media/node-logger').createLogger(loggerConfig)('some:label');

logger.debug('track the variable value during development', { info });
logger.info('somehing worth logging for future reference', { additionalInfo });
logger.warn('somehing worth notice', { additionalInfo });
logger.error('somehing terrible happened', new Error());
logger.fatal('somehing disastrous happened', new Error(), { additionalInfo });

Complete Way

提供配置以启动记录器:
只有提供了所有相应的配置,才会使用日志服务。

const { Logger } = require('@17media/node-logger');
const logger = new Logger(loggerConfig);

像这样使用记录器:

const { LogMessage } = require('@17media/node-logger');

logger.Log(
  Level.WARN,
  new LogMessage('something happened', { additionalInfo }),
  'some:label:for:the:message'
);

在大多数情况下,您希望预先标记文件中记录的所有消息。
您可以通过以下方式实现:

const labelledLogger = logger.Label('path:to:this:file');

labelledLogger.Log(
  Level.WARN,
  new LogMessage('something happened', { additionalInfo })
);

您可以扩展 LogMessageErrorMessage 来为您的上下文创建自定义格式。

node-logger CircleCI[npm (scoped)]() Coverage Status

Centralized logger for 17.Media Node.JS projects

… in the fires of Mount Doom, the dark lord Sauron forged, in secret, a master logger to control all others.
"One logger to log them all!"

Usage

First of all you have to set up your configs:

import { Level } from '@17media/node-logger';

const loggerConfig = {
  // configs shared by all log services
  base: {
    // minimum level to trigger logger [ERROR|WARN|INFO|DEBUG]
    // levels lower than this will not be logged
    // it can be overridden in each service specific config
    logLevel: Level.INFO,

    // project name, preferably 'name' from package.json
    project: require('~/package.json').name,

    // environment [production|stage|development]
    environment: 'production',
  },

  // configs for slack
  Slack: {
    // override minimum log level (optional)
    logLevel: Level.WARN,

    // slack bot access token
    slackToken: SLACK_BOT_TOKEN,

    // slack channel to log messages to
    slackChannel: SLACK_BOT_ALERT_CHANNEL,
  },

  // configs for log collecting service (fluentD)
  Fluentd: {
    // override minimum log level (optional)
    logLevel: Level.INFO,

    // log collector URL
    collectorUrl: LOG_COLLECTOR_URL,
  },

  // configs for logging to console
  Console: {
    // override minimum log level (optional)
    logLevel: Level.ERROR,
  },
};

Then, there are two ways to continue, the easy way and the complete way:

Easy Way

This is the simple way and should cover ~90% of the use cases.

const logger = require('@17media/node-logger').createLogger(loggerConfig)('some:label');

logger.debug('track the variable value during development', { info });
logger.info('somehing worth logging for future reference', { additionalInfo });
logger.warn('somehing worth notice', { additionalInfo });
logger.error('somehing terrible happened', new Error());
logger.fatal('somehing disastrous happened', new Error(), { additionalInfo });

Complete Way

Provide configs to initiate the logger:
A log service will be used only when all corresponding configs are provided.

const { Logger } = require('@17media/node-logger');
const logger = new Logger(loggerConfig);

Use the logger like:

const { LogMessage } = require('@17media/node-logger');

logger.Log(
  Level.WARN,
  new LogMessage('something happened', { additionalInfo }),
  'some:label:for:the:message'
);

In most situations you would want to pre-label all the messages logged in a file.
You can do it by:

const labelledLogger = logger.Label('path:to:this:file');

labelledLogger.Log(
  Level.WARN,
  new LogMessage('something happened', { additionalInfo })
);

You can extend LogMessage and ErrorMessage to create customized formatting for your context.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文