【问题标题】:GCP, the stdout is empty when read cloud function logs by nodejsGCP,nodejs读取云函数日志时stdout为空
【发布时间】:2018-09-13 13:12:03
【问题描述】:

这是我的nodejs代码:

const cp = require('child_process');

describe('cloud function test suites', () => {
  describe('deleteCampaign test suites', () => {
    const cloudFunctionName = 'deleteCampaign';
    it('should print campaign data', () => {
      const campaign = { id: '1' };
      const encodedCampaign = Buffer.from(JSON.stringify(campaign)).toString(
        'base64',
      );
      const data = JSON.stringify({ data: encodedCampaign });

      const executeResultOutput = cp
        .execSync(
          `gcloud beta functions call ${cloudFunctionName} --data '${data}'`,
        )
        .toString();

      const executionId = executeResultOutput.split(': ')[1];

      const logs = cp
        .execSync(
          `gcloud beta functions logs read ${cloudFunctionName} --execution-id ${executionId}`,
        )
        .toString();

      console.log(logs);

      expect(logs).toContain('campaign:  {"id":"1"}');
    });
  });
});

我想将日志打印到标准输出,但日志是空字符串。

但是当我使用gcloud 命令行读取日志时,没关系。标准输出是正确的:

gcloud beta functions logs read deleteCampaign --execution-id ee5owvtzlekc
LEVEL  NAME            EXECUTION_ID  TIME_UTC                 LOG
D      deleteCampaign  ee5owvtzlekc  2018-09-13 12:46:17.734  Function execution started
I      deleteCampaign  ee5owvtzlekc  2018-09-13 12:46:17.738  campaign:  {"id":"1"}
D      deleteCampaign  ee5owvtzlekc  2018-09-13 12:46:17.742  Function execution took 9 ms, finished with status: 'ok'

我使用jestnodejs 为我的云功能编写一些测试。为什么日志是空字符串?

【问题讨论】:

    标签: javascript google-cloud-platform google-cloud-functions


    【解决方案1】:

    您尝试获取的字符串为空,因为生成日志需要更多时间。即使 Google Cloud Function 已完成执行,您也必须等待几秒钟才能准备好日志。

    阅读您的代码,您不会让这种情况发生,因此您会得到一个空字符串。

    我注意到阅读您的代码的第一件事是这部分:

    const executionId = executeResultOutput.split(': ')[1];
    

    我了解到您想要提取 Google Cloud Function 的执行 ID。我在这里遇到了问题,因为字符串不限于执行 ID,它还包括一个换行符和“结果”一词。我确保只使用下一个代码提取必要的执行 ID:

    const executionId = executeResultOutput.split(':')[1]; //We get the GCP ID. 
    const executionId2 = executionId.split("\n")[0].toString(); //removing the right part of the string. 
    

    如果您找到了没有问题地获取执行 ID 的方法,请忽略我的代码。

    您可以在下面找到对我实现功能有用的代码。

    let cloudFunctionLog ='';
    
    function getLogs(){
        console.log('Trying to get logs...');
        const logs = cp
        .execSync(`gcloud beta functions logs read ${cloudFunctionName} --execution-id ${executionId2}`);
        return logs;
    }
    
    do{
        cloudFunctionLog=getLogs();
        if(!cloudFunctionLog){
            console.log('Logs are not ready yet...');
        }else{
            console.log(`${cloudFunctionLog}`);
        }
    }while(!cloudFunctionLog);//Do it while the string comes empty.
    

    当日志不再为空时,它们会显示在您的控制台中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 2019-01-20
      • 2020-10-14
      • 2023-03-12
      • 2021-05-24
      • 2021-10-06
      • 1970-01-01
      相关资源
      最近更新 更多