Mini ANSI Logger Documentation
A simple, customizable logger for Node.js applications with ANSI color support.
npm install mini-ansi-logger
If you are using TypeScript, type definitions are included automatically.
- Supports multiple log levels:
log
,info
,success
,warn
,error
- Customizable colors for log messages using ANSI codes
- Option to log messages to a file with a customizable path
- Configurable logger instances
- TypeScript support with type definitions included
// CommonJS
const { Logger, Colors } = require('mini-ansi-logger');
// ES Modules
import { Logger, Colors } from 'mini-ansi-logger';
// Create a new logger instance with default configuration
const logger = new Logger();
// Create a logger with custom configuration
const customLogger = new Logger({ logPath: '/custom/log/path' });
logger.log('This is a general log message');
logger.info('This is an informational message');
logger.success('Operation was successful');
logger.warn('This is a warning message');
logger.error('An error occurred');
logger.log('This is a magenta log message', { color: Colors.FgMagenta });
logger.info('This message will be logged to a file', { logToFile: true });
The Logger
class provides methods to log messages with different levels and configurations.
new Logger(config?: LoggerConfig)
Parameters:
config
(optional): An object containing configuration options.
-
log(message: string, options?: LogOptions): void
Logs a general message.
-
info(message: string, options?: LogOptions): void
Logs an informational message.
-
success(message: string, options?: LogOptions): void
Logs a success message.
-
warn(message: string, options?: LogOptions): void
Logs a warning message.
-
error(message: string, options?: LogOptions): void
Logs an error message.
-
configure(config: LoggerConfig): void
Updates the logger's configuration.
Configuration options for the logger instance.
interface LoggerConfig {
logPath?: string;
}
Properties:
logPath
(optional): A string specifying the directory path where logs will be saved.
Options for individual log messages.
interface LogOptions {
name?: string;
logToFile?: boolean;
isThrow?: boolean;
color?: Colors;
}
Properties:
name
(optional): A custom label for the log message.logToFile
(optional): A boolean indicating whether to log the message to a file.isThrow
(optional): If set totrue
in anerror
log, it will throw an exception.color
(optional): A color code from theColors
enum to customize the message color.
An enumeration of ANSI color codes for styling console output.
enum Colors {
Reset = '\x1b[0m',
Bright = '\x1b[1m',
Dim = '\x1b[2m',
Underscore = '\x1b[4m',
Blink = '\x1b[5m',
Reverse = '\x1b[7m',
Hidden = '\x1b[8m',
FgBlack = '\x1b[30m',
FgRed = '\x1b[31m',
FgGreen = '\x1b[32m',
FgYellow = '\x1b[33m',
FgBlue = '\x1b[34m',
FgMagenta = '\x1b[35m',
FgCyan = '\x1b[36m',
FgWhite = '\x1b[37m',
BgBlack = '\x1b[40m',
BgRed = '\x1b[41m',
BgGreen = '\x1b[42m',
BgYellow = '\x1b[43m',
BgBlue = '\x1b[44m',
BgMagenta = '\x1b[45m',
BgCyan = '\x1b[46m',
BgWhite = '\x1b[47m',
}
You can configure the logger instance by passing a LoggerConfig
object when creating a new instance or by using the configure
method.
// Setting configuration during instantiation
const logger = new Logger({ logPath: '/custom/log/path' });
// Updating configuration later
logger.configure({ logPath: '/another/log/path' });
You can customize individual log messages using LogOptions
.
logger.info('Custom log message', {
name: 'CUSTOM',
logToFile: true,
color: Colors.FgCyan
});
const logger = new Logger();
logger.log('This is a general log message');
logger.info('This is an info message');
logger.success('This is a success message');
logger.warn('This is a warning message');
logger.error('This is an error message');
logger.log('Custom color log message', {
color: Colors.FgMagenta
});
logger.error('This error will throw an exception', {
isThrow: true
});
const logger = new Logger({ logPath: './logs' });
logger.warn('This warning will be logged to a file', {
logToFile: true
});
const logger1 = new Logger({ logPath: './logs/logger1' });
const logger2 = new Logger({ logPath: './logs/logger2' });
logger1.info('Message from logger1', { logToFile: true });
logger2.info('Message from logger2', { logToFile: true });
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
If you have any questions or need further assistance, feel free to open an issue or contact the maintainer.