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

pino-sns-transport

v1.3.4

Published

Simple transport to publish logs to sns topics

Downloads

29,428

Readme

pino-sns-transport

NPM version

A pino v7+ transport for sending logs to AWS SNS.

It uses @aws-sdk/client-sns to send logs to sns topics.

  • Single required config

Table of Contents

Installation

npm install pino-sns-transport

Quick Usage

import pino, { TransportTargetOptions } from 'pino';
import type { SnsTransportOptions } from 'pino-sns-transport';

const transportTargets: TransportTargetOptions[] = [
  {
    target: 'pino-sns-transport',
    options: {
      topic: process.env.TOPIC,
    } as SnsTransportOptions,
    level: 'warn',
  },
];

const transport = pino.transport({
  targets: transportTargets,
});

const logger = pino(
  {
    /**
     * Set this to trace or the minimum from the logging
     * levels of the transports so that all logs are
     * forwarded to transports, each transport carries its
     * own level and therefore can decide whether it wants
     * to log or not
     */
    level: 'trace',
  },
  transport,
)

Configuration options

SnsTransportOptions

import { SNSClientConfig } from "@aws-sdk/client-sns";

export type LogFilter = {
  key: string;
  pattern: RegExp,
}

export type SnsTransportOptions = {
  snsClientConfig?: SNSClientConfig;
  topic?: string;
  topicArn?: string;
  beautify?: boolean;
  beautifyOptions?: {
    indentSize?: number;
    maxWidth?: number;
  };
  excludeKeys?: string[];
  keyExamineDepth?: number;
  includeLogs?: LogFilter[];
  excludeLogs?: LogFilter[];
}

Description

  • topic is the name of the sns topic to publish logs to. When provided the full arn of the sns topic is constructed by getting the region and aws account id from the aws sns client. The full arn is then passed to the publish method of the aws-sdk to push logs
  • topicArn should be the arn of the sns topic to publish logs to. It is useful for scenarios where cross region or cross account logs need to be published. Either one of topic or topicArn must be provided. When both are provided topicArn is given preference
  • snsClientConfig is optional and anything passed to it is forwarded directly to the aws-sdk thus making the underlying aws-sdk client transparently configurable
  • beautify is true by default but will not take effect until the optional dependency json-beautify is also installed, if you happen to have the dependency for unrelated reasons and don't want your logs to be formatted you can turn it off here
  • beautifyOptions are parameters passed to json-beautify and don't take effect until beautify is true and the dependency is met
  • excludeKeys can be used to delete keys from the json log before publish. Also supports dot notation for removing nested keys, see full example below
  • keyExamineDepth is the maximum depth level of json objects at which the keys will be examined for excludeKeys. The default value is 3.
  • includeLogs can be used to filter for logs that need to published and discard the rest. Providing an empty array here has the same effect as not providing a value which is that all logs will be published unless filtered out by excludeLogs. Unlike excludeKeys, this does not support dot notation for now
  • excludeLogs can be used to prevent certain logs from being published whose value at key matches the pattern. Unlike excludeKeys, this does not support dot notation for now

Nuances

  • If both includeLogs and excludeLogs are specified and a log matches both of them then it will be excluded.
  • Any errors encountered in initialization or publishing logs are printed to stdout with the context key as pino-sns-transport in pino style compatible with pino-pretty. Also includes the error and the original log that could not be published.

Example

const transportTargets: TransportTargetOptions[] = [
  {
    target: 'pino-sns-transport',
    options: {
      topicArn: process.env.TOPIC_ARN,
      excludeKeys: [
        'pid',
        'hostname',
        'res.headers',
        'req.headers',
        'req.remoteAddress',
        'req.remotePort',
      ],
      excludeLogs: [
        {
          key: 'msg',
          pattern: /Request (Completed|Errored)/,
        },
        {
          key: 'context',
          pattern: /ExceptionsHandler/,
        },
      ],
    } as SnsTransportOptions,
    level: 'warn',
  },
];