@24hr/sentry-logger-node 中文文档教程

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

Sentry logger node

这个包包括用于 Express 和 Koa 的哨兵中间件,并且还导出一个记录器功能。 这反映了 Winston 日志功能的工作方式,因此替换将尽可能简单。

How to install

安装包:

npm install @24hr/sentry-logger-node

在您的应用程序中初始化 Sentry:

const sentryLogger = require('@24hr/sentry-logger-node');

sentryLogger.init({ dsn: SENTRY_DSN, release: SENTRY_RELEASE, environment: SENTRY_ENVIRONMENT, serviceName: 'my-service' });

// If you dont want it to send to Sentry in development:
if (process.env.NODE_ENV === 'production') {
    sentryLogger.init({ dsn: SENTRY_DSN, release: SENTRY_RELEASE, environment: SENTRY_ENVIRONMENT, serviceName: 'my-service' });
}

Setup with Express

该包导出一个 Express 中间件。 要使用它,请务必尽快使用:

app.use(sentryLogger.express.middleware());

Setup with Koa

app.use(sentryLogger.koa.middleware());

Middleware options

中间件函数采用一个选项对象,您可以在其中传递标签和 requestId。 这对 Koa 和 Express 都是一样的。

Tags

添加更多标签:

const extraTags = {
    tagKey: 'tagValue';
}
app.use(sentryLogger.koa.middleware({ tags: extraTags }));

Request ID

请求 ID 只是另一个标签,但它是请求的唯一标识符。 如果您有一个必须通过多个微服务的请求,它会非常有用。

默认情况下,中间件会将请求 ID 设置为 x-request-id 标头的值,但如果这不适合您,您可以将 requestId 作为选项传递。

app.use(sentryLogger.koa.middleware({ requestId: 'unique id' }));

Get request Id from other source

如果您没有 x-request-id 标头,您可以创建自定义中间件来检索它。

对于 Koa:

app.use((ctx, next) => {
    const myRequestId = ctx.headers['x-my-request-id'];
    return sentryLogger.koa.middleware({ requestId: myRequestId })(ctx, next)
});

对于 Express:

app.use((req, res, next) => {
    const myRequestId = ctx.headers['x-my-request-id'];
    return sentryLogger.koa.middleware({ requestId: myRequestId })(req, res, next)
});

Logging

这个包导出一个具有不同日志级别的记录器对象。 当前的级别是:详细、信息、警告、错误、致命和严重。

发送到 Sentry 的是错误的、致命的和严重的。

我计划添加一个 LOG_LEVEL env 变量功能来排除不需要的日志,例如冗长的日志。 现在,verbose 和 info 是相同的,但存在是为了向后兼容。

How to use (examples)

const { logger } = require('@24hr/sentry-logger-node');

logger.info('Info text'); // Green color in console
logger.warning('Warning text'); // Yellow color in console
logger.error('Error', new Error('An error occcured!')); // Red color in console and sent to Sentry.

Sentry logger node

This package includes Sentry middleware for Express and Koa and also exports a logger function. This mirrors how Winston logging functions work so replacing would be as simple as possible.

How to install

Install the package:

npm install @24hr/sentry-logger-node

Initialize Sentry in your app:

const sentryLogger = require('@24hr/sentry-logger-node');

sentryLogger.init({ dsn: SENTRY_DSN, release: SENTRY_RELEASE, environment: SENTRY_ENVIRONMENT, serviceName: 'my-service' });

// If you dont want it to send to Sentry in development:
if (process.env.NODE_ENV === 'production') {
    sentryLogger.init({ dsn: SENTRY_DSN, release: SENTRY_RELEASE, environment: SENTRY_ENVIRONMENT, serviceName: 'my-service' });
}

Setup with Express

The package exports an Express middleware. To use it, be sure to use as soon as possible:

app.use(sentryLogger.express.middleware());

Setup with Koa

app.use(sentryLogger.koa.middleware());

Middleware options

The middleware function takes an option object where you can pass tags and requestId. This works the same for both Koa and Express.

Tags

To add more tags:

const extraTags = {
    tagKey: 'tagValue';
}
app.use(sentryLogger.koa.middleware({ tags: extraTags }));

Request ID

A Request ID is just a another tag but is a unique identifier for the request. It's super useful if you have a request that has to pass by multiple microservices.

By default the middleware will set the request ID to the value of the x-request-id header, but if that doesn't suit you, you can pass the requestId as an option.

app.use(sentryLogger.koa.middleware({ requestId: 'unique id' }));

Get request Id from other source

If you don't have a x-request-id header you can create a custom middleware to retrieve it.

For Koa:

app.use((ctx, next) => {
    const myRequestId = ctx.headers['x-my-request-id'];
    return sentryLogger.koa.middleware({ requestId: myRequestId })(ctx, next)
});

For Express:

app.use((req, res, next) => {
    const myRequestId = ctx.headers['x-my-request-id'];
    return sentryLogger.koa.middleware({ requestId: myRequestId })(req, res, next)
});

Logging

This package exports a logger object with different logging levels. The current levels are: verbose, info, warning, error, fatal and critical.

The ones that gets sent to Sentry are error, fatal and critical.

I plan to add a LOG_LEVEL env variable feature to exclude unwanted logs, e.g verbose. Right now, verbose and info are the same, but exists for backwards compatibility.

How to use (examples)

const { logger } = require('@24hr/sentry-logger-node');

logger.info('Info text'); // Green color in console
logger.warning('Warning text'); // Yellow color in console
logger.error('Error', new Error('An error occcured!')); // Red color in console and sent to Sentry.
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文