1

我正在尝试在带有 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 中出现相同的错误消息。

有人对如何使这项工作有任何想法吗?谢谢

4

1 回答 1

2

看到错误Error: 7 PERMISSION_DENIED: The caller does not have permission,您似乎没有使用所需的服务帐户来调用 api。根据github页面,先决条件是

  1. 选择或创建一个 Cloud Platform 项目。
  2. 选择或创建一个 Cloud Platform 项目。
  3. 启用 Stackdriver Logging API。
  4. 使用服务帐户设置身份验证,以便您可以从本地工作站访问 API。

您还提到您已启用服务帐户,我假设您已按照访问控制指南提供了正确的访问权限。

因此,我想查看显式身份验证设置,以确保您正在为正确的项目和正确的服务帐户调用 API。

注意:我将其发布为答案,因为我目前的声誉不支持我对您的帖子发表评论。如果我错过了什么或者它没有任何意义,请随时告诉我。我会相应地编辑它。

于 2020-01-10T16:06:50.643 回答