ts-tiny-logger
Version:
A lightweight TypeScript logger package with Flyweight pattern
77 lines (76 loc) • 2.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
/**
* A simple logger class that implements the Flyweight pattern
* to allow for multiple logger instances
*/
class Logger {
/**
* Private constructor to prevent direct instantiation
*/
constructor() {
this.logs = [];
}
/**
* Get a logger instance with the given identifier
* If an instance with this identifier already exists, it will be returned
* Otherwise, a new instance will be created
*
* @param identifier - Unique identifier for the logger instance
* @returns Logger instance
*/
static getInstance(identifier) {
if (!Logger.instances.has(identifier)) {
Logger.instances.set(identifier, new Logger());
}
return Logger.instances.get(identifier);
}
/**
* Add a new log entry
*
* @param name - Name or category of the log
* @param value - Value or message to be logged
*/
log(name, value) {
const now = new Date();
const dateTime = this.formatDateTime(now);
this.logs.push({
dateTime,
name,
value
});
}
/**
* Get all log entries
*
* @returns Array of log entries
*/
getLogs() {
return [...this.logs];
}
/**
* Clear all log entries
*/
clearLogs() {
this.logs = [];
}
/**
* Format date and time to 'YYYY-MM-DD:hh-mm-ss-ms' format
*
* @param date - Date object to format
* @returns Formatted date and time string
*/
formatDateTime(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
const milliseconds = String(date.getMilliseconds()).padStart(3, '0');
return `${year}-${month}-${day}:${hours}-${minutes}-${seconds}-${milliseconds}`;
}
}
exports.Logger = Logger;
Logger.instances = new Map();