NestJS logger plugin for Better Auth - structured HTTP request logging
npm install @fullstackhouse/nestjs-betterauth-loggerNestJS logger plugin for Better Auth - structured HTTP request logging with automatic log level selection.
- ๐ 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
``bash`
npm install @fullstackhouse/nestjs-betterauth-logger
`typescript
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')
],
});
`
`typescript
const customLogger = new Logger('CustomAuth');
export const auth = betterAuth({
plugins: [
authLoggerPlugin({ logger: customLogger }),
],
});
`
`typescript`
export const auth = betterAuth({
plugins: [
authLoggerPlugin({
disabled: process.env.NODE_ENV === 'test'
}),
],
});
Logs are formatted as:
``
-> {METHOD} {PATH} {STATUS_CODE} {RESPONSE_TIME}ms [{IP}] "{USER_AGENT}"
``
-> 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"
Creates a Better Auth plugin that logs HTTP requests using NestJS Logger.
#### Options
`typescript
interface LoggerPluginOptions {
/**
* Custom NestJS logger instance
* @default new Logger('BetterAuth')
*/
logger?: Logger;
/**
* Disable logging completely
* Useful for test environments
* @default false
*/
disabled?: boolean;
}
``
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
MIT
https://github.com/fullstackhouse/nestjs-betterauth-logger