JSNLog.js

2.30.0 · maintenance · verified Wed Apr 22

JSNLog.js is a compact (2kb min+gz) JavaScript logging library, currently at version 2.30.0, designed for both client-side and Node.js server-side environments. Its primary utility lies in enabling client-side log messages (such as exceptions or AJAX timeouts) to be sent and stored in existing server-side logging infrastructures, including .Net (supporting Log4net, Nlog, Elmah), PHP, and Node.js (via Winston transports). The library is notable for its extensive configuration options, allowing developers to precisely control and filter log data to minimize network traffic and storage, ensuring only relevant information is processed. Despite its utility and robust feature set, the project has been in maintenance mode for approximately five years, with no new releases or significant commits during that period, indicating an inactive development status, though it continues to be downloaded and used in existing projects. It ships with TypeScript type definitions, facilitating its use in modern TypeScript-based applications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates setting up JSNLog for client-side logging, configuring it to send logs to a Node.js server, and integrating with a Winston logger on the server to process and store these client-side messages.

import { JL } from 'jsnlog';
import express from 'express';
import winston from 'winston';
import { JSNLogMiddleware } from 'jsnlog-nodejs'; // Assuming jsnlog-nodejs is installed for server-side

// --- Client-side (in your frontend application) ---
// Configure JSNLog to send logs to the /jsnlog endpoint on your server
JL.setOptions({
    "appenders": [
        {
            "name": "ajaxAppender",
            "type": "ajax",
            "url": "/jsnlog",
            "level": "DEBUG", // Send all debug+ messages
            "bufferSize": 1,
            "batchSize": 5,
            "batchTimeout": 1000
        }
    ],
    "defaultLogger": {
        "appenders": ["ajaxAppender"]
    }
});

console.log('Client-side JSNLog configured. Sending logs to /jsnlog.');

// Example client-side logging
JL().debug('This is a debug message from the client');
JL().info('An informational message from the client');
try {
    throw new Error('Something went wrong on the client!');
} catch (e: any) {
    JL().fatalException('Caught a client-side error!', e);
}

// --- Server-side (in your Node.js application) ---
const app = express();
const PORT = process.env.PORT || 3000;

// Configure Winston logger for server-side logging
const serverLogger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({ filename: 'combined.log' }),
  ],
});

// Integrate JSNLog with Express and Winston
app.use(JSNLogMiddleware(serverLogger));

app.get('/', (req, res) => {
  res.send('JSNLog example running. Check your client console and server logs!');
});

app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
  console.log('Open your browser to http://localhost:3000 and check the network tab/console for client logs being sent.');
  serverLogger.info('Server started and JSNLog middleware active.');
});

// To run this example:
// 1. npm install jsnlog express winston jsnlog-nodejs
// 2. Add "type": "module" to your package.json for ESM support if not already present.
// 3. run `npx ts-node your-file.ts` (if using ts-node) or compile and run.

view raw JSON →