logixlysia 3.0.0 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,11 +1,26 @@
1
1
  # Changelog
2
2
 
3
+ ## [3.0.2](https://github.com/PunGrumpy/logixlysia/compare/v3.0.1...v3.0.2) (2024-04-08)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * **logger:** add `config` property for logging configuration ([27d80eb](https://github.com/PunGrumpy/logixlysia/commit/27d80eb6b0c3a4a40a96738b235d9bc6b117c804))
9
+
10
+ ## [3.0.1](https://github.com/PunGrumpy/logixlysia/compare/v3.0.0...v3.0.1) (2024-04-08)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * **logger:** use correct status code in error log message ([a3ec49c](https://github.com/PunGrumpy/logixlysia/commit/a3ec49cd3fc1dc7f01a29b21e4de9cf543329c43)), closes [#33](https://github.com/PunGrumpy/logixlysia/issues/33)
16
+
3
17
  ## [3.0.0](https://github.com/PunGrumpy/logixlysia/compare/v2.3.1...v3.0.0) (2024-04-08)
4
18
 
5
19
 
6
20
  ### ⚠ BREAKING CHANGES
7
21
 
8
- * **options:**
22
+ * Added support for custom log format in the logger
23
+ * Enhanced flexibility for define thier preferred log message structure
9
24
 
10
25
  ### Features
11
26
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "logixlysia",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
4
4
  "description": "🦊 Logixlysia is a logger for Elysia",
5
5
  "type": "module",
6
6
  "module": "src/index.ts",
package/src/index.ts CHANGED
@@ -2,7 +2,7 @@ import Elysia from 'elysia'
2
2
  import { createLogger, handleHttpError } from './logger'
3
3
  import startString from './utils/start'
4
4
  import { Server } from 'bun'
5
- import { Options } from './types'
5
+ import { HttpError, Options } from './types'
6
6
 
7
7
  /**
8
8
  * Creates a logger.
@@ -35,7 +35,12 @@ export const logger = (options?: Options): Elysia => {
35
35
  log.log('INFO', request, { status: 200 }, store as { beforeTime: bigint })
36
36
  })
37
37
  .onError({ as: 'global' }, ({ request, error, store }) => {
38
- handleHttpError(request, error, store as { beforeTime: bigint })
38
+ handleHttpError(
39
+ request,
40
+ error as HttpError,
41
+ store as { beforeTime: bigint },
42
+ options
43
+ )
39
44
  })
40
45
 
41
46
  return elysia
package/src/logger.ts CHANGED
@@ -4,8 +4,15 @@ import methodString from './utils/method'
4
4
  import logString from './utils/log'
5
5
  import pathString from './utils/path'
6
6
  import statusString from './utils/status'
7
- import { HttpError, RequestInfo } from './types'
8
- import { LogLevel, LogData, Logger, StoreData, Options } from './types'
7
+ import {
8
+ LogLevel,
9
+ LogData,
10
+ Logger,
11
+ StoreData,
12
+ Options,
13
+ RequestInfo,
14
+ HttpError
15
+ } from './types'
9
16
 
10
17
  /**
11
18
  * Logs a message.
@@ -52,12 +59,12 @@ function buildLogMessage(
52
59
  const statusStr = statusString(data.status || 200)
53
60
  const messageStr = data.message || ''
54
61
  const ipStr =
55
- options?.ip && request.headers.get('x-forwarded-for')
62
+ options?.config?.ip && request.headers.get('x-forwarded-for')
56
63
  ? `IP: ${request.headers.get('x-forwarded-for')}`
57
64
  : ''
58
65
 
59
66
  const logFormat =
60
- options?.customLogFormat ||
67
+ options?.config?.customLogFormat ||
61
68
  '🦊 {now} {level} {duration} {method} {pathname} {status} {message} {ip}'
62
69
  const logMessage = logFormat
63
70
  .replace('{now}', nowStr)
@@ -81,24 +88,24 @@ function buildLogMessage(
81
88
  export const createLogger = (options?: Options): Logger => ({
82
89
  log: (level, request, data, store) =>
83
90
  log(level, request, data, store, options),
84
- customLogFormat: options?.customLogFormat
91
+ customLogFormat: options?.config?.customLogFormat
85
92
  })
86
93
 
87
94
  /**
88
95
  * Handles an HTTP error.
89
96
  *
90
97
  * @param {RequestInfo} request The request.
91
- * @param {Error} error The error.
98
+ * @param {HttpError} error The HTTP error.
92
99
  * @param {StoreData} store The store data.
93
100
  * @param {Options} options The options.
94
101
  */
95
102
  export const handleHttpError = (
96
103
  request: RequestInfo,
97
- error: Error,
104
+ error: HttpError,
98
105
  store: StoreData,
99
106
  options?: Options
100
107
  ): void => {
101
- const statusCode = error instanceof HttpError ? error.status : 500
108
+ const statusCode = error.status || 500
102
109
  const logMessage = buildLogMessage(
103
110
  'ERROR',
104
111
  request,
package/src/types.ts CHANGED
@@ -45,8 +45,10 @@ class HttpError extends Error {
45
45
  }
46
46
 
47
47
  interface Options {
48
- ip?: boolean
49
- customLogFormat?: string
48
+ config?: {
49
+ ip?: boolean
50
+ customLogFormat?: string
51
+ }
50
52
  }
51
53
 
52
54
  export {
@@ -12,9 +12,11 @@ describe('Logixlysia with IP logging enabled', () => {
12
12
  server = new Elysia()
13
13
  .use(
14
14
  logger({
15
- ip: true,
16
- customLogFormat:
17
- '🦊 {now} {duration} {level} {method} {pathname} {status} {message} {ip}'
15
+ config: {
16
+ ip: true,
17
+ customLogFormat:
18
+ '🦊 {now} {duration} {level} {method} {pathname} {status} {message} {ip}'
19
+ }
18
20
  })
19
21
  )
20
22
  .get('/', ctx => {
@@ -69,9 +71,11 @@ describe('Logixlysia with IP logging disabled', () => {
69
71
  server = new Elysia()
70
72
  .use(
71
73
  logger({
72
- ip: false,
73
- customLogFormat:
74
- '🦊 {now} {duration} {level} {method} {pathname} {status} {message} {ip}'
74
+ config: {
75
+ ip: false,
76
+ customLogFormat:
77
+ '🦊 {now} {duration} {level} {method} {pathname} {status} {message} {ip}'
78
+ }
75
79
  })
76
80
  )
77
81
  .get('/', () => '🦊 Logixlysia Getting')