npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@velove/structured-logger

v2.1.3

Published

Structured logging & error reporting in GCP for node.js

Downloads

94

Readme

Velove Structured Logger

Structured logging & error reporting in GCP for node.js

By default, log messages to stdout (console), but provides an opt-in option to forward logs to Cloud Logging and Error Reporting (GCP)

Severity levels

The different levels are inspired from the Syslog levels

  • EMERGENCY: Panic condition: like a system is down
  • ALERT: Error requiring immediate attention (potentially corrupted data)
  • CRITICAL: Hard issue, system is at risk
  • ERROR: program (unexpected) errors to be handled
  • WARNING:
  • NOTICE: Not errors, but may need some attention
  • INFO: Informational messages about the program behavior
  • DEBUG: Program debugging informations
  • VERBOSE: the highest level, usually for large objects

Usage

Some options can be injected to the exposed "getLogger" function. You can use those to define generic options for your logger, potentially opt-in to GCP Cloud Logging in production for instance.

// logger.ts
import { getLogger as getBaseLogger } from '@velove/structured-logger';

// You can define some flags in env parameters if needed
// And probably define that in a shared file
export const getLogger = (request?: FastifyRequest) =>
  getBaseLogger({
    // default to WARNING
    level: 'WARNING',
    // Default to false, you need to opt-in if you wish to enable Cloud Logging and Error Reporting
    useGoogleCloudAgent: process.env.NODE_ENV === 'production',
    loggerAgentOptions: {
      // Name to identify logs in GCP
      name: 'velove.my-application-name',
      // Can be any string, like a commit hash or something
      version: '1.0.0',
      // Careful with GDPR and other local legislations (default to false)
      logClientIp: false,
      // Those credentials are only needed if you wish to test logging to GCP outside of it (like from your local environment)
      options: {
        projectId: process.env.GCLOUD_PROJECT_ID,
        keyFilename: process.env.GCP_CREDENTIALS_FILE
      }
    },
    request
  });

/** Possible to use directly, but will not be enriched by the HTTP request context */
const requestUnawareLogger = getLogger();

export default requestUnawareLogger;

Within a Fastify server: inject the HTTP request to get an enriched log context

// app.ts
import { getLogger } from 'logger.ts';

app.get('/foo', (request) => {
  const logger = getLogger(request);
  const contextName = 'request.foo'; // up to you

  logger.debug('Hello World', { contextName }); // log with high-level "debug"
  logger.info('My name is Bond. James Bond.', { contextName }); // log with level "info"

  try {
    somethingThatCouldFail();
  } catch (error) {
    logger.error(error, { contextName }); // log an Error object will also forward it to Error Reporting
  }
});

Outside of Fastify, use it straight without the request

import logger from 'logger.ts';

export function myFunction() {
  const logger = logger.warn('"myFunction" is not not 100% reliable yet.', { contextName: 'functions.myFunction' });

  if (Math.random() > 0.9) {
    throw new Error('I did warn you though');
  }
}