@node-cli/logger

A powerful, flexible console logger for Node.js command-line applications with rich formatting options and in-memory logging capabilities.
Installation
npm install --save-dev @node-cli/logger
Overview
@node-cli/logger provides two main classes:
Logger: A versatile console logger with enhanced formatting, colors, and utility methods
Spinner: An elegant terminal spinner for displaying progress, based on ora
Basic Usage
Logger
import { Logger } from "@node-cli/logger";
const log = new Logger();
log.info("This is an informational message");
log.warn("This is a warning message");
log.error("This is an error message");
log.debug("This is a debug message");
log.log("This is a standard log message");
Spinner
import { Spinner } from "@node-cli/logger";
const spinner = new Spinner("Processing files...");
spinner.start();
spinner.text = "Analyzing data...";
spinner.stop("Process completed successfully!");
spinner.stop("Process failed!", Spinner.ERROR);
spinner.stop("Process needs attention", Spinner.WARNING);
spinner.stop("Process information", Spinner.INFO);
Advanced Logger Examples
Customizing Logger Output
const log = new Logger({
boring: false,
silent: false,
prefix: "MyApp:",
timestamp: true,
inMemory: false
});
log.info("Starting application...");
log.debug("Debug information");
log.warn("Warning: configuration file not found");
log.error("Error: failed to connect to database");
Dynamic Configuration
const log = new Logger();
log.info("Normal colored output");
log.boring = process.env.NODE_ENV === "test";
log.info("No colors in test environment");
log.prefix = "[CONFIG]";
log.info("Loading configuration...");
log.timestamp = true;
log.debug("Detailed timing information");
log.silent = true;
log.info("This won't be displayed");
log.silent = false;
log.info("Logging resumed");
Printing Messages in a Box
log.printBox("Important Message");
log.printBox(["Welcome to MyApp v1.2.3", "Copyright © 2025"]);
log.printBox("WARNING: Disk space low", {
borderColor: "red",
padding: 2,
textAlignment: "center",
title: "System Alert",
newLineAfter: true,
newLineBefore: true
});
Error Handling and Process Exit
const errors = [
"Failed to connect to database",
"Configuration file is invalid",
"Required environment variables missing"
];
log.printErrorsAndExit(errors, 1);
log.printErrorsAndExit(errors);
In-Memory Logging
const memoryLogger = new Logger({ inMemory: true });
memoryLogger.info("Starting process");
memoryLogger.warn("Resource usage high");
memoryLogger.error("Process failed");
const logs = memoryLogger.getMemoryLogs();
console.log(logs);
memoryLogger.clearMemoryLogs();
const log = new Logger();
log.info("This goes to console");
log.inMemory = true;
log.info("This goes to memory only");
const memLogs = log.getMemoryLogs();
log.inMemory = false;
log.info("Back to console logging");
Advanced Spinner Examples
Spinner with Custom Options
const spinner = new Spinner({
text: "Processing...",
color: "blue",
spinner: "dots"
});
spinner.start();
Spinner in Async Functions
import { Spinner } from "@node-cli/logger";
async function deployApplication() {
const spinner = new Spinner("Preparing deployment...");
spinner.start();
try {
spinner.text = "Building application...";
await buildApp();
spinner.text = "Running tests...";
await runTests();
spinner.text = "Deploying to production...";
await deploy();
spinner.stop("Deployment completed successfully!");
return true;
} catch (error) {
spinner.stop(`Deployment failed: ${error.message}`, Spinner.ERROR);
return false;
}
}
Chaining Multiple Spinners
import { Spinner } from "@node-cli/logger";
async function processWorkflow() {
const step1 = new Spinner("Step 1: Data validation");
step1.start();
await validateData();
step1.stop("Data validation complete", Spinner.SUCCESS);
const step2 = new Spinner("Step 2: Processing records");
step2.start();
await processRecords();
step2.stop("Records processed", Spinner.SUCCESS);
const step3 = new Spinner("Step 3: Generating report");
step3.start();
await generateReport();
step3.stop("Report generated", Spinner.SUCCESS);
console.log("Workflow completed successfully!");
}
API Reference
Logger Class
Constructor Options
boring | boolean | false | Disable colors in output |
silent | boolean | false | Disable all logging |
prefix | string | "" | Add a prefix to all log messages |
timestamp | boolean | false | Add timestamps to all log messages |
inMemory | boolean | false | Store logs in memory instead of console |
Methods
log | ...args | Standard log output (white) |
info | ...args | Informational log output (blue) |
debug | ...args | Debug log output (grey) |
warn | ...args | Warning log output (yellow) |
error | ...args | Error log output (red) |
printBox | messages: string | string[], options?: PrintBoxOptions | Display message(s) in a formatted box |
printErrorsAndExit | errorMessages: string[], exitStatus?: number | Display error messages and optionally exit the process |
getMemoryLogs | none | Get all logs stored in memory as a string |
clearMemoryLogs | none | Clear all logs stored in memory |
Properties (Setters)
silent | boolean | Enable/disable logging |
boring | boolean | Enable/disable colors |
prefix | string | Set prefix for log messages |
timestamp | boolean | Enable/disable timestamps |
inMemory | boolean | Enable/disable in-memory logging |
PrintBox Options
newLineAfter | boolean | true | Print a new line after the box |
newLineBefore | boolean | true | Print a new line before the box |
borderColor | string | "yellow" | Color of the box border |
padding | number | 1 | Padding inside the box |
textAlignment | string | "center" | Text alignment ("left", "center", "right") |
title | string | undefined | Optional title for the box |
Plus all options from Boxen.
Spinner Class
Constructor Options
Accepts all options from ora.
text | string | Text to display after the spinner |
color | string | Color of the spinner |
spinner | string | object | Spinner pattern to use |
| ... | ... | All other ora options |
Methods
start | text?: string | Start the spinner with optional text |
stop | text?: string, status?: string | Stop the spinner with optional text and status |
Properties
text | string | Set the spinner text |
Status Constants
Spinner.SUCCESS | "success" | Success status (green checkmark) |
Spinner.ERROR | "fail" | Error status (red X) |
Spinner.WARNING | "warn" | Warning status (yellow exclamation) |
Spinner.INFO | "info" | Info status (blue i) |
Environment Compatibility
- Works with Node.js >=16
- ESM module format
- TypeScript definitions included
License
MIT © Arno Versini