Cloud Logging: Node.js Client
Google Cloud Logging allows you to store, search, analyze,monitor, and alert on log data and events from Google Cloud Platform and Amazon Web Services.
If you require lightweight dependencies, an experimental, minified version ofthis library is available at@google-cloud/logging-min.Note:logging-min is experimental, and its feature surface is subject tochange.
A comprehensive list of changes in each version may be found inthe CHANGELOG.
- Cloud Logging Node.js Client API Reference
- Cloud Logging Documentation
- github.com/googleapis/nodejs-logging
Read more about the client libraries for Cloud APIs, including the olderGoogle APIs Client Libraries, inClient Libraries Explained.
Table of contents:
Quickstart
Before you begin
- Select or create a Cloud Platform project.
- Enable the Cloud Logging API.
- Set up authentication with a service account so you can access theAPI from your local workstation.
Installing the client library
npm install @google-cloud/loggingUsing the client library
// Imports the Google Cloud client libraryconst {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 = newLogging({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();}Batching Writes
High throughput applications should avoid awaiting calls to the logger:
await log.write(logEntry1);await log.write(logEntry2);Rather, applications should use afire and forget approach:
log.write(logEntry1);log.write(logEntry2);The@google-cloud/logging library will handle batching and dispatchingthese log lines to the API.
Writing to Stdout
TheLogSync class helps users easily write context-rich structured logs tostdout or any custom transport. It extracts additional log properties liketrace context from HTTP headers and can be used as an on/off toggle betweenwriting to the API or tostdout during local development.
Logs written tostdout are then picked up, out-of-process, by a Loggingagent in the respective GCP environment. Logging agents can add moreproperties to each entry before streaming it to the Logging API.
Read more aboutLogging agents.
Serverless applications like Cloud Functions, Cloud Run, and App Engineare highly recommended to use theLogSync class as async logs may be droppeddue to lack of CPU.
Read more aboutstructured logging.
// Optional: Create and configure a clientconst logging = new Logging();await logging.setProjectId()await logging.setDetectedResource()// Create a LogSync transport, defaulting to `process.stdout`const log = logging.logSync(logname);const meta = { // optional field overrides here };const entry = log.entry(meta, 'Your log message');log.write(entry);// Syntax sugar for logging at a specific severitylog.alert(entry);log.warning(entry);Samples
Samples are in thesamples/ directory. Each sample'sREADME.md has instructions for running its sample.
| Sample | Source Code | Try it |
|---|---|---|
| Fluent | source code | ![]() |
| Log HTTP Request | source code | ![]() |
| Logs | source code | ![]() |
| Quickstart | source code | ![]() |
| Sinks | source code | ![]() |
TheCloud Logging Node.js Client API Reference documentationalso contains samples.
Supported Node.js Versions
Our client libraries follow theNode.js release schedule.Libraries are compatible with all currentactive andmaintenance versions ofNode.js.
Client libraries targeting some end-of-life versions of Node.js are available, andcan be installed via npmdist-tags.The dist-tags follow the naming conventionlegacy-(version).
Legacy Node.js versions are supported as a best effort:
- Legacy versions will not be tested in continuous integration.
- Some security patches may not be able to be backported.
- Dependencies will not be kept up-to-date, and features will not be backported.
Legacy tags available
legacy-8: install client libraries from this dist-tag for versionscompatible with Node.js 8.
Versioning
This library followsSemantic Versioning.
This library is considered to beGeneral Availability (GA). This means itis stable; the code surface will not change in backwards-incompatible waysunless absolutely necessary (e.g. because of critical security issues) or withan extensive deprecation period. Issues and requests againstGA librariesare addressed with the highest priority.
More Information:Google Cloud Platform Launch Stages
Contributing
Contributions welcome! See theContributing Guide.
Please note that thisREADME.md, thesamples/README.md,and a variety of configuration files in this repository (including.nycrc andtsconfig.json)are generated from a central template. To edit one of these files, make an editto its template in thisdirectory.
License
Apache Version 2.0
SeeLICENSE
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-10-30 UTC.
