Logger - pacificnm/wiki-ai GitHub Wiki

📝 Logging Implementation

This document describes the current logging setup in the wiki-ai application using Winston.


📁 middleware/logger.js

The application uses a custom Winston logger configuration that provides:

  • Console logging for development
  • File logging for production
  • HTTP request logging via Morgan
  • Structured JSON format for log entries

Key Features

  • ✅ Different log levels (error, warn, info, debug)
  • ✅ Timestamp formatting
  • ✅ Colorized console output for development
  • ✅ Integration with Morgan for HTTP request logging
  • ✅ Custom stream for Morgan to use Winston

🛠️ Usage in server/index.js

import { logger, morganStream } from './middleware/logger.js';
import morgan from 'morgan';

// HTTP request logging
app.use(morgan('combined', { stream: morganStream }));

// Application logging
logger.info('Server starting up');
logger.error('MongoDB connection failed', { error: error.message });

📊 Log Levels

Level Purpose Example Usage
error Application errors and exceptions logger.error('DB connection failed')
warn Warning messages logger.warn('Deprecated API used')
info General information logger.info('Server started')
debug Debug information logger.debug('User data', userData)

🔧 Integration Points

MongoDB Connection

mongoose.connect(process.env.MONGO_URI)
  .then(() => {
    logger.info(`MongoDB connected`);
  })
  .catch((error) => {
    logger.error('MongoDB connection failed', { error: error.message });
  });

Health Check Endpoint

app.get('/', (req, res) => {
  logger.info('Health check requested');
  res.status(200).json({ status: 'ok', message: 'Server running' });
});

📋 Environment Configuration

The logger configuration may vary based on:

  • NODE_ENV (development/production)
  • LOG_LEVEL environment variable
  • File output paths for production logging

🎯 Benefits

  1. Centralized Logging: All logs go through Winston
  2. HTTP Request Tracking: Morgan integration tracks all API calls
  3. Structured Data: JSON format makes logs searchable
  4. Development Friendly: Colorized console output
  5. Production Ready: File output for log aggregation