@fullstackhouse/nestjs-betterauth-logger
v0.1.0
Published
NestJS logger plugin for Better Auth - structured HTTP request logging
Maintainers
Readme
@fullstackhouse/nestjs-betterauth-logger
NestJS logger plugin for Better Auth - structured HTTP request logging with automatic log level selection.
Features
- 🔍 Structured logging of all Better Auth HTTP requests
- ⚡ Request timing (response time in milliseconds)
- 🎯 Automatic log level selection (error for 5xx, warn for 4xx, info for 2xx/3xx)
- 🌐 Client IP and User-Agent tracking
- 🧪 Easy to disable for testing environments
- 🔧 Custom NestJS logger support
Installation
npm install @fullstackhouse/nestjs-betterauth-loggerUsage
Basic Usage
import { Logger } from '@nestjs/common';
import { betterAuth } from 'better-auth';
import { authLoggerPlugin } from '@fullstackhouse/nestjs-betterauth-logger';
export const auth = betterAuth({
// ... other config
plugins: [
authLoggerPlugin(), // Uses default Logger('BetterAuth')
],
});With Custom Logger
const customLogger = new Logger('CustomAuth');
export const auth = betterAuth({
plugins: [
authLoggerPlugin({ logger: customLogger }),
],
});Disable in Tests
export const auth = betterAuth({
plugins: [
authLoggerPlugin({
disabled: process.env.NODE_ENV === 'test'
}),
],
});Log Format
Logs are formatted as:
-> {METHOD} {PATH} {STATUS_CODE} {RESPONSE_TIME}ms [{IP}] "{USER_AGENT}"Example Output
-> POST /auth/sign-in 200 45ms [192.168.1.1] "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0"
-> POST /auth/sign-up 400 23ms [192.168.1.2] "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)"
-> GET /auth/session 500 102ms [192.168.1.3] "PostmanRuntime/7.32.0"API
authLoggerPlugin(options?)
Creates a Better Auth plugin that logs HTTP requests using NestJS Logger.
Options
interface LoggerPluginOptions {
/**
* Custom NestJS logger instance
* @default new Logger('BetterAuth')
*/
logger?: Logger;
/**
* Disable logging completely
* Useful for test environments
* @default false
*/
disabled?: boolean;
}Log Levels
The plugin automatically selects appropriate log levels based on HTTP status codes:
- Error (5xx): Server errors
- Warn (4xx): Client errors
- Info (2xx, 3xx): Successful requests and redirects
License
MIT
Repository
https://github.com/fullstackhouse/nestjs-betterauth-logger
