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 +16 -1
- package/package.json +1 -1
- package/src/index.ts +7 -2
- package/src/logger.ts +15 -8
- package/src/types.ts +4 -2
- package/test/logixlysia.test.ts +10 -6
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
|
-
*
|
|
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
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(
|
|
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 {
|
|
8
|
-
|
|
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 {
|
|
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:
|
|
104
|
+
error: HttpError,
|
|
98
105
|
store: StoreData,
|
|
99
106
|
options?: Options
|
|
100
107
|
): void => {
|
|
101
|
-
const statusCode = error
|
|
108
|
+
const statusCode = error.status || 500
|
|
102
109
|
const logMessage = buildLogMessage(
|
|
103
110
|
'ERROR',
|
|
104
111
|
request,
|
package/src/types.ts
CHANGED
package/test/logixlysia.test.ts
CHANGED
|
@@ -12,9 +12,11 @@ describe('Logixlysia with IP logging enabled', () => {
|
|
|
12
12
|
server = new Elysia()
|
|
13
13
|
.use(
|
|
14
14
|
logger({
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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')
|