logixlysia 3.0.2 → 3.1.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 +12 -0
- package/README.md +1 -0
- package/package.json +1 -1
- package/src/logger.ts +53 -0
- package/src/types.ts +5 -0
- package/test/logixlysia.test.ts +103 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [3.1.0](https://github.com/PunGrumpy/logixlysia/compare/v3.0.2...v3.1.0) (2024-04-09)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* **logger:** add log filtering functional ([af7d9ec](https://github.com/PunGrumpy/logixlysia/commit/af7d9ec5b2bb5ff5942d1a586be23a9d23a2c1cf)), closes [#38](https://github.com/PunGrumpy/logixlysia/issues/38)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* CI/CD GitHub Actions ([3c40092](https://github.com/PunGrumpy/logixlysia/commit/3c40092bd0ac7bf8bd601a299453cf5080224234))
|
|
14
|
+
|
|
3
15
|
## [3.0.2](https://github.com/PunGrumpy/logixlysia/compare/v3.0.1...v3.0.2) (2024-04-08)
|
|
4
16
|
|
|
5
17
|
|
package/README.md
CHANGED
|
@@ -37,6 +37,7 @@ app.listen(3000)
|
|
|
37
37
|
| ------------------ | --------- | --------------------------------------------------------------------- | ------------------------------------------------------------------------- |
|
|
38
38
|
| `ip` | `boolean` | Display the incoming IP address based on the `X-Forwarded-For` header | `false` |
|
|
39
39
|
| `customLogMessage` | `string` | Custom log message to display | `🦊 {now} {level} {duration} {method} {pathname} {status} {message} {ip}` |
|
|
40
|
+
| `logFilter` | `object` | Filter the logs based on the level, method, and status | `null` |
|
|
40
41
|
|
|
41
42
|
## `📄` License
|
|
42
43
|
|
package/package.json
CHANGED
package/src/logger.ts
CHANGED
|
@@ -14,6 +14,55 @@ import {
|
|
|
14
14
|
HttpError
|
|
15
15
|
} from './types'
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Filters log messages.
|
|
19
|
+
*
|
|
20
|
+
* @param {LogLevel} logLevel The log level.
|
|
21
|
+
* @param {number} status The status code.
|
|
22
|
+
* @param {string} method The method.
|
|
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
|
+
}
|
|
65
|
+
|
|
17
66
|
/**
|
|
18
67
|
* Logs a message.
|
|
19
68
|
*
|
|
@@ -30,6 +79,10 @@ function log(
|
|
|
30
79
|
store: StoreData,
|
|
31
80
|
options?: Options
|
|
32
81
|
): void {
|
|
82
|
+
if (!filterLog(level, data.status || 200, request.method, options)) {
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
|
|
33
86
|
const logMessage = buildLogMessage(level, request, data, store, options)
|
|
34
87
|
console.log(logMessage)
|
|
35
88
|
}
|
package/src/types.ts
CHANGED
package/test/logixlysia.test.ts
CHANGED
|
@@ -124,3 +124,106 @@ describe('Logixlysia with IP logging disabled', () => {
|
|
|
124
124
|
expect(error).toBeInstanceOf(Error)
|
|
125
125
|
})
|
|
126
126
|
})
|
|
127
|
+
|
|
128
|
+
describe('Logixlysia with log filtering enabled', () => {
|
|
129
|
+
let server: Elysia
|
|
130
|
+
let app: any
|
|
131
|
+
let logs: string[] = []
|
|
132
|
+
|
|
133
|
+
beforeAll(() => {
|
|
134
|
+
server = new Elysia()
|
|
135
|
+
.use(
|
|
136
|
+
logger({
|
|
137
|
+
config: {
|
|
138
|
+
logFilter: {
|
|
139
|
+
level: 'INFO',
|
|
140
|
+
status: [200, 404],
|
|
141
|
+
method: 'GET'
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
})
|
|
145
|
+
)
|
|
146
|
+
.get('/', () => '🦊 Logixlysia Getting')
|
|
147
|
+
.post('logixlysia', () => '🦊 Logixlysia Posting')
|
|
148
|
+
.listen(3000)
|
|
149
|
+
|
|
150
|
+
app = edenTreaty<typeof server>('http://127.0.0.1:3000')
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
beforeEach(() => {
|
|
154
|
+
logs = []
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it("Logs 'GET' requests with status 200 or 404 when log filtering criteria are met", async () => {
|
|
158
|
+
const requestCount = 5
|
|
159
|
+
|
|
160
|
+
for (let i = 0; i < requestCount; i++) {
|
|
161
|
+
logs.push((await app.get('/')).data)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
expect(logs.length).toBe(requestCount)
|
|
165
|
+
logs.forEach(log => {
|
|
166
|
+
expect(log).toMatch('🦊 Logixlysia Getting')
|
|
167
|
+
})
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
it("Doesn't log 'POST' requests when log filtering criteria are not met", async () => {
|
|
171
|
+
const requestCount = 5
|
|
172
|
+
|
|
173
|
+
for (let i = 0; i < requestCount; i++) {
|
|
174
|
+
await app.post('/logixlysia', {})
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
expect(logs.length).toBe(0)
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
const otherMethods = ['PUT', 'DELETE', 'PATCH', 'HEAD'] // OPTIONS is failed (IDK why)
|
|
181
|
+
otherMethods.forEach(async method => {
|
|
182
|
+
it(`Logs '${method}' requests with status 200 or 404 when log filtering criteria are met`, async () => {
|
|
183
|
+
const requestCount = 5
|
|
184
|
+
|
|
185
|
+
for (let i = 0; i < requestCount; i++) {
|
|
186
|
+
logs.push((await app[method.toLowerCase()]('/')).data)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
expect(logs.length).toBe(requestCount)
|
|
190
|
+
})
|
|
191
|
+
})
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
describe('Logixlysia with log filtering disabled', () => {
|
|
195
|
+
let server: Elysia
|
|
196
|
+
let app: any
|
|
197
|
+
let logs: string[] = []
|
|
198
|
+
|
|
199
|
+
beforeAll(() => {
|
|
200
|
+
server = new Elysia()
|
|
201
|
+
.use(
|
|
202
|
+
logger({
|
|
203
|
+
config: {
|
|
204
|
+
logFilter: null
|
|
205
|
+
}
|
|
206
|
+
})
|
|
207
|
+
)
|
|
208
|
+
.get('/', () => '🦊 Logixlysia Getting')
|
|
209
|
+
.post('logixlysia', () => '🦊 Logixlysia Posting')
|
|
210
|
+
.listen(3000)
|
|
211
|
+
|
|
212
|
+
app = edenTreaty<typeof server>('http://127.0.0.1:3000')
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
beforeEach(() => {
|
|
216
|
+
logs = []
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
it('Logs all requests when log filtering is disabled', async () => {
|
|
220
|
+
const requestCount = 5
|
|
221
|
+
|
|
222
|
+
for (let i = 0; i < requestCount; i++) {
|
|
223
|
+
logs.push((await app.get('/')).data)
|
|
224
|
+
logs.push((await app.post('/logixlysia', {})).data)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
expect(logs.length).toBe(requestCount * 2)
|
|
228
|
+
})
|
|
229
|
+
})
|