Skip to content

Repository files navigation

NodeJS package

npm versionTypeScriptLicense: MIT

A browser based logger to track events during development and testing. Log entries are written to the console as well as stored in localStorage.

⚠️ v2.0.0 Breaking Changes

  • Event Namespacing: CustomEvents now use i45-logger: prefix
    • Old: window.addEventListener("LOG", ...)
    • New: window.addEventListener("i45-logger:LOG", ...)
  • Property Names: Better, clearer names
    • enableEventsenableDispatchEvents (clearer what it controls)
    • enableLoggingloggingEnabled (better grammar)
  • Removed Exports: iLogger and iLoggerValidator are no longer exported
  • Removed Deprecated: clearEvents() method removed (use clearEventLog(), clearConsole(), or clearAll())
  • Return Types: addClient() and removeClient() return boolean instead of 0|1

See CHANGES.md for full migration guide.

See ROADMAP.md to see future plans.

Environment Support

  • Browser: Full support with all features
  • ⚠️Node.js: Limited support (no localStorage, no events)
  • ⚠️SSR (Next.js, Nuxt): Requires client-side initialization

The package now includes environment detection and won't crash in Node.js environments, but localStorage and CustomEvent features will be unavailable.

Contents

Installation

npm i i45-jslogger

Quick Start

Simple Usage (Default Instance)

importloggerfrom"i45-jslogger";logger.log("Basic log message");logger.info("This is an info message");logger.warn("This is a warning");logger.error("This is an error");

Multiple Instances

import{Logger}from"i45-jslogger";// Separate loggers for different purposesconstdebugLogger=newLogger();constanalyticsLogger=newLogger();debugLogger.info("Debug information");analyticsLogger.suppressConsole=true;// Only log to localStorage and clients

Console output:

Console output

localStorage output:

Local storage

API Overview

Logging Methods

All methods support method chaining and return this.

logger.log("message", ...args);// Basic loglogger.info("message", ...args);// Info levellogger.warn("message", ...args);// Warning levellogger.error("message", ...args);// Error level

Client Management

logger.addClient(customLogger);// Returns: booleanlogger.removeClient(customLogger);// Returns: booleanlogger.clients();// Returns: Arraylogger.removeAllClients();// Returns: this

Event Management

logger.getEvents();// Returns: Array of logged eventslogger.clearEventLog();// Clear localStorage eventslogger.clearConsole();// Clear browser consolelogger.clearAll();// Clear both

Full API documentation available in TypeScript definitions.

Configuration

Properties

// Enable/disable logginglogger.loggingEnabled=true;// Default: true// CustomEvent dispatching to windowlogger.enableDispatchEvents=true;// Default: true// Suppress outputslogger.suppressNative=false;// Skip localStorage/events (Default: false)logger.suppressConsole=false;// Skip console output (Default: false)// Storage limitslogger.maxEvents=null;// Default: null (unlimited)// Set to positive integer to limit stored events (oldest are removed)// Set to 0 to prevent event storage entirely

Console Filtering

Filter by log levels in the browser console:

Logging levels

Custom Logger Clients

Add custom clients to pipe logs to APIs, files, or analytics services.

Client Interface

constcustomLogger={log: (message, ...args)=>{/* custom logic */},info: (message, ...args)=>{/* custom logic */},warn: (message, ...args)=>{/* custom logic */},error: (message, ...args)=>{/* custom logic */},};logger.addClient(customLogger);// Returns true if added successfully

Example: API Logger

constapiLogger={log: (msg, ...args)=>fetch("/api/logs",{method: "POST",body: JSON.stringify({level: "log", msg, args }),}),info: (msg, ...args)=>fetch("/api/logs",{method: "POST",body: JSON.stringify({level: "info", msg, args }),}),warn: (msg, ...args)=>fetch("/api/logs",{method: "POST",body: JSON.stringify({level: "warn", msg, args }),}),error: (msg, ...args)=>fetch("/api/logs",{method: "POST",body: JSON.stringify({level: "error", msg, args }),}),};logger.addClient(apiLogger);logger.info("User logged in",{userId: 123});// Logs to console AND API

Event Listeners

Subscribe to log events using window CustomEvents with namespaced event names.

Available Events

Events are namespaced with i45-logger: prefix to prevent conflicts:

window.addEventListener("i45-logger:LOG",(event)=>{console.log(event.detail);// { message, args, timestamp }});window.addEventListener("i45-logger:INFO",(event)=>{/* ... */});window.addEventListener("i45-logger:WARN",(event)=>{/* ... */});window.addEventListener("i45-logger:ERROR",(event)=>{/* ... */});

Disable Events

logger.enableDispatchEvents=false;// Stop dispatching CustomEvents

Advanced Usage

Use in Classes

importloggerfrom"i45-jslogger";classDataService{
#debug;constructor(debug=false){this.#debug =debug;}fetchData(){if(this.#debug)logger.info("Fetching data...");// ... fetch logicif(this.#debug)logger.info("Data fetched successfully");}}

Multiple Isolated Loggers

import{Logger}from"i45-jslogger";// Development loggerconstdevLog=newLogger();devLog.enableLogging=process.env.NODE_ENV==="development";// Analytics logger (console suppressed)constanalyticsLog=newLogger();analyticsLog.suppressConsole=true;analyticsLog.addClient(analyticsClient);// Error tracking loggerconsterrorLog=newLogger();errorLog.suppressNative=true;errorLog.addClient(sentryClient);

Testing with Isolated Instances

import{Logger}from"i45-jslogger";test("logging disabled",()=>{constlogger=newLogger();logger.enableLogging=false;logger.info("test");// Won't logexpect(logger.getEvents()).toHaveLength(0);});

TypeScript Support

Full TypeScript definitions included:

importlogger,{Logger,LoggerClient,LogEvent}from"i45-jslogger";constcustomClient: LoggerClient={log: (msg)=>console.log(msg),info: (msg)=>console.info(msg),warn: (msg)=>console.warn(msg),error: (msg)=>console.error(msg),};constevents: LogEvent[]=logger.getEvents();

Contributing

Contributions welcome! Please see CHANGES.md for the full changelog and migration guides.

Planning for v3.0.0 is underway - see the v3.0.0 roadmap for upcoming features.

License

MIT

Links

About

A simple JavaScript logger for capturing events in the console and local storage.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages