【问题标题】:Stackdriver execution_id and custom labelsStackdriver execution_id 和自定义标签
【发布时间】:2018-07-26 05:01:31
【问题描述】:

我正在使用 Google Cloud Functions 和模块 @google-cloud/logging-winston。

我的第一个问题是使用这个模块时的属性

labels: {
  execution_id: "idHere"
}

不在 Stackdriver 日志中,就像我使用常规 console.log(''); 时一样是否可以将该 execution_id 恢复到日志中?

另外,我可以向日志添加自定义标签,例如用户 ID。我试图让我自己的日志记录过程更轻松,这样我就可以通过 execution_id 查看每个 API 端点请求的流程,还可以通过添加用户 ID 标签查看某个用户的所有日志。

我当前的日志记录代码是

'use strict'

import * as winston from 'winston';
const Logger = winston.Logger;
const Console = winston.transports.Console;

import { LoggingWinston } from '@google-cloud/logging-winston'
const loggingWinston = new LoggingWinston();

const logger = new Logger({
    level: 'debug', // log at 'debug' and above
    transports: [
        // Log to the console
        new Console(),
        // And log to Stackdriver Logging
        loggingWinston,
    ],
});

function formatMessage(message) {
    return message;
};

export const error = (message) => {
    logger.error(formatMessage(message));
}
export const warn = (message) => {
    logger.warn(formatMessage(message));
}
export const verbose = (message) => {
    logger.verbose(formatMessage(message));
}
export const info = (message) => {
    logger.info(formatMessage(message));
}
export const debug = (message) => {
    logger.debug(formatMessage(message));
}

【问题讨论】:

  • 你找到这个@Kim的答案了吗?

标签: logging google-cloud-platform google-cloud-functions stackdriver


【解决方案1】:

从 GCP 文档中,您应该能够编写像 these examples 和 Winston specific example 这样的结构化日志行。

看起来像这样

// A json log entry with additional context
const metadata = {
  severity: 'WARNING',
  labels: {
    baz: 'bax',
    'one-fish': 'two-fish',
  },
};

log.info('Hello log line', metadata);

【讨论】:

    猜你喜欢
    • 2017-12-02
    • 2020-09-15
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 2018-05-14
    • 1970-01-01
    • 2018-06-29
    相关资源
    最近更新 更多