gpt4 book ai didi

node.js - Nodejs 在 google-cloud/logging 中编写 protopayload

转载 作者:行者123 更新时间:2023-12-02 12:11:05 29 4
gpt4 key购买 nike

我们如何在 NodeJS 中使用 google-cloud/logging 包传递 protopayload 联合标签?

到目前为止,我可以在日志中写入 textPayload 和 jsonPayload。

尝试使用 google-protobuf 和 protobufjs 创建 Protocol Buffer ,甚至尝试了 Protocol Buffer 包,但是当我执行 log.entry 和 log.write 时,在日志中看不到有效负载。

这是写日志的代码
//writelogs.js

function writeLogEntryAdvanced (logName, options, callback) {
var logging = Logging();
var log = logging.log(logName);
var protobuf = require('protocol-buffers');
var fs = require('file-system');
// pass a proto file as a buffer/string or pass a parsed protobuf-schema object
var messages = protobuf(fs.readFileSync('test.proto'));
var buf = messages.Test.encode({
num: 42,
payload: 'hello world'
});
console.log(buf);
var entry = log.entry({ resource: options.resource }, buf);
// See https://googlecloudplatform.github.io/google-cloud-node/#/docs/logging/latest/logging/log?method=write
log.write(entry, function (err, apiResponse) {
if (err) {
return callback(err);
}
console.log('Wrote entry to log: %s', logName);
return callback(null, apiResponse);
});
}

//测试.proto

message Test {
required float num = 1;
required string payload = 2;
}

编写代码是从github上的nodejs-doc-samples复制的,我自己对protopayload做了一些更改。示例日志写入仅适用于 jsonPayload 和 textPayload。

最佳答案

不允许在日志 API 中的 protoPayload 字段中使用任意 Protocol Buffer 。仅允许在该领域使用少数具有众所周知描述符的 Google Cloud Platform 服务。

对于任意结构化日志记录,您应该使用 jsonPayload 字段。

有关这些字段的更多信息记录在:https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry .

关于node.js - Nodejs 在 google-cloud/logging 中编写 protopayload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43539160/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com