Skip to content

Commit

Permalink
fix(logger): add config property for logging configuration
Browse files Browse the repository at this point in the history
Adding `config` property to the `Options` interface to enhance clearity and consistency
  • Loading branch information
PunGrumpy committed Apr 8, 2024
1 parent bfa33cb commit 27d80eb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
8 changes: 5 additions & 3 deletions example/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ const app = new Elysia({
})
.use(
logger({
ip: true,
customLogFormat:
'🦊 {now} {level} {duration} {method} {pathname} {status} {message} {ip}' // default format
config: {
ip: true,
customLogFormat:
'🦊 {now} {level} {duration} {method} {pathname} {status} {message} {ip}'
}
})
)
.get('/', () => {
Expand Down
17 changes: 12 additions & 5 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ import methodString from './utils/method'
import logString from './utils/log'
import pathString from './utils/path'
import statusString from './utils/status'
import { HttpError, RequestInfo } from './types'
import { LogLevel, LogData, Logger, StoreData, Options } from './types'
import {
LogLevel,
LogData,
Logger,
StoreData,
Options,
RequestInfo,
HttpError
} from './types'

/**
* Logs a message.
Expand Down Expand Up @@ -52,12 +59,12 @@ function buildLogMessage(
const statusStr = statusString(data.status || 200)
const messageStr = data.message || ''
const ipStr =
options?.ip && request.headers.get('x-forwarded-for')
options?.config?.ip && request.headers.get('x-forwarded-for')
? `IP: ${request.headers.get('x-forwarded-for')}`
: ''

const logFormat =
options?.customLogFormat ||
options?.config?.customLogFormat ||
'🦊 {now} {level} {duration} {method} {pathname} {status} {message} {ip}'
const logMessage = logFormat
.replace('{now}', nowStr)
Expand All @@ -81,7 +88,7 @@ function buildLogMessage(
export const createLogger = (options?: Options): Logger => ({
log: (level, request, data, store) =>
log(level, request, data, store, options),
customLogFormat: options?.customLogFormat
customLogFormat: options?.config?.customLogFormat
})

/**
Expand Down
6 changes: 4 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ class HttpError extends Error {
}

interface Options {
ip?: boolean
customLogFormat?: string
config?: {
ip?: boolean
customLogFormat?: string
}
}

export {
Expand Down
16 changes: 10 additions & 6 deletions test/logixlysia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ describe('Logixlysia with IP logging enabled', () => {
server = new Elysia()
.use(
logger({
ip: true,
customLogFormat:
'🦊 {now} {duration} {level} {method} {pathname} {status} {message} {ip}'
config: {
ip: true,
customLogFormat:
'🦊 {now} {duration} {level} {method} {pathname} {status} {message} {ip}'
}
})
)
.get('/', ctx => {
Expand Down Expand Up @@ -69,9 +71,11 @@ describe('Logixlysia with IP logging disabled', () => {
server = new Elysia()
.use(
logger({
ip: false,
customLogFormat:
'🦊 {now} {duration} {level} {method} {pathname} {status} {message} {ip}'
config: {
ip: false,
customLogFormat:
'🦊 {now} {duration} {level} {method} {pathname} {status} {message} {ip}'
}
})
)
.get('/', () => '🦊 Logixlysia Getting')
Expand Down

0 comments on commit 27d80eb

Please sign in to comment.