1.0.3 • Published 2 months ago

@specter-labs/fastify-logger v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
2 months ago

Fastify Logger

NPM Downloads NPM License NPM Version JavaScript Style Guide

Advanced HTTP request logging plugin for Fastify with support for custom contexts and flexible configuration.

Install

npm i @specter-labs/fastify-logger

Usage

import Fastify from 'fastify'
import logger from '@specter-labs/fastify-logger'

const fastify = Fastify()

// Register the plugin with default options
await fastify.register(logger)

// Or with custom options
await fastify.register(logger, {
  level: 'info',
  prefix: 'api',
  ignoreStatusCodes: [400],
  adapter: {
    log: (message) => console.log(message),
    error: (message) => console.error(message)
  }
})

Options

OptionDefaultDescription
level'info'The logging level. Can be 'info' or 'error'
prefixundefinedOptional prefix for log messages
ignoreStatusCodes[]Array of HTTP status codes to ignore
ignorePaths[]Array of URL paths to ignore. If a request URL starts with any of these paths, it won't be logged
adapter{ log: fastify.log.info, error: fastify.log.error }Custom logging adapter implementation
logRequestPayloadfalseWhen enabled, logs the request payload for error responses

Custom Adapter

You can provide a custom adapter to handle logging:

const customAdapter = {
  log: (message) => {
    // Handle regular logs
  },
  error: (message) => {
    // Handle error logs
  }
}

await fastify.register(logger, { adapter: customAdapter })

Log Format

The plugin logs requests in the following format:

[prefix] timestamp | statusCode | responseTime | ip | method | url

For errors, the error message is appended:

[prefix] timestamp | statusCode | responseTime | ip | method | url | errorMessage

License

Licensed under MIT.

1.0.3

2 months ago

1.0.2

2 months ago

1.0.1

3 months ago

1.0.0

3 months ago