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

@yingyeothon/logger-s3

v0.4.0

Published

A very simple logger to send them into S3

Downloads

5

Readme

Logger S3

A library to send logs into S3 via s3-cache-bridge.

Logger

Usage

import LogSeverity from "@yingyeothon/logger/lib/severity";
import S3Logger from "@yingyeothon/logger-s3";
import { serializeError } from "serialize-error";

function buildLogFileName(date: Date, severity: LogSeverity) {
  function zeroPad(value: number, length: number) {
    return `0${value}`.slice(-length);
  }
  return [
    "logging",
    "mylog",
    severity,
    date.getFullYear() +
      zeroPad(date.getMonth() + 1, 2) +
      zeroPad(date.getDate(), 2)
  ].join("/");
}

const { logger, flush } = S3Logger({
  // A function to build the S3 Key of logging file.
  asKey: buildLogFileName,

  // Connection information for s3-cache-bridge
  // https://github.com/lacti/s3-cache-bridge#environment
  apiUrl: "http://localhost:3000/", // Or, process.env.S3CB_URL
  // Optional credential for S3CB.
  apiId: "test", // Or, process.env.S3CB_ID
  apiPassword: " test", // Or, process.env.S3CB_PASSWORD

  // Default parameters.
  // Serialize as JSON.
  serializer: (timestamp, level, args) =>
    JSON.stringify({
      level,
      timestamp: timestamp.toISOString(),
      args: args.map(arg => {
        if (arg instanceof Error) {
          return serializeError(arg);
        }
        return arg;
      })
    }) + "\n",
  autoFlushIntervalMillis: 10 * 1000,
  autoFlushMaxBufferSize: 1024,
  severity: "info",
  withConsole: false
});

async function main() {
  try {
    logger.debug(`Hello`, `World`);
    logger.info(`Info with` /*, Some object can be placed in here. */);
  } catch (error) {
    logger.error(`Error occurred`, error);
  }
  await flush();
}

Simplified

If process.env.S3CB_URL, process.env.S3CB_ID and process.env.S3CB_PASSWORD are already set and there is no need to rotate a logging file, it can be simplified like this.

import S3Logger from "@yingyeothon/logger-s3";

async function main() {
  const { logger, flush } = S3Logger({
    asKey: () => `logging/mylog/all`
  });
  try {
    logger.debug(`Hello`, `World`);
    logger.info(`Info with` /*, Some object can be placed in here. */);
  } catch (error) {
    logger.error(`Error occurred`, error);
  }
  await flush();
}

AWS Lambda Helper

It will append logs to logging/${systemName}/${yyyyMMdd}.

import { LambdaS3Logger } from "@yingyeothon/logger-s3";

const { logger, flush, updateSystemId } = LambdaS3Logger({
  logKeyPrefix: "logging",

  // Lambda information
  systemName: "HelloWorld",
  lambdaId: "2f40adbe-b450-40fe-9796-cc3d072b4c62",
  handlerName: "testHandler",

  // Logger behavior
  severity: "debug",
  withConsole: false, // Its value is true as default in Lambda logger.

  // S3CB Connection
  apiUrl: "http://localhost:3000/",
  apiId: "test",
  apiPassword: " test"
});

async function main(systemId: string) {
  logger.info("Before having systemId");
  updateSystemId(systemId);
  logger.info("After systemId is set");
  await flush();
}

main("COMPLEX-SYSTEM-ID");

This is an example of a JSON serialized log tuple.

{
  "timestamp": "2020-03-15T12:36:38.094Z",
  "level": "info",
  "systemName": "HelloWorld",
  "systemId": "COMPLEX-SYSTEM-ID",
  "handlerName": "testHandler",
  "lambdaId": "2f40adbe-b450-40fe-9796-cc3d072b4c62",
  "args": ["Before having systemId"]
}
{
  "timestamp": "2020-03-15T12:36:38.094Z",
  "level": "info",
  "systemName": "HelloWorld",
  "systemId": "COMPLEX-SYSTEM-ID",
  "handlerName": "testHandler",
  "lambdaId": "2f40adbe-b450-40fe-9796-cc3d072b4c62",
  "args": ["After systemId is set"]
}

In this case, withConsole is true as default. And it writes a log like this.

# TIMESTAMP LEVEL SYSTEM-NAME SYSTEM-ID HANDLER-NAME LAMBDA-ID LOG-ARGS
2020-03-15T13:10:23.344Z INFO HelloWorld null testHandler 2f40adbe-b450-40fe-9796-cc3d072b4c62 Before having systemId
2020-03-15T13:10:23.345Z INFO HelloWorld COMPLEX-SYSTEM-ID testHandler 2f40adbe-b450-40fe-9796-cc3d072b4c62 After systemId is set

The first console log doesn't have systemId because it is written beforesystemId is set. But the first JSON log have systemId because it is flushed aftersystemId is set. The sequence of above example is logger.info (to console) -> updateSystemId -> logger.info (to console) -> flush (to S3 with JSON via S3CB).

License

MIT