logixlysia 3.1.0 → 3.2.0
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 +7 -0
- package/package.json +1 -1
- package/src/index.ts +5 -4
- package/src/logger/filter.ts +52 -0
- package/src/logger/handle.ts +29 -0
- package/src/{logger.ts → logger/index.ts} +18 -88
- package/src/utils/start.ts +2 -2
- package/test/logixlysia.test.ts +5 -5
- package/test/utils/start.test.ts +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [3.2.0](https://github.com/PunGrumpy/logixlysia/compare/v3.1.0...v3.2.0) (2024-05-07)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* update logger function name for clarity ([9badffc](https://github.com/PunGrumpy/logixlysia/commit/9badffc286fc13feda9eeffd28881990ae437c9f))
|
|
9
|
+
|
|
3
10
|
## [3.1.0](https://github.com/PunGrumpy/logixlysia/compare/v3.0.2...v3.1.0) (2024-04-09)
|
|
4
11
|
|
|
5
12
|
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import Elysia from 'elysia'
|
|
2
|
-
import
|
|
3
|
-
import startString from './utils/start'
|
|
2
|
+
import startServer from './utils/start'
|
|
4
3
|
import { Server } from 'bun'
|
|
5
4
|
import { HttpError, Options } from './types'
|
|
5
|
+
import { createLogger } from './logger'
|
|
6
|
+
import { handleHttpError } from './logger/handle'
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Creates a logger.
|
|
@@ -19,14 +20,14 @@ import { HttpError, Options } from './types'
|
|
|
19
20
|
*
|
|
20
21
|
* @returns {Elysia} The logger.
|
|
21
22
|
*/
|
|
22
|
-
export
|
|
23
|
+
export default function logixlysia(options?: Options): Elysia {
|
|
23
24
|
const log = createLogger(options)
|
|
24
25
|
|
|
25
26
|
const elysia = new Elysia({
|
|
26
27
|
name: 'Logixlysia'
|
|
27
28
|
})
|
|
28
29
|
.onStart(ctx => {
|
|
29
|
-
|
|
30
|
+
startServer(ctx.server as Server)
|
|
30
31
|
})
|
|
31
32
|
.onRequest(ctx => {
|
|
32
33
|
ctx.store = { beforeTime: process.hrtime.bigint() }
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { LogLevel, Options } from '~/types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Filters log messages.
|
|
5
|
+
*
|
|
6
|
+
* @param {LogLevel} logLevel The log level.
|
|
7
|
+
* @param {number} status The status code.
|
|
8
|
+
* @param {string} method The method.
|
|
9
|
+
* @param {Options} options The options.
|
|
10
|
+
* @returns {boolean} `true` if the log message should be logged, otherwise `false`.
|
|
11
|
+
*/
|
|
12
|
+
function filterLog(
|
|
13
|
+
logLevel: LogLevel,
|
|
14
|
+
status: number,
|
|
15
|
+
method: string,
|
|
16
|
+
options?: Options
|
|
17
|
+
): boolean {
|
|
18
|
+
const filter = options?.config?.logFilter
|
|
19
|
+
|
|
20
|
+
if (!filter) return true
|
|
21
|
+
|
|
22
|
+
// Level
|
|
23
|
+
if (filter.level) {
|
|
24
|
+
if (Array.isArray(filter.level)) {
|
|
25
|
+
if (!filter.level.includes(logLevel)) return false
|
|
26
|
+
}
|
|
27
|
+
} else {
|
|
28
|
+
if (filter.level !== logLevel) return false
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Status
|
|
32
|
+
if (filter.status) {
|
|
33
|
+
if (Array.isArray(filter.status)) {
|
|
34
|
+
if (!filter.status.includes(status)) return false
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
if (filter.status !== status) return false
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Method
|
|
41
|
+
if (filter.method) {
|
|
42
|
+
if (Array.isArray(filter.method)) {
|
|
43
|
+
if (!filter.method.includes(method)) return false
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
if (filter.method !== method) return false
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return true
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { filterLog }
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { HttpError, Options, RequestInfo, StoreData } from '~/types'
|
|
2
|
+
import { buildLogMessage } from '.'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Handles an HTTP error.
|
|
6
|
+
*
|
|
7
|
+
* @param {RequestInfo} request The request.
|
|
8
|
+
* @param {HttpError} error The HTTP error.
|
|
9
|
+
* @param {StoreData} store The store data.
|
|
10
|
+
* @param {Options} options The options.
|
|
11
|
+
*/
|
|
12
|
+
function handleHttpError(
|
|
13
|
+
request: RequestInfo,
|
|
14
|
+
error: HttpError,
|
|
15
|
+
store: StoreData,
|
|
16
|
+
options?: Options
|
|
17
|
+
): void {
|
|
18
|
+
const statusCode = error.status || 500
|
|
19
|
+
const logMessage = buildLogMessage(
|
|
20
|
+
'ERROR',
|
|
21
|
+
request,
|
|
22
|
+
{ status: statusCode },
|
|
23
|
+
store,
|
|
24
|
+
options
|
|
25
|
+
)
|
|
26
|
+
console.error(logMessage)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { handleHttpError }
|
|
@@ -1,67 +1,18 @@
|
|
|
1
|
-
import chalk from 'chalk'
|
|
2
|
-
import durationString from './utils/duration'
|
|
3
|
-
import methodString from './utils/method'
|
|
4
|
-
import logString from './utils/log'
|
|
5
|
-
import pathString from './utils/path'
|
|
6
|
-
import statusString from './utils/status'
|
|
7
1
|
import {
|
|
8
|
-
LogLevel,
|
|
9
2
|
LogData,
|
|
10
3
|
Logger,
|
|
11
|
-
|
|
4
|
+
LogLevel,
|
|
12
5
|
Options,
|
|
13
6
|
RequestInfo,
|
|
14
|
-
|
|
15
|
-
} from '
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
* @param {Options} options The options.
|
|
24
|
-
* @returns {boolean} `true` if the log message should be logged, otherwise `false`.
|
|
25
|
-
*/
|
|
26
|
-
function filterLog(
|
|
27
|
-
logLevel: LogLevel,
|
|
28
|
-
status: number,
|
|
29
|
-
method: string,
|
|
30
|
-
options?: Options
|
|
31
|
-
): boolean {
|
|
32
|
-
const filter = options?.config?.logFilter
|
|
33
|
-
|
|
34
|
-
if (!filter) return true
|
|
35
|
-
|
|
36
|
-
// Level
|
|
37
|
-
if (filter.level) {
|
|
38
|
-
if (Array.isArray(filter.level)) {
|
|
39
|
-
if (!filter.level.includes(logLevel)) return false
|
|
40
|
-
}
|
|
41
|
-
} else {
|
|
42
|
-
if (filter.level !== logLevel) return false
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Status
|
|
46
|
-
if (filter.status) {
|
|
47
|
-
if (Array.isArray(filter.status)) {
|
|
48
|
-
if (!filter.status.includes(status)) return false
|
|
49
|
-
}
|
|
50
|
-
} else {
|
|
51
|
-
if (filter.status !== status) return false
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Method
|
|
55
|
-
if (filter.method) {
|
|
56
|
-
if (Array.isArray(filter.method)) {
|
|
57
|
-
if (!filter.method.includes(method)) return false
|
|
58
|
-
}
|
|
59
|
-
} else {
|
|
60
|
-
if (filter.method !== method) return false
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return true
|
|
64
|
-
}
|
|
7
|
+
StoreData
|
|
8
|
+
} from '~/types'
|
|
9
|
+
import { filterLog } from './filter'
|
|
10
|
+
import chalk from 'chalk'
|
|
11
|
+
import logString from '~/utils/log'
|
|
12
|
+
import durationString from '~/utils/duration'
|
|
13
|
+
import methodString from '~/utils/method'
|
|
14
|
+
import pathString from '~/utils/path'
|
|
15
|
+
import statusString from '~/utils/status'
|
|
65
16
|
|
|
66
17
|
/**
|
|
67
18
|
* Logs a message.
|
|
@@ -138,33 +89,12 @@ function buildLogMessage(
|
|
|
138
89
|
* @param {Options} options The options.
|
|
139
90
|
* @returns {Logger} The logger.
|
|
140
91
|
*/
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
log(level, request, data, store
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Handles an HTTP error.
|
|
149
|
-
*
|
|
150
|
-
* @param {RequestInfo} request The request.
|
|
151
|
-
* @param {HttpError} error The HTTP error.
|
|
152
|
-
* @param {StoreData} store The store data.
|
|
153
|
-
* @param {Options} options The options.
|
|
154
|
-
*/
|
|
155
|
-
export const handleHttpError = (
|
|
156
|
-
request: RequestInfo,
|
|
157
|
-
error: HttpError,
|
|
158
|
-
store: StoreData,
|
|
159
|
-
options?: Options
|
|
160
|
-
): void => {
|
|
161
|
-
const statusCode = error.status || 500
|
|
162
|
-
const logMessage = buildLogMessage(
|
|
163
|
-
'ERROR',
|
|
164
|
-
request,
|
|
165
|
-
{ status: statusCode },
|
|
166
|
-
store,
|
|
167
|
-
options
|
|
168
|
-
)
|
|
169
|
-
console.error(logMessage)
|
|
92
|
+
function createLogger(options?: Options): Logger {
|
|
93
|
+
return {
|
|
94
|
+
log: (level, request, data, store) =>
|
|
95
|
+
log(level, request, data, store, options),
|
|
96
|
+
customLogFormat: options?.config?.customLogFormat
|
|
97
|
+
}
|
|
170
98
|
}
|
|
99
|
+
|
|
100
|
+
export { log, buildLogMessage, createLogger }
|
package/src/utils/start.ts
CHANGED
|
@@ -21,7 +21,7 @@ function createBoxText(text: string, width: number): string {
|
|
|
21
21
|
*
|
|
22
22
|
* @returns {void} The server string.
|
|
23
23
|
*/
|
|
24
|
-
function
|
|
24
|
+
function startServer(config: Server): void {
|
|
25
25
|
const { hostname, port, protocol } = config
|
|
26
26
|
const ELYSIA_VERSION = import.meta.require('elysia/package.json').version
|
|
27
27
|
const title = `Elysia v${ELYSIA_VERSION}`
|
|
@@ -41,4 +41,4 @@ function startString(config: Server): void {
|
|
|
41
41
|
`)
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
export default
|
|
44
|
+
export default startServer
|
package/test/logixlysia.test.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Elysia } from 'elysia'
|
|
2
2
|
import { edenTreaty } from '@elysiajs/eden'
|
|
3
3
|
import { describe, it, expect, beforeAll, beforeEach } from 'bun:test'
|
|
4
|
-
import
|
|
4
|
+
import logixlysia from '../src'
|
|
5
5
|
|
|
6
6
|
describe('Logixlysia with IP logging enabled', () => {
|
|
7
7
|
let server: Elysia
|
|
@@ -11,7 +11,7 @@ describe('Logixlysia with IP logging enabled', () => {
|
|
|
11
11
|
beforeAll(() => {
|
|
12
12
|
server = new Elysia()
|
|
13
13
|
.use(
|
|
14
|
-
|
|
14
|
+
logixlysia({
|
|
15
15
|
config: {
|
|
16
16
|
ip: true,
|
|
17
17
|
customLogFormat:
|
|
@@ -70,7 +70,7 @@ describe('Logixlysia with IP logging disabled', () => {
|
|
|
70
70
|
beforeAll(() => {
|
|
71
71
|
server = new Elysia()
|
|
72
72
|
.use(
|
|
73
|
-
|
|
73
|
+
logixlysia({
|
|
74
74
|
config: {
|
|
75
75
|
ip: false,
|
|
76
76
|
customLogFormat:
|
|
@@ -133,7 +133,7 @@ describe('Logixlysia with log filtering enabled', () => {
|
|
|
133
133
|
beforeAll(() => {
|
|
134
134
|
server = new Elysia()
|
|
135
135
|
.use(
|
|
136
|
-
|
|
136
|
+
logixlysia({
|
|
137
137
|
config: {
|
|
138
138
|
logFilter: {
|
|
139
139
|
level: 'INFO',
|
|
@@ -199,7 +199,7 @@ describe('Logixlysia with log filtering disabled', () => {
|
|
|
199
199
|
beforeAll(() => {
|
|
200
200
|
server = new Elysia()
|
|
201
201
|
.use(
|
|
202
|
-
|
|
202
|
+
logixlysia({
|
|
203
203
|
config: {
|
|
204
204
|
logFilter: null
|
|
205
205
|
}
|
package/test/utils/start.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, it, jest } from 'bun:test'
|
|
2
2
|
import { Server } from '~/types'
|
|
3
|
-
import
|
|
3
|
+
import startServer from '~/utils/start'
|
|
4
4
|
|
|
5
5
|
describe('Start String', () => {
|
|
6
6
|
let originalConsoleLog: any
|
|
@@ -23,7 +23,7 @@ describe('Start String', () => {
|
|
|
23
23
|
protocol: 'http'
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
startServer(config)
|
|
27
27
|
|
|
28
28
|
const expectedMessage = `🦊 Elysia is running at http://localhost:3000`
|
|
29
29
|
|