gcloud logging の制限について
gcloud logging コマンドを使うことで、ログエントリを書き込むことができます。
https://cloud.google.com/sdk/gcloud/reference/logging/write
しかし、gcloud コマンドを使った場合は、resource.type が global になります。
これを変えたい場合は、node.js 用に用意されているライブラリを使うと良いです。
https://www.npmjs.com/package/@google-cloud/logging
以下は、上記URLにはってあるサンプルコードです。
metadata で resource.type を指定しています。
// Imports the Google Cloud client library
const {Logging} = require('@google-cloud/logging');
async function quickstart(
projectId = 'YOUR_PROJECT_ID', // Your Google Cloud Platform project ID
logName = 'my-log' // The name of the log to write to
) {
// Creates a client
const logging = new Logging({projectId});
// Selects the log to write to
const log = logging.log(logName);
// The data to write to the log
const text = 'Hello, world!';
// The metadata associated with the entry
const metadata = {
resource: {type: 'global'},
// See: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity
severity: 'INFO',
};
// Prepares a log entry
const entry = log.entry(metadata, text);
async function writeLog() {
// Writes the log entry
await log.write(entry);
console.log(`Logged: ${text}`);
}
writeLog();
}
最近のコメント