banana-logger
v0.2.0
Published
Banana (logger): A fun and advanced TypeScript logger with formatting, callback, and timing capabilities
Downloads
204
Maintainers
Readme
Banana (Logger) 🍌
Banana (Logger) is a fun and advanced TypeScript logger with formatting, callback, timing, and highlighting capabilities. Every log message includes at least one banana and caller file and line for easier debugging. Inspired by the simplicity of logging but with a twist of humor, Banana Logger ensures your logging experience is both efficient and enjoyable.
Features
- Formatted Logging: Customizable log messages with tags, details, metadata, highlights, and bananas.
- Callbacks: Define custom log handlers to extend functionality.
- Timing: Measure and log the duration of operations.
- Group Logging: Organize logs into groups for better readability.
- Tabular Data Logging: Easily log data in table format.
- Highlighting Keywords: Highlight specific keywords in log messages for better visibility.
- Environment-Specific Logging: Automatically adjust logging levels based on environment (development, staging, production).
Compatibility
| Runtime | Supported | |---------|-----------| | Node.js | ✅ | | Bun | ✅ | | Browser | ✅ | | Deno | ❌ Not tested |
On the server (Node.js / Bun), Banana Logger uses Pino as its logging engine. In the browser, it uses native console.* methods with the same visual format (emoji, timestamp, level). Bundlers that support the exports field in package.json (webpack, Vite, esbuild, Rollup) will automatically select the browser entry point.
Note: In the browser, ANSI highlight colors are not applied — the text is returned as-is.
Installation
npm install banana-loggerUsage
Basic Configuration
import Banana from 'banana-logger';
// Basic configuration
Banana.configure({ tag: 'APP' });
// Simple logging
Banana.info('Application started');Logging with Options
Banana.warn('Warning!', { details: 'Low memory', metadata: 'RAM: 80%' });Using Timer
Banana.time('operation');
// ... perform operation ...
Banana.timeEnd('operation');Using Callback
Banana.setLogCallback((level, message, options) => {
// Send log to external service
});Grouped Logs
Banana.groupStart('Initialization');
Banana.debug('Loading modules...');
Banana.groupEnd();Tabular Data Logging
Banana.tab([{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }]);Adding Blank Lines
Banana.addBlankLine();
Banana.info('This message is after a blank line');Highlighting Keywords
Banana.configure({ highlights: [{ keyword: 'error', style: '31' }] });
Banana.info('This is an error message that should be highlighted');Configuring with Global Metadata
Banana.configure({ metadata: 'https://example.com' });
Banana.info('This log entry includes clickable metadata');Resetting Global Configurations
Banana.reset();
Banana.info('This log entry should not include any global configuration');API
configure(options: { tag?: string; details?: string; metadata?: string; highlights?: HighlightConfig[] })
Configure global options for all logs.
setLogCallback(callback: (level: LogLevel, message: string, options?: LogOptions) => void)
Set a callback function for all log messages.
debug(message: string, options?: LogOptions)
Log a debug message.
info(message: string, options?: LogOptions)
Log an info message.
warn(message: string, options?: LogOptions)
Log a warning message.
error(message: string, options?: LogOptions)
Log an error message.
time(label: string)
Start a timer with the specified label.
timeEnd(label: string)
End a timer and log the elapsed time.
timePromise<T>(label: string, fn: (() => Promise<T>) | Promise<T>): Promise<T>
Measure the execution time of a function or promise.
groupStart(label: string)
Start a new log group.
groupEnd()
End the current log group.
tab(data: Array<unknown> | object)
Display data in a tabular format.
addBlankLine()
Add a blank line to the logs.
applyHighlights(message: string, highlights: HighlightConfig[]): string
Apply highlights to specific keywords in the log message.
isValidUrl(url: string): boolean
Check if a given string is a valid URL.
reset()
Reset global configurations to their default state.
Contributing
Contributions are welcome! Please submit a pull request or open an issue on GitHub.
License
This project is licensed under the MIT License.
Made with ❤️ by trevonerd and a touch of 🍌.
