gcloud-logger

simple logger for gcp

Usage no npm install needed!

<script type="module">
  import gcloudLogger from 'https://cdn.skypack.dev/gcloud-logger';
</script>

README

gcloud-logger

NPM Version Build Status Test Coverage

A simple lightweight logger for GCP Stackdriver.

Contents

  1. Usage requirements
  2. Installation
  3. Quick start
  4. Levels
  5. uncaughtException and unhandledRejection handlers

Usage requirements

Requires Node version 10 or higher.

To use Stackdriver logging your application must have access to the GCP project and permission to write logs.

Installation

npm install gcloud-logger

Quick start

To initialize the logger pass the options object to the method createLogger.

Configuration is simple and consists of next params:

  • console - writing logs to stdout
  • stackdriver - sending logs to Stackdriver

You can omit any of these properties or set the value to falsy if it's not needed.

const gcloudLogger = require('gcloud-logger');

const logger = gcloudLogger.createLogger({
  console: true,
  stackdriver: { projectId: 'gcloud-project-ID', logName: 'log-name' },
});

logger.debug('Debug');
logger.error(new Error('Something went wrong'));

Stackdriver logs screenshot

Levels

Available logging levels (RFC 5424) and corresponding Stackdriver severity:

logger.debug('Debug');
logger.info('Info');
logger.notice('Info');
logger.warning('Warning');
logger.error('Error');
logger.crit('Fatal');
logger.alert('Fatal');
logger.emerg('Fatal');

uncaughtException and unhandledRejection handlers

You can add the following properties to your options object to log uncaughtException and unhandledRejection errors:

const gcloudLogger = require('gcloud-logger');

const logger = gcloudLogger.createLogger({
  console: true,
  logExceptionLevel: 'alert',
  logRejectionLevel: 'crit',
});

Note: the logger will exit with process.exit(1) after logging uncaughtException or unhandledRejection.