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

dynamodb-logger

v2.0.0

Published

A DynamoDB wrapper which logs all the entries to the table and an easy way to query the logs

Downloads

12

Readme

DynamoDB wrapper for logging and scanning the logs in an easy manner

We all know the pain of finding the data from the logs especially when working with Lambda function in serverless environment. I agree cloudwatch is one of the great options for logging. But we need an easy solution to scan the logs without using the setup like Elasticsearch stack. That's why we developed this small library which is basically a DynamoDb wrapper, which easily do the basic insert and scan operation very easily.

Table name: LambdaLogs

| Partition key | Sort key | | ------------- | ----------- | | PK (String) | SK (String) |

Local secondary indexes

| Name | Partition key | Sort key | Projected attributes | | ----------- | ------------- | ----------------- | -------------------- | | LSI-1-index | PK (String) | LSI-1-SK (String) | All | | LSI-2-index | PK (String) | LSI-2-SK (String) | All | | LSI-3-index | PK (String) | LSI-3-SK (String) | All |

The above is the default Table & Index convention. But it is totally configurable. Deafult configuration is

const config: LoggerConfig = {
  region: 'eu-west-1',
  table: {
    name: 'LambdaLogs',
    partitionKey: 'PK',
    sortKey: 'SK',
    lsi1SortKey: 'LSI-1-SK',
    lsi2SortKey: 'LSI-2-SK',
    lsi1Index: 'LSI-1-index',
    lsi2Index: 'LSI-2-index',
  },
};

Type details are

export type LoggerTableConfig = {
  name: string;
  partitionKey?: string;
  sortKey?: string;
  lsi1SortKey?: string;
  lsi2SortKey?: string;
  lsi1Index?: string;
  lsi2Index?: string;
};

export type LoggerConfig = {
  region: string;
  credentials?: Credentials; //import { Credentials } from '@aws-sdk/types';
  table?: LoggerTableConfig;
};

An example implementation if you follow the same table name and index names are

import { logger, getLoggerConfig } from 'dynamodb-logger';
logger.initailize(getLoggerConfig()); // If you are using the same Table and index name conventions as above

An example if you are using diferent table name and index names are then

import { logger, getLoggerConfig } from 'dynamodb-logger';
logger.initailize({
  region: 'eu-west-1',
  table: {
    name: 'LambdaLogs',
    partitionKey: 'PK',
    sortKey: 'SK',
    lsi1SortKey: 'LSI-1-SK',
    lsi2SortKey: 'LSI-2-SK',
    lsi1Index: 'LSI-1-index',
    lsi2Index: 'LSI-2-index',
  },
  credentials: {
    accessKeyId: 'XXXXXXX',
    secretAccessKey: 'YYYYYYYYY',
  },
}); // If you are using API keys and different table name and index names

It is better you can use a logger helper module to put this initialzation and can use in your project

Logging example is

logger.log(LogModules.LambdaInvoker, LogLevels.Payload, JSON.stringify(logStack, null, 2)); // we added LogModules & LogLevels as fixed constants

Scanning the log table example is

import { logger, getLoggerConfig } from 'dynamodb-logger';
logger.initailize(getLoggerConfig());

const searchOptions: LogSearchConfig = {
  module: LogModules.PushNotifier,
  level: LogLevel.ERROR,
  startTime: '2022-10-03T07:01:46.037Z',
  endTime: '2022-12-03T07:01:46.037Z',
  limit: 1,
  sort: 'asc',
};

let logResult: LogResult;
do {
  logResult = await logger.search(searchOptions);
  if (logResult.lastEvaluatedKey) {
    searchOptions.lastEvaluatedKey = logResult.lastEvaluatedKey;
  }
  console.log(logResult);
} while (logResult.lastEvaluatedKey);