A simple and customizable logger for Node.js applications with support for colored console output and optional file logging.
Install the module using your preferred package manager
npm install prettierlogs
yarn install prettierlogs
bun install prettierlogs
import { prettier } from 'prettierlogs'
You can create a logger instance with an optional prefix, log level, and log file configuration.
/** CREATE A NEW DEBUG LOG AND SAVE THE LOGS CREATED UNDER IT TO A "app.log" FILE */
const logs = new prettier('My App', 'debug', { path: 'app.log', enabled: true });
You can log messages at different levels:
logs.info('This is an info message');
logs.warn('This is a warning message');
logs.error('This is an error message');
logs.ready('The application is ready');
logs.debug('This is a debug message');
You can change the log level dynamically:
logs.setLogLevel('warn');
new Logger(prefix?: string, logLevel?: LogLevel, logFile?: LogFile);
prefix
(optional): A string to prefix all log messages with.logLevel
(optional): The minimum log level to output ('info', 'warn', 'error', 'ready', 'debug'). Default is 'info'.logFile
(optional): An object with path
and enabled
properties to configure file logging.info(message: string | object): void
: Logs an info message.warn(message: string | object): void
: Logs a warning message.error(message: string | object): void
: Logs an error message.ready(message: string | object): void
: Logs a ready message.debug(message: string | object): void
: Logs a debug message.setLogLevel(level: LogLevel): void
: Sets the log level.An interface to configure file logging.
interface LogFile {
path: string;
enabled: boolean;
}
A type representing the log levels.
type LogLevel = 'info' | 'warn' | 'error' | 'ready' | 'debug';
import { prettier } from 'prettierlogs';
const logs = new prettier('MyApp', 'debug', { path: 'app.log', enabled: true });
logs.info('This is an info message');
logs.warn('This is a warning message');
logs.error('This is an error message');
logs.ready('The application is ready');
logs.debug('This is a debug message');
logs.setLogLevel('warn');
logs.debug('This debug message will not be logged');
logs.error('This error message will be logged');