Closed4
Cloud Logging for Node.js
- Winston: a general-purpose library, implementing a variety of log formatters and transports.
- Bunyan: specialized in structured JSON logs.
Node.js 用 Logging クライアントライブラリを直接使用することも、好みのロギングライブラリと独自の統合を作成することもできます。
Bunyan を使用する場合
Cloud Logging provides a plugin for the Bunyan Node.js Logging library. The Logging plugin for Bunyan provides a simpler, higher-level layer for working with Logging.
npm i bunyan @google-cloud/logging-bunyan
const bunyan = require('bunyan');
// Imports the Google Cloud client library for Bunyan
const { LoggingBunyan } = require('@google-cloud/logging-bunyan');
// Creates a Bunyan Cloud Logging client
const loggingBunyan = new LoggingBunyan();
// Create a Bunyan logger that streams to Cloud Logging
// Logs will be written to: "projects/YOUR_PROJECT_ID/logs/bunyan_log"
const logger = bunyan.createLogger({
// The JSON payload of the log as it appears in Cloud Logging
// will contain "name": "my-service"
name: 'my-service',
streams: [
// Log to the console at 'info' and above
{stream: process.stdout, level: 'info'},
// And log to Cloud Logging, logging at 'info' and above
loggingBunyan.stream('info'),
],
});
// Writes some log entries
logger.error('warp nacelles offline');
logger.info('shields at 99%');
Winston を使用する場合
Cloud Logging provides a plugin for the Winston Node.js Logging library. The Logging plugin for Winston provides a simpler, higher-level layer for working with Logging.
npm i winston @google-cloud/logging-winston
const winston = require('winston');
// Imports the Google Cloud client library for Winston
const { LoggingWinston } = require('@google-cloud/logging-winston');
const loggingWinston = new LoggingWinston();
// Create a Winston logger that streams to Cloud Logging
// Logs will be written to: "projects/YOUR_PROJECT_ID/logs/winston_log"
const logger = winston.createLogger({
level: 'info',
transports: [
new winston.transports.Console(),
// Add Cloud Logging
loggingWinston,
],
});
// Writes some log entries
logger.error('warp nacelles offline');
logger.info('shields at 99%');
Cloud Logging クライアントライブラリを使用する場合
Client libraries provide an integration option with Logging. You can use client libraries in addition to using an agent.
npm i @google-cloud/logging
// 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();
}
このスクラップは2024/04/21にクローズされました