@lashdlhaldsfhoq/simpler
v2.0.1
Published
A simple utility to unset console.log arguments and provide enhanced logging capabilities
Maintainers
Readme
Simpler
A powerful utility library for managing console.log behavior and creating enhanced loggers with TypeScript support.
Features
- Console Control: Easily unset and restore console.log functionality
- Enhanced Loggers: Create loggers with timestamps, colors, prefixes, and log levels
- Log Buffering: Buffer logs and flush them when needed
- Performance Timing: Measure and log execution times
- Conditional Logging: Log based on conditions or environment
- Group & Table Support: Use console.group and console.table with enhanced features
- TypeScript Support: Full TypeScript types included
Installation
npm install simplerUsage
Basic Console Control
import { unsetConsoleLog, restoreConsoleLog, simpler } from 'simpler';
// Disable console.log
unsetConsoleLog();
console.log('This will not appear');
// Restore console.log
restoreConsoleLog();
console.log('This will appear');
// Quick disable using simpler()
simpler();Create Enhanced Loggers
import { createLogger } from 'simpler';
// Create a logger with custom options
const logger = createLogger({
timestamp: true,
prefix: 'MyApp',
color: true,
minLevel: 'info' // Only log info and above
});
logger.debug('Debug message'); // Won't show (below minLevel)
logger.info('Info message'); // Will show with timestamp and prefix
logger.error('Error message'); // Will show in red
// Use advanced features
logger.group('Processing');
logger.time('operation');
// ... do something
logger.timeEnd('operation');
logger.groupEnd();
// Display data in table format
logger.table([
{ name: 'John', age: 30 },
{ name: 'Jane', age: 25 }
]);Log Buffering
import { createBufferedLogger } from 'simpler';
const logger = createBufferedLogger({ prefix: 'Buffer' });
// Logs are stored, not printed
logger.log('First message');
logger.error('Error occurred');
logger.info('Info message');
// Flush all logs at once
logger.buffer.flush();
// Clear buffer without flushing
logger.buffer.clear();
// Get buffer entries
const entries = logger.buffer.getEntries();Conditional and Environment Logging
import { conditionalLog, devLog } from 'simpler';
// Log only when condition is true
const debugMode = true;
conditionalLog(debugMode, 'Debug information');
// Log only in development environment
devLog('This only appears when NODE_ENV=development');Utility Functions
import { prefixLog, jsonLog, measureTime, unsetAllConsole, restoreAllConsole } from 'simpler';
// Log with prefix
prefixLog('SERVER', 'Request received');
// Pretty print JSON
const data = { users: [{ name: 'John', age: 30 }] };
jsonLog(data, 4); // 4 spaces indentation
// Measure execution time
const result = await measureTime(async () => {
await someAsyncOperation();
return 'done';
}, 'Operation');
// Logs: "Operation: 123.45ms"
// Disable/restore all console methods
unsetAllConsole(); // Disables log, info, warn, error, debug
restoreAllConsole(); // Restores all methodsAPI Reference
Console Control
unsetConsoleLog()- Disables console.logrestoreConsoleLog()- Restores console.logunsetAllConsole()- Disables all console methodsrestoreAllConsole()- Restores all console methodssimpler()- Quick function to unset console.log
Logger Creation
createLogger(options)- Creates an enhanced loggercreateSilentLogger(options)- Creates a logger and unsets console.logcreateBufferedLogger(options)- Creates a buffered logger
Logger Options
interface LogOptions {
timestamp?: boolean; // Add timestamps to logs
prefix?: string; // Add prefix to all logs
color?: boolean; // Use colors in output
minLevel?: 'debug' | 'log' | 'info' | 'warn' | 'error'; // Minimum log level
}Utility Functions
conditionalLog(condition, ...args)- Log when condition is truedevLog(...args)- Log only in developmentprefixLog(prefix, ...args)- Log with a prefixjsonLog(data, indent?)- Pretty print JSONmeasureTime(fn, label?)- Measure function execution time
License
MIT
