在Firebase Cloud Functions中记录'jsonPayload' [英] Log 'jsonPayload' in Firebase Cloud Functions

查看:55
本文介绍了在Firebase Cloud Functions中记录'jsonPayload'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR;

有人知道是否有可能在Firebase/Google Cloud Function中使用jsonPayload属性使用console.log将条目记录到Stack Driver中,以便可以搜索我的日志(目前,我传递给console.log的所有内容都被字符串化为textPayload).

Does anyone know if it's possible to use console.log in a Firebase/Google Cloud Function to log entries to Stack Driver using the jsonPayload property so my logs are searchable (currently anything I pass to console.log gets stringified into textPayload).

我有一个多模块项目,其中一些代码在Firebase Cloud Functions上运行,而另一些代码在其他环境(例如Google Compute Engine)上运行.稍微简化一下,我本质上有一个核心"模块,然后将"cloud-functions"模块部署到Cloud Functions,将后端服务"部署到GCE,这些都依赖于"core"等.

I have a multi-module project with some code running on Firebase Cloud Functions, and some running in other environments like Google Compute Engine. Simplifying things a little, I essentially have a 'core' module, and then I deploy the 'cloud-functions' module to Cloud Functions, 'backend-service' to GCE, which all depend on 'core' etc.

我正在使用bunyan在整个核心"模块中进行日志记录,并且在将其部署到GCE时,使用"@ google-cloud/logging-bunyan"配置了记录器,因此我的日志进入了堆栈驱动程序.

I'm using bunyan for logging throughout my 'core' module, and when deployed to GCE the logger is configured using '@google-cloud/logging-bunyan' so my logs go to Stack Driver.

在旁边:在Google Cloud Functions中使用此配置会导致Error: Endpoint read failed出现问题,我认为这是由于函数没有变冷并试图重用死连接而引起的,但是我不确定100%真正的原因原因是.

Aside: Using this configuration in Google Cloud Functions is causing issues with Error: Endpoint read failed which I think is due to functions not going cold and trying to reuse dead connections, but I'm not 100% sure what the real cause is.

所以现在我尝试使用console.log(arg)记录日志,其中arg是对象,而不是字符串.我希望此对象出现在jsonPayload下的堆栈驱动程序中,但已被字符串化并放入textPayload字段中.

So now I'm trying to log using console.log(arg) where arg is an object, not a string. I want this object to appear in Stack Driver under the jsonPayload but it's being stringified and put into the textPayload field.

推荐答案

我花了一段时间,但终于遇到了中firebase函数示例存储库.最后,我选择了一些类似的东西:

It took me awhile, but I finally came across this example in firebase functions samples repository. In the end I settled on something a bit like this:

const Logging = require('@google-cloud/logging');
const logging = new Logging();
const log = logging.log('my-func-logger');
const logMetadata = {
  resource: {
    type: 'cloud_function',
    labels: {
      function_name: process.env.FUNCTION_NAME ,
      project: process.env.GCLOUD_PROJECT,
      region: process.env.FUNCTION_REGION
    },
  },
};
const logData = { id: 1, score: 100 };
const entry = log.entry(logMetaData, logData);
log.write(entry)

您可以在logMetaData中添加字符串severity属性值(例如"INFO"或"ERROR"). 这是可能值的列表.

You can add a string severity property value to logMetaData (e.g. "INFO" or "ERROR"). Here is the list of possible values.

更新可用节点10的环境变量.这些似乎可以解决问题:

Update for available node 10 env vars. These seem to do the trick:

labels: {
  function_name: process.env.FUNCTION_TARGET,
  project: process.env.GCP_PROJECT,
  region: JSON.parse(process.env.FIREBASE_CONFIG).locationId
}


更新:对于Node 10运行时,他们希望您


UPDATE: Looks like for Node 10 runtimes they want you to set env values explicitly during deploy. I guess there has been a grace period in place because my deployed functions are still working.

这篇关于在Firebase Cloud Functions中记录'jsonPayload'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆