2.0.0 • Published 2 months ago

@zaneray/gcp-node-logging v2.0.0

Weekly downloads
-
License
-
Repository
-
Last release
2 months ago

/**

  • This is a wrapper aroung the Winson logger, with support for logging to
  • Google Cloud Platform (GCP) Logging.
  • The behavior keys of NODE_ENV.
  • If NODE_ENV is 'development', logging is done to the console on one line
  • with a timestamped entry. Otherwise logging is streamed to the GCP Logging
  • in JSON format.
  • If NODE_ENV is 'development', all log levels are logged.
  • If NODE_ENV is 'test' or 'staging', log levels above 'debug' are logged.
  • If NODE_ENV is 'production', only log level 'error' is logged.
  • The level determined by NODE_ENV can be overwritten by calling the `logging.setLevel(level)'
  • method at runtime.
  • */

ZaneRay Node Logging

This is a wrapper aroung the Winson logger, with support for logging to Google Cloud Platform (GCP) Logging.

Environments

The logging behavior keys of NODE_ENV, and supports the following environments

  • development, all log levels are logged.
  • test, log levels >= 'debug' are logged.
  • staging, log levels >= 'debug' are logged.
  • production, log levels >= 'info' are logged.

Log Levels

The levels supported are the Winton defaults.

The log level can be changed at runtime by using the logging.setLevel(level) method.

Setting the log level at runtime affects all loggers and their transports in the runtime.

Logging levels in winston conform to the severity ordering specified by syslog and supported by GCP Logging.

See https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity for levels and semantics/usage.

In order of severity, the log levels are

  • debug, info, notice, warning, error, crit (short for critical), alert, emerg (short for emergency)

  • development, log levels >= 'debug' are logged.

  • test, log levels >= 'debug' are logged.
  • staging, log levels >= 'debug' are logged.
  • production, log levels >= 'error' are logged.

Transports

Transports defines where logging output is sent and the formet, based on the environment

  • development, logs to Console/Standard Out, on a single line with a timestamp
  • test, logs to GCP Logging in JSON format, GCP Logging sets the timestamp
  • staging, logs to GCP Logging in JSON format, GCP Logging sets the timestamp
  • production, logs to GCP Logging in JSON format, GCP Logging sets the timestamp

Usage (see test/loggingTest.mjs)

in your node project

yarn add @zaneray/gcp-node-logging

in your application code

const {logging, logger} = require("@zaneray/gcp-node-logging");
// Writes some log entries
logger.info("here's some information");
logger.warn("something might be wrong");
logger.error("something is wrong, seriously");

//change log level to log warn and above
logging.setLevel("warning");
//show the current level
logger.warning(logging.getLevel());

// Writes some log entries
logger.info("here's some information");
logger.warning("something might be wrong");
logger.error("something is wrong, seriously");

to configure different transports, or formats, the 'logging' object is also esposed.

import {logging} from '../lib/logging.mjs';
import winston from 'winston';

let transports = [new winston.transports.Console({level: "debug",handleExceptions: true})];
let format = winston.format.json();

const customLogger = logging.getCustomLogger(transports,format);

// Writes some log entries
customLogger.debug("Debug or trace information");
customLogger.info("Routine information, such as ongoing status or performance");
customLogger.notice("Normal but significant events, such as start up, shut down, or a configuration change");
customLogger.warning("Warning events might cause problems");
customLogger.error("Error events are likely to cause problems");
customLogger.crit("Critical events cause more severe problems or outages");
customLogger.alert("A person must take an action immediately");
customLogger.emerg("One or more systems are unusable");