logixlysia 3.4.0 → 3.6.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 +15 -0
- package/README.md +22 -1
- package/package.json +15 -15
- package/src/index.ts +1 -1
- package/src/logger/buildLogMessage.ts +5 -2
- package/src/logger/createLogger.ts +3 -0
- package/src/transports/index.ts +22 -0
- package/src/types.ts +22 -2
- package/src/utils/start.ts +14 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [3.6.0](https://github.com/PunGrumpy/logixlysia/compare/v3.5.0...v3.6.0) (2024-07-24)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* **custom-log:** add unix epoch timestamp option on `customLogFormat` configuration ([58d3e5b](https://github.com/PunGrumpy/logixlysia/commit/58d3e5b89bfef86aed1f5daa70d9a39982750073)), closes [#56](https://github.com/PunGrumpy/logixlysia/issues/56)
|
|
9
|
+
* **transports:** add custom tranporter support for flexible logging ([69e1b89](https://github.com/PunGrumpy/logixlysia/commit/69e1b8991d323d8463fb81cf5bf1b441f678318b)), closes [#51](https://github.com/PunGrumpy/logixlysia/issues/51)
|
|
10
|
+
|
|
11
|
+
## [3.5.0](https://github.com/PunGrumpy/logixlysia/compare/v3.4.0...v3.5.0) (2024-07-11)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
* **config:** add option to toggle server start banner display ([c975cd6](https://github.com/PunGrumpy/logixlysia/commit/c975cd6da94e39d055cdf7bc43cc1ed1eec4971e)), closes [#52](https://github.com/PunGrumpy/logixlysia/issues/52)
|
|
17
|
+
|
|
3
18
|
## [3.4.0](https://github.com/PunGrumpy/logixlysia/compare/v3.3.2...v3.4.0) (2024-06-17)
|
|
4
19
|
|
|
5
20
|
|
package/README.md
CHANGED
|
@@ -21,9 +21,11 @@ const app = new Elysia({
|
|
|
21
21
|
}).use(
|
|
22
22
|
logixlysia({
|
|
23
23
|
config: {
|
|
24
|
+
showBanner: true,
|
|
24
25
|
ip: true,
|
|
26
|
+
logFilePath: './logs/example.log',
|
|
25
27
|
customLogFormat:
|
|
26
|
-
'🦊 {now} {level} {duration} {method} {pathname} {status} {message} {ip}',
|
|
28
|
+
'🦊 {now} {level} {duration} {method} {pathname} {status} {message} {ip} {epoch}',
|
|
27
29
|
logFilter: {
|
|
28
30
|
level: ['ERROR', 'WARNING'],
|
|
29
31
|
status: [500, 404],
|
|
@@ -36,15 +38,34 @@ const app = new Elysia({
|
|
|
36
38
|
app.listen(3000)
|
|
37
39
|
```
|
|
38
40
|
|
|
41
|
+
> [!NOTE]
|
|
42
|
+
> You can discover more about example in the [example](example) directory.
|
|
43
|
+
|
|
39
44
|
## `📚` Documentation
|
|
40
45
|
|
|
41
46
|
### Options
|
|
42
47
|
|
|
43
48
|
| Option | Type | Description | Default |
|
|
44
49
|
| ------------------ | --------- | --------------------------------------------------------------------- | ------------------------------------------------------------------------- |
|
|
50
|
+
| `showBanner` | `boolean` | Display the banner on the console | `true` |
|
|
45
51
|
| `ip` | `boolean` | Display the incoming IP address based on the `X-Forwarded-For` header | `false` |
|
|
46
52
|
| `customLogMessage` | `string` | Custom log message to display | `🦊 {now} {level} {duration} {method} {pathname} {status} {message} {ip}` |
|
|
47
53
|
| `logFilter` | `object` | Filter the logs based on the level, method, and status | `null` |
|
|
54
|
+
| `logFilePath` | `string` | Path to the log file | `./logs/elysia.log` |
|
|
55
|
+
|
|
56
|
+
### Custom Log Message
|
|
57
|
+
|
|
58
|
+
| Placeholder | Description |
|
|
59
|
+
| ------------ | --------------------------------------------------------------------------- |
|
|
60
|
+
| `{now}` | Current date and time in `YYYY-MM-DD HH:mm:ss` format |
|
|
61
|
+
| `{level}` | Log level (`INFO`, `WARNING`, `ERROR`) |
|
|
62
|
+
| `{duration}` | Request duration in milliseconds |
|
|
63
|
+
| `{method}` | Request method (`GET`, `POST`, `PUT`, `DELETE`, `PATCH`, `HEAD`, `OPTIONS`) |
|
|
64
|
+
| `{pathname}` | Request pathname |
|
|
65
|
+
| `{status}` | Response status code |
|
|
66
|
+
| `{message}` | Custom message |
|
|
67
|
+
| `{ip}` | Incoming IP address |
|
|
68
|
+
| `{epoch}` | Current date and time in Unix epoch format (seconds since January 1, 1970 |
|
|
48
69
|
|
|
49
70
|
## `📄` License
|
|
50
71
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "logixlysia",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0",
|
|
4
4
|
"description": "🦊 Logixlysia is a logger for Elysia",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "src/index.ts",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"dev": "bun run --watch example/basic.ts",
|
|
24
24
|
"prepare": "husky",
|
|
25
25
|
"lint:staged": "lint-staged",
|
|
26
|
-
"
|
|
26
|
+
"format": "prettier --write --config .trunk/configs/.prettierrc.yaml .",
|
|
27
27
|
"trunk:check": "trunk check",
|
|
28
28
|
"trunk:fmt": "trunk fmt"
|
|
29
29
|
},
|
|
@@ -71,25 +71,25 @@
|
|
|
71
71
|
],
|
|
72
72
|
"dependencies": {
|
|
73
73
|
"chalk": "^5.3.0",
|
|
74
|
-
"elysia": "^1.
|
|
74
|
+
"elysia": "^1.1.4"
|
|
75
75
|
},
|
|
76
76
|
"devDependencies": {
|
|
77
|
-
"@elysiajs/eden": "^1.
|
|
78
|
-
"@eslint/js": "^9.
|
|
77
|
+
"@elysiajs/eden": "^1.1.1",
|
|
78
|
+
"@eslint/js": "^9.7.0",
|
|
79
79
|
"@trunkio/launcher": "^1.3.1",
|
|
80
|
-
"@typescript-eslint/eslint-plugin": "^7.
|
|
81
|
-
"@typescript-eslint/parser": "^7.
|
|
82
|
-
"bun-types": "^1.1.
|
|
80
|
+
"@typescript-eslint/eslint-plugin": "^7.17.0",
|
|
81
|
+
"@typescript-eslint/parser": "^7.17.0",
|
|
82
|
+
"bun-types": "^1.1.20",
|
|
83
83
|
"commitizen": "^4.3.0",
|
|
84
84
|
"cz-conventional-changelog": "^3.3.0",
|
|
85
|
-
"eslint": "9.
|
|
86
|
-
"eslint-plugin-simple-import-sort": "^12.1.
|
|
87
|
-
"husky": "^9.
|
|
88
|
-
"lint-staged": "^15.2.
|
|
89
|
-
"prettier": "^3.
|
|
90
|
-
"typescript-eslint": "^7.
|
|
85
|
+
"eslint": "^9.7.0",
|
|
86
|
+
"eslint-plugin-simple-import-sort": "^12.1.1",
|
|
87
|
+
"husky": "^9.1.1",
|
|
88
|
+
"lint-staged": "^15.2.7",
|
|
89
|
+
"prettier": "^3.3.3",
|
|
90
|
+
"typescript-eslint": "^7.17.0"
|
|
91
91
|
},
|
|
92
92
|
"peerDependencies": {
|
|
93
|
-
"typescript": "^5.
|
|
93
|
+
"typescript": "^5.2.2"
|
|
94
94
|
}
|
|
95
95
|
}
|
package/src/index.ts
CHANGED
|
@@ -25,7 +25,7 @@ export default function logixlysia(options?: Options): Elysia {
|
|
|
25
25
|
return new Elysia({
|
|
26
26
|
name: 'Logixlysia'
|
|
27
27
|
})
|
|
28
|
-
.onStart(ctx => startServer(ctx.server as Server))
|
|
28
|
+
.onStart(ctx => startServer(ctx.server as Server, options))
|
|
29
29
|
.onRequest(ctx => {
|
|
30
30
|
ctx.store = { beforeTime: process.hrtime.bigint() }
|
|
31
31
|
})
|
|
@@ -26,9 +26,11 @@ export function buildLogMessage(
|
|
|
26
26
|
options?: Options,
|
|
27
27
|
useColors: boolean = true
|
|
28
28
|
): string {
|
|
29
|
+
const now = new Date()
|
|
29
30
|
const nowStr = useColors
|
|
30
|
-
? chalk.bgYellow(chalk.black(
|
|
31
|
-
:
|
|
31
|
+
? chalk.bgYellow(chalk.black(now.toLocaleString()))
|
|
32
|
+
: now.toLocaleString()
|
|
33
|
+
const epochStr = Math.floor(now.getTime() / 1000).toString()
|
|
32
34
|
const levelStr = logString(level, useColors)
|
|
33
35
|
const durationStr = durationString(store.beforeTime, useColors)
|
|
34
36
|
const methodStr = methodString(request.method, useColors)
|
|
@@ -46,6 +48,7 @@ export function buildLogMessage(
|
|
|
46
48
|
|
|
47
49
|
return logFormat
|
|
48
50
|
.replace('{now}', nowStr)
|
|
51
|
+
.replace('{epoch}', epochStr)
|
|
49
52
|
.replace('{level}', levelStr)
|
|
50
53
|
.replace('{duration}', durationStr)
|
|
51
54
|
.replace('{method}', methodStr)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { buildLogMessage } from '~/logger/buildLogMessage'
|
|
2
|
+
import { LogData, LogLevel, Options, RequestInfo, StoreData } from '~/types'
|
|
3
|
+
|
|
4
|
+
export async function logToTransports(
|
|
5
|
+
level: LogLevel,
|
|
6
|
+
request: RequestInfo,
|
|
7
|
+
data: LogData,
|
|
8
|
+
store: StoreData,
|
|
9
|
+
options?: Options
|
|
10
|
+
): Promise<void> {
|
|
11
|
+
if (!options?.config?.transports || options.config.transports.length === 0) {
|
|
12
|
+
return
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const message = buildLogMessage(level, request, data, store, options, false)
|
|
16
|
+
|
|
17
|
+
const promises = options.config.transports.map(transport =>
|
|
18
|
+
transport.log(level, message, { request, data, store })
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
await Promise.all(promises)
|
|
22
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -44,9 +44,24 @@ class HttpError extends Error {
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
interface TransportFunction {
|
|
48
|
+
(
|
|
49
|
+
level: LogLevel,
|
|
50
|
+
message: string,
|
|
51
|
+
meta: {
|
|
52
|
+
request: RequestInfo
|
|
53
|
+
data: LogData
|
|
54
|
+
store: StoreData
|
|
55
|
+
}
|
|
56
|
+
): Promise<void> | void
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface Transport {
|
|
60
|
+
log: TransportFunction
|
|
61
|
+
}
|
|
62
|
+
|
|
47
63
|
interface Options {
|
|
48
64
|
config?: {
|
|
49
|
-
ip?: boolean
|
|
50
65
|
customLogFormat?: string
|
|
51
66
|
logFilePath?: string
|
|
52
67
|
logFilter?: {
|
|
@@ -54,6 +69,9 @@ interface Options {
|
|
|
54
69
|
method?: string | string[]
|
|
55
70
|
status?: number | number[]
|
|
56
71
|
} | null
|
|
72
|
+
ip?: boolean
|
|
73
|
+
showBanner?: boolean
|
|
74
|
+
transports?: Transport[]
|
|
57
75
|
}
|
|
58
76
|
}
|
|
59
77
|
|
|
@@ -66,5 +84,7 @@ export {
|
|
|
66
84
|
Options,
|
|
67
85
|
RequestInfo,
|
|
68
86
|
Server,
|
|
69
|
-
StoreData
|
|
87
|
+
StoreData,
|
|
88
|
+
Transport,
|
|
89
|
+
TransportFunction
|
|
70
90
|
}
|
package/src/utils/start.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Server } from '~/types'
|
|
1
|
+
import { Options, Server } from '~/types'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Creates a box text.
|
|
@@ -19,15 +19,18 @@ function createBoxText(text: string, width: number): string {
|
|
|
19
19
|
* @param {Server} config The server configuration.
|
|
20
20
|
* @returns {void} The server string.
|
|
21
21
|
*/
|
|
22
|
-
function startServer(config: Server): void {
|
|
22
|
+
function startServer(config: Server, options?: Options): void {
|
|
23
23
|
const { hostname, port, protocol } = config
|
|
24
|
-
const
|
|
25
|
-
const title = `Elysia v${ELYSIA_VERSION}`
|
|
26
|
-
const message = `🦊 Elysia is running at ${protocol}://${hostname}:${port}`
|
|
27
|
-
const boxWidth = Math.max(title.length, message.length) + 4
|
|
28
|
-
const border = '─'.repeat(boxWidth)
|
|
24
|
+
const showBanner = options?.config?.showBanner ?? true
|
|
29
25
|
|
|
30
|
-
|
|
26
|
+
if (showBanner) {
|
|
27
|
+
const ELYSIA_VERSION = import.meta.require('elysia/package.json').version
|
|
28
|
+
const title = `Elysia v${ELYSIA_VERSION}`
|
|
29
|
+
const message = `🦊 Elysia is running at ${protocol}://${hostname}:${port}`
|
|
30
|
+
const boxWidth = Math.max(title.length, message.length) + 4
|
|
31
|
+
const border = '─'.repeat(boxWidth)
|
|
32
|
+
|
|
33
|
+
console.log(`
|
|
31
34
|
┌${border}┐
|
|
32
35
|
│${createBoxText('', boxWidth)}│
|
|
33
36
|
│${createBoxText(title, boxWidth)}│
|
|
@@ -36,6 +39,9 @@ function startServer(config: Server): void {
|
|
|
36
39
|
│${createBoxText('', boxWidth)}│
|
|
37
40
|
└${border}┘
|
|
38
41
|
`)
|
|
42
|
+
} else {
|
|
43
|
+
console.log(`🦊 Elysia is running at ${protocol}://${hostname}:${port}`)
|
|
44
|
+
}
|
|
39
45
|
}
|
|
40
46
|
|
|
41
47
|
export default startServer
|