logixlysia 3.0.1 → 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 CHANGED
@@ -1,5 +1,24 @@
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
+
15
+ ## [3.0.2](https://github.com/PunGrumpy/logixlysia/compare/v3.0.1...v3.0.2) (2024-04-08)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * **logger:** add `config` property for logging configuration ([27d80eb](https://github.com/PunGrumpy/logixlysia/commit/27d80eb6b0c3a4a40a96738b235d9bc6b117c804))
21
+
3
22
  ## [3.0.1](https://github.com/PunGrumpy/logixlysia/compare/v3.0.0...v3.0.1) (2024-04-08)
4
23
 
5
24
 
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "logixlysia",
3
- "version": "3.0.1",
3
+ "version": "3.1.0",
4
4
  "description": "🦊 Logixlysia is a logger for Elysia",
5
5
  "type": "module",
6
6
  "module": "src/index.ts",
package/src/logger.ts CHANGED
@@ -4,8 +4,64 @@ 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'
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
+ }
9
65
 
10
66
  /**
11
67
  * Logs a message.
@@ -23,6 +79,10 @@ function log(
23
79
  store: StoreData,
24
80
  options?: Options
25
81
  ): void {
82
+ if (!filterLog(level, data.status || 200, request.method, options)) {
83
+ return
84
+ }
85
+
26
86
  const logMessage = buildLogMessage(level, request, data, store, options)
27
87
  console.log(logMessage)
28
88
  }
@@ -52,12 +112,12 @@ function buildLogMessage(
52
112
  const statusStr = statusString(data.status || 200)
53
113
  const messageStr = data.message || ''
54
114
  const ipStr =
55
- options?.ip && request.headers.get('x-forwarded-for')
115
+ options?.config?.ip && request.headers.get('x-forwarded-for')
56
116
  ? `IP: ${request.headers.get('x-forwarded-for')}`
57
117
  : ''
58
118
 
59
119
  const logFormat =
60
- options?.customLogFormat ||
120
+ options?.config?.customLogFormat ||
61
121
  '🦊 {now} {level} {duration} {method} {pathname} {status} {message} {ip}'
62
122
  const logMessage = logFormat
63
123
  .replace('{now}', nowStr)
@@ -81,7 +141,7 @@ function buildLogMessage(
81
141
  export const createLogger = (options?: Options): Logger => ({
82
142
  log: (level, request, data, store) =>
83
143
  log(level, request, data, store, options),
84
- customLogFormat: options?.customLogFormat
144
+ customLogFormat: options?.config?.customLogFormat
85
145
  })
86
146
 
87
147
  /**
package/src/types.ts CHANGED
@@ -45,8 +45,15 @@ 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
+ logFilter?: {
52
+ level?: LogLevel | LogLevel[]
53
+ method?: string | string[]
54
+ status?: number | number[]
55
+ } | null
56
+ }
50
57
  }
51
58
 
52
59
  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')
@@ -120,3 +124,106 @@ describe('Logixlysia with IP logging disabled', () => {
120
124
  expect(error).toBeInstanceOf(Error)
121
125
  })
122
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
+ })