Winston Logger

Advanced structured logging system for Node.js and Express using Winston. Supports console and file logs, JSON formatting, and request tracking middleware.

loggingwinstonexpressnodemonitoringjson
v2.0.0utility

Overview

A robust, developer-ready logging component built on Winston, providing structured, leveled, and environment-based logging for Express and Node.js apps. Supports file and console transports, request logging middleware, and JSON-structured log output for analysis tools like ELK or Datadog.

Installation

CLI
npx backternity add logger-winston

What This Does

Installs a Winston-based logger with default transports (console and optional file). Includes structured JSON logging and Express middleware for request and error logging.

Files & Folders Created

File / PathDescription
/src/services/logger.jsConfigures Winston logger with transports and formats.
/server.js.exampleDemonstrates request logging and error handling.
/.env.exampleConfigures log level and file output settings.

Files to be modified

File / PathDescription
server.jsIntegrates logger middleware and error tracking.
.envAdds logging configuration (level, file path, output toggle).

Configuration

LOG_LEVEL=info          
LOG_TO_FILE=true        
LOG_FILE_PATH=./logs/app.log
SERVICE_NAME=api-service
NODE_ENV=development

Frontend Integration

Although this is a backend logger, it supports frontend-compatible structured logs when used in full-stack apps (e.g., for API request metrics). Use with tools like Datadog, Logtail, or custom dashboards to visualize logs.

POST/api/log-event

Send a custom log event from frontend to backend logger.

GET/api/logs/health

Get logging system health and recent stats.

GET/api/logs/recent

Retrieve recent log entries (last 50).

GET/

Root route to confirm the logger is active.

GET/error

Simulates a server error and logs it through Winston.

Example

1// app/components/LogEventButton.jsx (Next.js example) 2'use client'; 3import { useState } from 'react'; 4 5export default function LogEventButton() { 6 const [status, setStatus] = useState(''); 7 8 const sendLog = async () => { 9 setStatus('Sending log...'); 10 try { 11 const res = await fetch('/api/log-event', { 12 method: 'POST', 13 headers: { 'Content-Type': 'application/json' }, 14 body: JSON.stringify({ 15 level: 'info', 16 message: 'User triggered frontend log event', 17 context: { page: '/dashboard', action: 'button_click' } 18 }), 19 }); 20 if (res.ok) setStatus('✅ Log sent successfully'); 21 else setStatus('⚠️ Log send failed'); 22 } catch { 23 setStatus('❌ Failed to send log'); 24 } 25 }; 26 27 return ( 28 <div className="p-4 border border-white/10 rounded-lg bg-black/30"> 29 <button 30 onClick={sendLog} 31 className="bg-emerald-600 hover:bg-emerald-700 text-white font-medium px-4 py-2 rounded" 32 > 33 Send Log Event 34 </button> 35 <p className="text-sm text-gray-400 mt-2">{status}</p> 36 </div> 37 ); 38}

Usage

1// Logging different events 2const logger = require('./src/services/logger'); 3 4// Informational 5logger.info('User login successful', { userId: 123 }); 6 7// Warnings 8logger.warn('High response time', { route: '/api/data', duration: 1200 }); 9 10// Errors 11logger.error('Database connection failed', { service: 'db', retrying: true }); 12 13// Debug logs 14logger.debug('Fetched records', { count: 34, source: 'cache' });

Get in Touch

For partnerships, collaborations, or custom backend components - let’s build something powerful together.

Send us a message

Reach us directly

Email

team@backternity.dev

Quick Answers

Are components free?

Yes, they’re absolutely free to use.

Do you offer custom work?

Absolutely, we build scalable backend systems tailored to your stack.

How often are updates released?

Monthly, with new components, optimizations, and features.

Winston Logger – Utility Component | Backternity