sassy-log
Version:
Logging, but with sass, satire, and some serious fun. A developer-first NPM package that replaces boring console.log() statements with snarky, sarcastic, or corporate-smooth one-liners.
212 lines (184 loc) • 4.48 kB
JavaScript
const quotes = require('./quotes');
const { getColorCode, resetColor, supportsColor } = require('./colors');
/**
* Sassy Logger - Main logging class
* Handles all the sass, sarcasm, and personality injection
*/
class SassyLogger {
constructor(options = {}) {
this.mode = options.mode || 'sarcastic';
this.colors = options.colors !== false && supportsColor();
this.timestamps = options.timestamps || false;
this.emojis = options.emojis !== false;
this.customQuotes = options.customQuotes || {};
// Validate mode
if (!quotes[this.mode]) {
console.warn(`⚠️ Unknown mode '${this.mode}'. Falling back to 'sarcastic'.`);
this.mode = 'sarcastic';
}
// Bind methods to preserve context
this.log = this.log.bind(this);
this.info = this.info.bind(this);
this.success = this.success.bind(this);
this.warn = this.warn.bind(this);
this.error = this.error.bind(this);
}
/**
* Get a random quote for the specified type
* @private
*/
_getQuote(type) {
const modeQuotes = this.customQuotes[this.mode] || quotes[this.mode];
const typeQuotes = modeQuotes[type] || modeQuotes.info;
return typeQuotes[Math.floor(Math.random() * typeQuotes.length)];
}
/**
* Format the final log message
* @private
*/
_formatMessage(quote, originalMessage, type) {
let message = '';
// Add timestamp if enabled
if (this.timestamps) {
const timestamp = new Date().toLocaleTimeString();
message += `[${timestamp}] `;
}
// Add emoji based on type
if (this.emojis) {
const emojis = {
info: '💬',
success: '✅',
warn: '⚠️',
error: '❌'
};
message += `${emojis[type]} `;
}
// Add the sassy quote
message += quote;
// Add original message if provided
if (originalMessage) {
message += ` | Original: "${originalMessage}"`;
}
return message;
}
/**
* Apply colors to the message
* @private
*/
_colorize(message, type) {
if (!this.colors) return message;
const color = getColorCode(type);
return `${color}${message}${resetColor}`;
}
/**
* Core logging method
* @private
*/
_doLog(type, originalMessage) {
const quote = this._getQuote(type);
const message = this._formatMessage(quote, originalMessage, type);
const coloredMessage = this._colorize(message, type);
// Use appropriate console method
const consoleMethods = {
info: console.log,
success: console.log,
warn: console.warn,
error: console.error
};
consoleMethods[type](coloredMessage);
}
/**
* Default log (info level)
*/
log(message) {
this._doLog('info', message);
return this;
}
/**
* Info level logging
*/
info(message) {
this._doLog('info', message);
return this;
}
/**
* Success level logging
*/
success(message) {
this._doLog('success', message);
return this;
}
/**
* Warning level logging
*/
warn(message) {
this._doLog('warn', message);
return this;
}
/**
* Error level logging
*/
error(message) {
this._doLog('error', message);
return this;
}
/**
* Switch logging mode
*/
setMode(newMode) {
if (!quotes[newMode]) {
this.warn(`Unknown mode '${newMode}'. Available modes: ${Object.keys(quotes).join(', ')}`);
return this;
}
this.mode = newMode;
this.info(`Mode switched to: ${newMode}`);
return this;
}
/**
* Get current mode
*/
getMode() {
return this.mode;
}
/**
* Get available modes
*/
getAvailableModes() {
return Object.keys(quotes);
}
/**
* Add custom quotes for a mode and type
*/
addCustomQuotes(mode, type, newQuotes) {
if (!this.customQuotes[mode]) {
this.customQuotes[mode] = {};
}
if (!this.customQuotes[mode][type]) {
this.customQuotes[mode][type] = [];
}
this.customQuotes[mode][type].push(...newQuotes);
return this;
}
/**
* Enable/disable colors
*/
setColors(enabled) {
this.colors = enabled && supportsColor();
return this;
}
/**
* Enable/disable timestamps
*/
setTimestamps(enabled) {
this.timestamps = enabled;
return this;
}
/**
* Enable/disable emojis
*/
setEmojis(enabled) {
this.emojis = enabled;
return this;
}
}
module.exports = SassyLogger;