Skip to content

A lightweight Node.js logger with structured logging and HTTP middleware, designed to match the format and philosophy of go.uber.org/zap.

License

Notifications You must be signed in to change notification settings

reflective-technology/zap-nodejs

Repository files navigation

@reflective-technology/zap-nodejs

A lightweight Node.js logger with structured logging and Express middleware, designed to match the format and philosophy of go.uber.org/zap.

Features

  • Structured Logging: Log messages with structured fields for better readability and analysis.
  • HTTP Middleware: Built-in middleware for logging HTTP requests and responses.
  • Customizable Levels: Supports multiple log levels (e.g., debug, info, warn, error).

Installation

npm install @reflective-technology/zap-nodejs
# or
yarn add @reflective-technology/zap-nodejs
# or
pnpm add @reflective-technology/zap-nodejs

Usage

Basic Logger

import Logger from '@reflective-technology/zap-nodejs'

const logger = new Logger()

logger.info('This is an info message', { key: 'value' })
logger.error('This is an error message', { error: 'Something went wrong' })

HTTP Middleware

import Logger from '@reflective-technology/zap-nodejs'
import express from 'express'

const app = express()
const logger = new Logger()

app.use(logger.http())

app.get('/', (req, res) => {
  res.send('Hello, world!')
})

app.listen(3000, () => {
  logger.info('Server is running on port 3000')
})

API

Logger

new Logger(logFunction, level)

  • logFunction (optional): A custom function to handle log messages.
  • level (optional): The minimum log level (e.g., debug, info, warn, error).

Methods

  • logger.debug(message, fields): Logs a debug message.
  • logger.info(message, fields): Logs an info message.
  • logger.warn(message, fields): Logs a warning message.
  • logger.error(message, fields): Logs an error message.
  • logger.setLevel(level): Sets the minimum log level.

HTTP Middleware

logger.http()

Returns an Express middleware function that logs HTTP requests and responses.

Configuration

You can configure the logger using environment variables:

  • LOG_LEVEL: Sets the minimum log level (default: info).

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

Links

About

A lightweight Node.js logger with structured logging and HTTP middleware, designed to match the format and philosophy of go.uber.org/zap.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •