@ImLunaHey/logger
is a TypeScript package that provides a logger utility based on the winston
library.
You can install @ImLunaHey/logger
using npm:
npm i @ImLunaHey/logger
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
});
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') }) });
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.
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'
.
This package is provided under the MIT License.