zed*_*ian 5 amazon-web-services node.js winston amazon-cloudwatch nestjs
到目前为止,我读过的所有文章和文档都讨论了 Cloudwatch 和 Winston 在普通 Node 应用程序上的集成,但没有讨论 Nestjs
到目前为止,我的app.module.ts上有:
imports: [
ConfigModule.forRoot({ isGlobal: true }),
MongooseModule.forRoot(
`mongodb://${environment.MONGO_INITDB_ROOT_USERNAME}:${environment.MONGO_INITDB_ROOT_PASSWORD}@${environment.MONGODB_HOST}/${environment.MONGO_INITDB_DATABASE}`,
),
VoucherModule,
ApiKeyModule,
WinstonModule.forRoot(loggerConfig),
],
Run Code Online (Sandbox Code Playgroud)
其中 loggerConfig 是基本的 Winston 配置,具体取决于环境。
使用winston-cloudwatch包,我需要创建一个新的 Transporter 并将其添加到 Winston,但似乎找不到方法来执行此操作。
Ras*_*han 13
我最近在 Nestjs 中实现了 aws-cloudwatch 并遇到了类似的问题,但在浏览和阅读了有关 winston 和 cloudwatch 的内容后,提出了这个解决方案。
//main.ts
import {
utilities as nestWinstonModuleUtilities,
WinstonModule,
} from 'nest-winston';
import * as winston from 'winston';
import CloudWatchTransport from 'winston-cloudwatch';
const app = await NestFactory.create(AppModule, {
logger: WinstonModule.createLogger({
format: winston.format.uncolorize(), //Uncolorize logs as weird character encoding appears when logs are colorized in cloudwatch.
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.ms(),
nestWinstonModuleUtilities.format.nestLike()
),
}),
new CloudWatchTransport({
name: "Cloudwatch Logs",
logGroupName: process.env.CLOUDWATCH_GROUP_NAME,
logStreamName: process.env.CLOUDWATCH_STREAM_NAME,
awsAccessKeyId: process.env.AWS_ACCESS_KEY,
awsSecretKey: process.env.AWS_KEY_SECRET,
awsRegion: process.env.CLOUDWATCH_AWS_REGION,
messageFormatter: function (item) {
return (
item.level + ": " + item.message + " " + JSON.stringify(item.meta)
);
},
}),
],
}),
});
Run Code Online (Sandbox Code Playgroud)
在这里,我们定义了两种传输,一种是transports.Console(),它是默认的winston传输,另一种是cloudwatch传输。
这基本上做的是,替换默认的nestjs Logger(从@nestjs/common导入)。因此,我们不必在任何模块中导入 Winston,甚至不需要在“app.module.ts”中导入。相反,只需将 Logger 导入您需要的任何模块中,每当我们记录任何内容时,它都会显示在终端中并将其上传到 cloudwatch。
编辑: 使用 configService..
//main.js
import {
utilities as nestWinstonModuleUtilities,
WinstonModule,
} from 'nest-winston';
import * as winston from 'winston';
import CloudWatchTransport from 'winston-cloudwatch';
import { ConfigService } from '@nestjs/config';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);
app.useLogger(
WinstonModule.createLogger({
format: winston.format.uncolorize(),
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.ms(),
nestWinstonModuleUtilities.format.nestLike(),
),
}),
new CloudWatchTransport({
name: 'Cloudwatch Logs',
logGroupName: configService.get('CLOUDWATCH_GROUP_NAME'),
logStreamName: configService.get('CLOUDWATCH_STREAM_NAME'),
awsAccessKeyId: configService.get('AWS_ACCESS_KEY'),
awsSecretKey: configService.get('AWS_KEY_SECRET'),
awsRegion: configService.get('CLOUDWATCH_AWS_REGION'),
messageFormatter: function (item) {
return (
item.level + ': ' + item.message + ' ' + JSON.stringify(item.meta)
);
},
}),
],
}),
);
await app.listen(configService.get('PORT') || 3000);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7659 次 |
| 最近记录: |