# dynamodb-logger

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

Latest version **2.0.0** (published 2022-12-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install dynamodb-logger
pnpm add dynamodb-logger
yarn add dynamodb-logger
bun add dynamodb-logger
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2022-12-20 |
| First published | 2022-10-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 41.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Vimson Varghese |
| Maintainers | vimson |
| Keywords | npm, javascript, typescript, nodejs, DynamoDB, logger, Easy Scanner |

## Links

- npm: https://www.npmjs.com/package/dynamodb-logger
- Repository: https://github.com/vimson/dynamodb-logger
- Homepage: https://github.com/vimson/dynamodb-logger.git
- Issues: https://github.com/vimson/dynamodb-logger.git/issues
- npm.io page: https://npm.io/package/dynamodb-logger

## Dependencies (3)

- [short-uuid](https://npm.io/package/short-uuid.md) 4.2.0
- [@aws-sdk/util-dynamodb](https://npm.io/package/@aws-sdk/util-dynamodb.md) 3.188.0
- [@aws-sdk/client-dynamodb](https://npm.io/package/@aws-sdk/client-dynamodb.md) 3.188.0

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 2.0.0 (latest) — 2022-12-20
- 1.0.0-alpha.1.1.0 — 2022-11-03
- 1.0.0-alpha.1.0.1 — 2022-10-15
- 1.0.0-alpha.1 — 2022-10-15
- 1.0.0 — 2022-10-15

## 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

```typescript
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

```typescript
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

```typescript
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

```typescript
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

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

Scanning the log table example is

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

---
_Source: https://npm.io/package/dynamodb-logger · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
