@cnips/simplelog
v1.0.0
Published
A lightweight, flexible logging library for TypeScript/JavaScript applications with support for multiple log levels and printf-style formatting. This logger is mainly for use in CNIPS componenets using javascript/typescript.
Readme
TypeScript Logger
A lightweight, flexible logging library for TypeScript/JavaScript applications with support for multiple log levels and printf-style formatting. This logger is mainly for use in CNIPS componenets using javascript/typescript.
CommonJS is not supported, this library is designed to be used with ES modules.
Features
- 🎯 Multiple Log Levels: Debug, Info, Warn, and Error
- 📝 Printf-style Formatting: Use format strings with arguments
- 🏷️ Application Tagging: Tag logs with application/component names
- ⏰ Automatic Timestamps: ISO 8601 formatted timestamps
- 💾 In-Memory Storage: Collect and retrieve logs programmatically
- 🎛️ Level Filtering: Only log messages at or above the configured level
Installation
npm install @cnips/loggerQuick Start
import { Logger, levels } from "@cnips/logger";
// Create a logger instance
const logger = new Logger("MyApp", levels.info);
// Log messages at different levels
logger.info("Application started");
logger.warn("This is a warning");
logger.error("Something went wrong");
logger.debug("Debug info"); // Won't be logged (below info level)API Reference
Logger Class
Constructor
new Logger(app: string, level?: number)app: Application or component name (appears in log messages)level: Minimum log level (defaults tolevels.info)
Methods
info(message: string, ...args: any[]): void
Logs an informational message.
logger.info("User %s logged in at %s", username, new Date().toISOString());warn(message: string, ...args: any[]): void
Logs a warning message.
logger.warn("Low disk space: %d%% remaining", diskUsage);error(message: string, ...args: any[]): void
Logs an error message.
logger.error("Failed to connect to database: %s", error.message);debug(message: string, ...args: any[]): void
Logs a debug message.
logger.debug("Processing request with ID: %s", requestId);getLogs(): string[]
Returns all logged messages as an array of strings.
const allLogs = logger.getLogs();
console.log(allLogs);clearLogs(): void
Clears all stored log messages.
logger.clearLogs();string(): string
Returns all logs as a single newline-separated string.
const logString = logger.string();
console.log(logString);Log Levels
export const levels = {
debug: -1, // Most verbose
info: 0, // Default level
warn: 1, // Warnings only
error: 2 // Errors only
}Usage Examples
Basic Logging
import { Logger, levels } from "@cnips/logger";
const logger = new Logger("WebServer", levels.debug);
logger.debug("Server configuration loaded");
logger.info("Server starting on port 3000");
logger.warn("High memory usage detected");
logger.error("Failed to connect to database");Printf-style Formatting
const logger = new Logger("UserService");
const userId = 12345;
const action = "login";
const timestamp = new Date().toISOString();
logger.info("User %d performed action '%s' at %s", userId, action, timestamp);
// Output: 2025-06-30T10:30:45.123Z [UserService] INFO User 12345 performed action 'login' at 2025-06-30T10:30:45.123ZLog Level Filtering
// Only log warnings and errors
const logger = new Logger("CriticalService", levels.warn);
logger.debug("Debug message"); // Won't be logged
logger.info("Info message"); // Won't be logged
logger.warn("Warning message"); // Will be logged
logger.error("Error message"); // Will be loggedCollecting and Processing Logs
const logger = new Logger("DataProcessor");
// Perform some operations
logger.info("Processing started");
logger.warn("Found %d invalid records", 5);
logger.info("Processing completed");
// Get all logs
const logs = logger.getLogs();
logs.forEach(log => console.log(log));
// Get logs as a single string
const logOutput = logger.string();
console.log(logOutput);
// Clear logs for next batch
logger.clearLogs();Log Format
Each log message follows this format:
YYYY-MM-DDTHH:mm:ss.sssZ [AppName] LEVEL MessageExample:
2025-06-30T14:30:45.123Z [MyApp] INFO User authentication successful
2025-06-30T14:30:46.456Z [MyApp] WARN Rate limit approaching for user 12345
2025-06-30T14:30:47.789Z [MyApp] ERROR Database connection failedTypeScript Support
This library is written in TypeScript and includes full type definitions. No additional @types packages are needed.
