HTTP Log使用
1.导入
import { HttpLoggerMiddleware } from '@treelab/logger-metrics-middleware'
import * as Prometheus from 'prom-client'
2. 使用中间件
// 日志
app.use(HttpLoggerMiddleware())
// 指标
app.use('/metrics', async (req, res) => {
res.set('Content-Type', Prometheus.register.contentType)
const end = await Prometheus.register.metrics()
res.end(end)
})
}
3.HttpLog中间件配置说明
{
prettyPrint?: boolean // 是否以json方式输出, 默认false
... // 后续新增..
}
完整例子
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { HttpLoggerMiddleware } from '@treelab/logger-metrics-middleware'
import * as Prometheus from 'prom-client'
import bodyParser = require('body-parser');
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.use(bodyParser.json());
app.use(HttpLoggerMiddleware({ prettyPrint: false}))
app.use('/metrics', async (req, res) => {
res.set('Content-Type', Prometheus.register.contentType)
const end = await Prometheus.register.metrics()
res.end(end)
})
await app.listen(3000);
}
bootstrap();
Grpc Log 使用
导入
import { GrpcLoggerMiddleware } from '@treelab/logger-metrics-middleware';
使用
// 在grpc Controller 上使用
@UseInterceptors(GrpcLoggerMiddleware)
@Controller()
export class GrpcController {
constructor(){}
}