🚀 Big News:Socket Has Acquired Secure Annex.Learn More
Socket
Book a DemoSign in
Socket

logdrive

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

logdrive

🚀 Minimal, production-grade Node.js logger with automatic request context tracking.

latest
npmnpm
Version
1.0.9
Version published
Maintainers
1
Created
Source

logdrive

🚀 Minimal, production-grade Node.js logger with automatic request context tracking.

Built for real-world apps — structured logs, request tracing, and zero headache setup.

✨ Why logdrive?

  • 🔗 Automatic request context (requestId, IP, user, etc.)
  • 📦 Structured JSON logs (ready for Grafana / Loki)
  • ⚡ Zero-config start
  • 🧠 Handles errors, circular objects, deep data safely
  • 🪵 File rotation built-in

📦 Install

npm install logdrive

🚀 Quick Example (Real-World Express App)

import express from 'express'
import {
  initLogger,
  requestContextMiddleware,
  Log,
  getContext
} from 'logdrive'

const app = express()

// MUST be first
app.use(requestContextMiddleware)

// Attach user to logs
app.use((req, _res, next) => {
  const ctx = getContext()
  if (ctx) {
    ctx.userId = req.headers['x-user-id'] as string
  }
  next()
})

// Initialize logger once
initLogger({
  defaultMeta: { service: 'user-service' },
  logDir: 'logs',
  enableConsole: true
})

app.get('/users', (_req, res) => {
  Log.info('fetch users', { success: true }, 'USR_2001')
  res.json({ ok: true })
})

app.listen(3000, () => {
  Log.notice('server started', { port: 3000 }, 'SRV_0001')
})

🧠 What You Get in Logs (Auto Context)

{
  "message": "fetch users",
  "requestId": "abc-123",
  "ip": "127.0.0.1",
  "method": "GET",
  "url": "/users",
  "userId": "u_42"
}

📚 All Imports

import {
  initLogger,
  Log,
  requestContextMiddleware,
  getContext,
  contextStore
} from 'logdrive'

⚙️ initLogger(config)

initLogger({
  logLevel: 'debug',
  logDir: 'logs',
  maxSize: '20m',
  maxFiles: '1d',
  enableConsole: true,
  defaultMeta: { service: 'api' }
})

Config Options

FieldDefaultDescription
logLeveldebugLogging level
logDirlogsLog folder
maxSize20mFile size before rotation
maxFiles1dRetention
enableConsoleenv-basedConsole logs toggle
defaultMeta{}Added to every log

🧾 Logging API

Log.error('DB failed', new Error('timeout'), 'DB_5001')
Log.warning('Slow query', { ms: 800 }, 'PERF_1001')
Log.notice('Job started', { job: 'sync' }, 'JOB_0001')
Log.info('User login', { userId: 'u1' }, 'AUTH_2001')
Log.debug('Payload', { data: {} }, 'DBG_0001')

Signature

(message: string, data?: unknown, code?: string | number)

🔗 requestContextMiddleware

import { requestContextMiddleware } from 'logdrive'

app.use(requestContextMiddleware)

Auto Injected Fields

  • requestId (or generates one)
  • IP address
  • HTTP method
  • URL
  • userAgent

🧠 getContext()

import { getContext } from 'logdrive'

const ctx = getContext()
if (ctx) {
  ctx.userId = 'user_123'
}

Context Shape

interface RequestContext {
  requestId: string
  ip: string
  method: string
  url: string
  userId?: string
  userAgent?: string
}

🧪 Advanced: contextStore

import { contextStore, Log } from 'logdrive'

contextStore.run(
  {
    requestId: 'manual-id',
    ip: '127.0.0.1',
    method: 'GET',
    url: '/test'
  },
  () => {
    Log.info('manual context log')
  }
)

⚠️ Important Rules

  • Use requestContextMiddleware before routes
  • Call initLogger() before logging
  • Never log before initialization

If not initialized:

Logger not initialized. Call initLogger() first.

💥 Error Handling Example

try {
  throw new Error('DB crashed')
} catch (err) {
  Log.error('critical failure', err, 'SYS_5000')
}

📁 Log File Format

  • JSON logs
  • Rotated daily
  • File format: app-YYYY-MM-DD.log

🧠 Best Use Cases

  • Microservices
  • APIs
  • Backend systems
  • Observability pipelines (Loki / ELK)

👨‍💻 Creator

Pratik Panchal 📧 pratikpanchal2505@gmail.com 🔗 https://github.com/Pratikpanchal25

📄 License (MIT)

MIT License

Copyright (c) 2026 Pratik Panchal

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

🌟 Support

If you like this project:

  • ⭐ Star on GitHub
  • 🐛 Report issues
  • 💡 Suggest features

🚀 Roadmap

  • Express plugin (logdrive.express(app))
  • Loki direct transport
  • Log dashboard (SaaS 👀)
  • Request tracing UI

Keywords

logger

FAQs

Package last updated on 18 Mar 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts