来自云功能的结构化日志

来自云功能的结构化日志

所以我已经为此苦苦思索了一段时间,想在这里问一下。

是否可以在云函数中使用结构化日志记录?

CF 中没有日志和指标代理,我希望我们的 StackDriver 日志中获得一些更丰富的数据(主要是为了利用一些基于日志的指标)。

是否有人可以指点一下,如果没有使用日志代理的额外机器,那么可以做什么?

答案1

我发布了这个社区维基答案,以做出以下建议:@安德鲁·海恩斯更加明显。这个问题得到了回答这里。请参阅以下解决方案摘要:

// global
const { Logging } = require("@google-cloud/logging");
const logging = new Logging();
const Log = logging.log("cloudfunctions.googleapis.com%2Fcloud-functions");
const LogMetadata = {
  severity: "INFO",
  type: "cloud_function",
  labels: {
  function_name: process.env.FUNCTION_TARGET,
  project: process.env.GCP_PROJECT,
  region: JSON.parse(process.env.FIREBASE_CONFIG).locationId
}
};
 
// per request
const data = { FOO: "BAR" };
const traceId = req.get("x-cloud-trace-context").split("/")[0];
const metadata = {
  ...LogMetadata,
  severity: 'INFO',
  trace: `projects/${process.env.GCLOUD_PROJECT}/traces/${traceId}`,
  labels: {
    execution_id: req.get("function-execution-id")
  }
};
Log.write(Log.entry(metadata, data));

severity可以找到的值这里

DEFAULT (0) The log entry has no assigned severity level.
DEBUG   (100) Debug or trace information.
INFO    (200) Routine information, such as ongoing status or performance.
NOTICE  (300) Normal but significant events, such as start up, shut down, or a configuration change.
WARNING (400) Warning events might cause problems.
ERROR   (500) Error events are likely to cause problems.
CRITICAL    (600) Critical events cause more severe problems or outages.
ALERT   (700) A person must take an action immediately.
EMERGENCY   (800) One or more systems are unusable.

相关内容