From 27d80eb6b0c3a4a40a96738b235d9bc6b117c804 Mon Sep 17 00:00:00 2001 From: PunGrumpy <108584943+PunGrumpy@users.noreply.github.com> Date: Tue, 9 Apr 2024 00:24:15 +0700 Subject: [PATCH] fix(logger): add `config` property for logging configuration Adding `config` property to the `Options` interface to enhance clearity and consistency --- example/basic.ts | 8 +++++--- src/logger.ts | 17 ++++++++++++----- src/types.ts | 6 ++++-- test/logixlysia.test.ts | 16 ++++++++++------ 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/example/basic.ts b/example/basic.ts index 5f2cf63..3e5beb5 100644 --- a/example/basic.ts +++ b/example/basic.ts @@ -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('/', () => { diff --git a/src/logger.ts b/src/logger.ts index 2d850d3..a127bfa 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -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. @@ -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) @@ -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 }) /** diff --git a/src/types.ts b/src/types.ts index 2d1363f..ff09f33 100644 --- a/src/types.ts +++ b/src/types.ts @@ -45,8 +45,10 @@ class HttpError extends Error { } interface Options { - ip?: boolean - customLogFormat?: string + config?: { + ip?: boolean + customLogFormat?: string + } } export { diff --git a/test/logixlysia.test.ts b/test/logixlysia.test.ts index b113c78..b949d14 100644 --- a/test/logixlysia.test.ts +++ b/test/logixlysia.test.ts @@ -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 => { @@ -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')