Giter Site home page Giter Site logo

dynamodb-logger's Introduction

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);

dynamodb-logger's People

Contributors

vimson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.