这是我的 pino 配置
import { config } from 'dotenv';
import { Params } from 'nestjs-pino';
config();
const pinoConfig: Params = {
pinoHttp:
{
level: process.env.NODE_ENV !== 'production' ? 'debug' : 'info',
formatters: {
level: (label, number) => {
return { level: label };
},
},
customReceivedMessage: function(req, res) {
return `request ${req.id} received: ${req.method} ${req.url}`;
},
customSuccessMessage: function(req, res) {
return `${req.method} ${req.url} completed`;
},
customErrorMessage: function(req, res, err) {
return `request ${req.id} to ${req.method} ${req.url} errored with status code: ${res.statusCode} message: ${err.message}}`;
},
transport: process.env.NODE_ENV !== 'production'
?
{
target: 'pino-pretty',
options: {
singleLine: true,
ignore: 'pid,hostname,req.headers,req.remotePort,req.remoteAddress,req,res',
}
}
: undefined
,
}
};
export default pinoConfig;
在我的 main.ts 文件中
import { NestFactory, Reflector } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerModule } from '@nestjs/swagger';
import { Logger, LoggerErrorInterceptor } from 'nestjs-pino';
async function bootstrap() {
const PORT = process.env.PORT || 3001;
const HOST = process.env.HOST || 'localhost';
const PROTOCOL = process.env.PROTOCOL || 'http';
const app = await NestFactory.create(AppModule, { cors: true });
app.useLogger(app.get(Logger));
app.useGlobalInterceptors(new LoggerErrorInterceptor());
...
await app.listen(PORT);
const serverUrl = `${PROTOCOL}://${HOST}:${PORT}`;
...
}
bootstrap();
pino 日志在本地工作正常。但是,我还想将这些日志发送到 azure 中的应用程序洞察。我该怎么做呢? 我已经创建了应用程序洞察资源,并且知道我的仪器和连接密钥。
applicationinsights
npm 包 - npm install applicationinsights
。
main.ts
文件中设置 Application Insights:import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { TelemetryClient } from 'applicationinsights';
import { setupInstrumentation } from 'applicationinsights/NestJs';
async function bootstrap() {
const PORT = process.env.PORT || 3001;
const HOST = process.env.HOST || 'localhost';
const PROTOCOL = process.env.PROTOCOL || 'http';
// Initialize Application Insights
const instrumentationKey = 'YOUR_INSTRUMENTATION_KEY'; // Replace with your key
const telemetryClient = new TelemetryClient(instrumentationKey);
setupInstrumentation(telemetryClient);
const app = await NestFactory.create(AppModule, { cors: true });
// Start Application Insights telemetry
telemetryClient.trackNodeHttpDependency();
await app.listen(PORT);
const serverUrl = `${PROTOCOL}://${HOST}:${PORT}`;
console.log(`Application is running on: ${serverUrl}`);
}
bootstrap();
pinoConfig.ts
文件以包含 Azure Application Insights 传输。pinoConfig.ts:
import { config } from 'dotenv';
import { Params } from 'nestjs-pino';
import { TelemetryClient } from 'applicationinsights';
config();
// Initialize the Application Insights client
const appInsights = new TelemetryClient(process.env.APP_INSIGHTS_INSTRUMENTATION_KEY);
const pinoConfig: Params = {
pinoHttp: {
level: process.env.NODE_ENV !== 'production' ? 'debug' : 'info',
formatters: {
level: (label, number) => {
return { level: label };
},
},
customReceivedMessage: function (req, res) {
return `request ${req.id} received: ${req.method} ${req.url}`;
},
customSuccessMessage: function (req, res) {
return `${req.method} ${req.url} completed`;
},
customErrorMessage: function (req, res, err) {
return `request ${req.id} to ${req.method} ${req.url} errored with status code: ${res.statusCode} message: ${err.message}}`;
},
transport: [
{
target: 'pino-pretty',
options: {
singleLine: true,
ignore: 'pid,hostname,req.headers,req.remotePort,req.remoteAddress,req,res',
},
},
// Add Azure Application Insights transport
{
target: 'pino-azure-appinsights',
options: {
client: appInsights,
},
},
],
},
};
export default pinoConfig;
您可以在
portal>application insights>overview
中找到仪器密钥。
在应用程序洞察>调查>交易搜索中检查日志。