Skip to content

ImLunaHey/logger

Repository files navigation

@ImLunaHey/logger

@ImLunaHey/logger is a TypeScript package that provides a logger utility based on the winston library.

Installation

You can install @ImLunaHey/logger using npm:

npm i @ImLunaHey/logger

Usage

To use the logger, you need to import the Logger class from the package:

import { Logger } from '@ImLunaHey/logger';

Then, you can create an instance of the logger by providing the necessary options:

const logger = new Logger({
  service: 'my-service',
  schema: MySchema, // Optional: Define a schema for the log data
});

Log Levels

The logger supports the following log levels:

  • debug
  • info
  • warn
  • error

You can use the logger's methods to log messages at the desired level:

logger.debug('This is a debug message');
logger.info('This is an info message');
logger.warn('This is a warning message');
logger.error('This is an error message', { error: new Error('Something went wrong') });
logger.error('This is an error message', { error: new Error('Something went wrong', { cause: new Error('This actually caused the error') }) });

Log Data Schema

If you have defined a schema for your log data, you can pass it as an option when creating the logger instance. The schema is used to validate the log data and ensure that it conforms to the expected structure. For example:

import z from 'zod';
import { Logger } from '@ImLunaHey/logger';

const schema = {
  info: {
    'User logged in': {
      userId: z.string(),
      action: z.string(),
    },
  },
};

const logger = new Logger({
  service: 'my-service',
  schema,
});

logger.info('User logged in', { userId: '123', action: 'login' });

If extra keys are included in the meta object they will be stripped.

Test Environment

When running tests, the logger will not output any logs to prevent interference with the test output. This behaviour can be controlled by setting the NODE_ENV environment variable to 'test'.

License

This package is provided under the MIT License.