For Node.js applications, plugins are maintained for the popular Bunyan and Winston logging libraries.
Winston is a general-purpose library, implementing a variety of log formatters and transports.
Bunyan is specialized in structured JSON logs. Log formatting can be performed by piping to the Bunyan command line.
You can also use the Logging client library for Node.js directly, or create your own integrations with your preferred logging library.
The Logging agent does not have to be installed to use the Bunyan and Winston libraries on a Compute Engine VM instance.
Before you begin
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.
- Enable the Cloud Logging API.
- Prepare your environment for Node.js development.
Using 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.
Installing the plugin
The easiest way to install the Logging Bunyan plugin is with npm:
npm install --save bunyan @google-cloud/logging-bunyan
Import the plugin and add it to your Bunyan configuration:
Configuring the plugin
You can customize the behavior of the Bunyan plugin
using the same configuration options supported by
the Cloud Logging API Cloud client library for Node.js.
These options can be passed in the options
object passed to the plugin's
constructor.
Using Bunyan and Express
You can set up and use Bunyan with Logging in a Node.js Express application.
Using 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.
Installing the plugin
The easiest way to install the Logging Winston plugin is with npm:
npm install --save @google-cloud/logging-winston winston
Import the plugin and add it to your Winston configuration:
Configuring the plugin
You can customize the behavior of the Winston
plugin using the same
configuration options supported by
the Cloud Logging API Cloud client library for Node.js.
These options can be passed in the options
object passed to the plugin's
constructor.
For more information on installation, see the documentation for the Cloud Logging libraries for Node.js. You can also report issues using the issue tracker.
Using the Cloud Client Library directly
For information on using the Cloud Logging Cloud client library for Node.js directly, see Cloud Logging Client Libraries.
Running on Google Cloud
Using Cloud Logging libraries for Node.js requires the IAM Logs Writer role on Google Cloud. Most Google Cloud environments provide this role by default.
App Engine
App Engine grants the Logs Writer role by default.
The Cloud Logging libraries for Node.js can be used without needing to explicitly provide credentials.
Cloud Logging is automatically enabled for App Engine applications. No additional setup is required.
Google Kubernetes Engine
On Google Kubernetes Engine, you must add the logging.write
access
scope when creating the cluster:
gcloud container clusters create example-cluster-name --scopes https://www.googleapis.com/auth/logging.write
Compute Engine
When using Compute Engine VM instances, add the cloud-platform
access scope to each instance. When creating a new instance through the
Google Cloud Console, you can do this in the Identity and API access section
of the Create Instance panel. Use the Compute Engine default service
account or another service account of your choice, and select
Allow full access to all Cloud APIs in the Identity and API access
section. Whichever service
account you select, ensure that it has been granted the
Logs Writer role in the IAM & Admin section of the
Cloud Console.
Cloud Functions
Cloud Functions grants the Logs Writer role by default.
The Cloud Logging libraries for Node.js can be used without needing to explicitly provide credentials.
Cloud Functions is configured to use Cloud Logging automatically.
Running locally and elsewhere
To use the Cloud Logging libraries for Node.js outside of Google Cloud, including running the library on your own workstation, on your data center's computers, or on the VM instances of another cloud provider, you must supply your Google Cloud project ID and appropriate service account credentials directly to the Cloud Logging libraries for Node.js.
You can create and obtain service account credentials manually. When specifying the Role field, use the Logs Writer role. For more information on Identity and Access Management roles, go to Access control guide.
Using Bunyan:Using Winston:
Viewing the logs
After deployment, you can view the logs in the Logs Explorer.
In the Logs Explorer, you must specify one or more resources, but the resource selection might not be obvious. Here are some tips to help you get started:
If you are deploying your application to App Engine or using the App Engine-specific libraries, set your resource to GAE Application.
If you are deploying your application on Compute Engine, set the resource to GCE VM Instance.
If you are deploying your application on Google Kubernetes Engine, your cluster's logging configuration determines the resource type of the log entries. For a detailed discussion on the Legacy Google Cloud's operations suite and the Google Cloud's operations suite Kubernetes Monitoring solutions, and how those options affect the resource type, see Migrating to Google Cloud's operations suite Kubernetes Monitoring.
If your application is using the Cloud Logging API directly, the resource is dependent on the API and your configuration. For example, in your application, you can specify a resource or use a default resource.
If you don't see any logs in the Logs Explorer, to see all log entries, switch to the advanced query mode and use an empty query.
- To switch to the advanced query mode, click menu (▾) at the top of the Logs Explorer and then select Convert to advanced filter.
- Clear the content that appears in the filter box.
- Click Submit Filter.
You can examine the individual entries to identify your resources.
For additional information, see Viewing Logs and Advanced logs queries.