@burakbey/pino-mongodb
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@burakbey/pino-mongodb 🎄

@burakbey/pino-mongodb is a package that enables you to save logs from pino to MongoDB, offering a convenient method to store your application's logs.

Installation 🚀

To get started, use your preferred package manager to install the @burakbey/pino-mongodb package.

yarn install @burakbey/pino-mongodb

Implementation 🛠️

Using @burakbey/pino-mongodb is straightforward. Simply import the getWritableStream function and use it as a stream in your code, as shown in the examples below.

import pino from 'pino';

import { getWritableStream } from '@burakbey/pino-mongodb';

async function bootstrap() {
  const mongodbStream = await getWritableStream({
    connectionUri: 'mongodb://localhost:27017',
    collectionName: 'logs',
    dbName: 'logs'
  });

  const logger = pino(
    { level: 'info' },
    pino.multistream([{ stream: process.stdout }, { stream: mongodbStream }])
  );

  logger.info('hello');
}

bootstrap();

You can also customize the default formatter to suit your needs, giving you the flexibility to modify the log data before it is stored in the MongoDB instance. This includes controlling how the timestamp is converted and making any other desired modifications to the log entries.

import pino from 'pino';

import { FormatFunction, getWritableStream } from '@burakbey/pino-mongodb';

const customFormat: FormatFunction = chunk => {
  const result = chunk
    .split('\n')
    .filter(x => x)
    .map(x => JSON.parse(x))
    .map(x => {
      // do whatever you want here
      // if (x.time) {
      //   x.time = new Date(x.time);
      // }

      return x;
    });

  return result;
};

async function bootstrap() {
  const mongodbStream = await getWritableStream({
    connectionUri: 'mongodb://localhost:27017',
    collectionName: 'logs',
    dbName: 'logs',
    format: customFormat
  });

  const logger = pino(
    { level: 'info' },
    pino.multistream([{ stream: process.stdout }, { stream: mongodbStream }])
  );

  logger.info('hello');
}

bootstrap();

With @burakbey/pino-mongodb, you can easily save and manage your application logs in a MongoDB database. Enjoy streamlined log management for your projects!

Package Sidebar

Install

npm i @burakbey/pino-mongodb

Weekly Downloads

5

Version

1.0.1

License

MIT

Unpacked Size

9.54 kB

Total Files

11

Last publish

Collaborators

  • burakbey