我正在尝试在带有 bunyan 记录器的 nodejs 应用程序中使用 stackdriver 记录器,我正在使用它来记录 graphql 请求,并且我遇到了配置问题,我遵循了@google-cloud/logging-bunyan 文档。
记录器的设置如下:
import { createLogger } from 'bunyan';
import { LoggingBunyan } from '@google-cloud/logging-bunyan';
const streams = [{ stream: process.stdout, level: 'info' as 'info' }];
if (process.env.ENVIRONMENT === 'staging' || process.env.ENVIRONMENT === 'production') {
const loggingBunyan = new LoggingBunyan();
streams.push(loggingBunyan.stream('info'));
}
export const logger = createLogger({
name: 'backend-logger',
streams,
});
用法是这样设置的:
const logRequests = async (ctx: ParameterizedContext, next: () => Promise<any>) => {
const auth = await authMiddleware(ctx.request);
ctx.state.auth = auth;
const start = Date.now();
await next();
const duration = Date.now() - start;
if (ctx.method === 'POST' && ctx.path === process.env.CLIENT_SERVER_ENDPOINT) {
const { query } = ctx.request.body;
const logData = {
query,
OrganizationId: auth.user?.OrganizationId,
UserId: auth.user?.id,
UserName: auth.user?.name,
duration,
};
if (duration > 200) {
StackLogger.warn(logData);
} else {
StackLogger.info(logData);
}
}
};
记录器仅在登台时设置,问题是,我只得到 nginx-access 日志(之前,我不会得到),但没有得到我传递给记录的内容当我使用 PM2 运行时,我看到了错误应该记录
1|api | {"name":"backend-logger","hostname":"backend","pid":6405,"level":30,"msg":"{ logData:\n { query:\n 'mutation LoginMutation(\\n $email: String!\\n $password: String!\\n) {\\n login(Email: $email, Password: $password) {\\n Token\\n }\\n}\\n',\n OrganizationId: undefined,\n UserId: undefined,\n UserName: undefined,\n duration: 38 } }","time":"2020-01-09T19:17:45.428Z","v":0}
1|api | --> POST /graphql 200 48ms 1.71kb
1|api | You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:
1|api | Error: 7 PERMISSION_DENIED: The caller does not have permission
1|api | at Object.callErrorFromStatus (/home/api/sl_api/node_modules/@grpc/grpc-js/src/call.ts:79:24)
1|api | at Http2CallStream.call.on (/home/api/sl_api/node_modules/@grpc/grpc-js/src/client.ts:155:18)
1|api | at Http2CallStream.emit (events.js:203:15)
1|api | at Http2CallStream.EventEmitter.emit (domain.js:448:20)
1|api | at process.nextTick (/home/api/sl_api/node_modules/@grpc/grpc-js/src/call-stream.ts:183:14)
1|api | at process._tickCallback (internal/process/next_tick.js:61:11)
它在 gcloud VM 实例中,所以我想我有权登录到 stackdriver 所以我设置了一个服务帐户,即使这样,日志也不会出现在 stackdriver 中,并且在 stackdrive 中出现相同的错误消息。
有人对如何使这项工作有任何想法吗?谢谢