logixlysia 3.5.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 CHANGED
@@ -1,5 +1,13 @@
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
+
3
11
  ## [3.5.0](https://github.com/PunGrumpy/logixlysia/compare/v3.4.0...v3.5.0) (2024-07-11)
4
12
 
5
13
 
package/README.md CHANGED
@@ -25,7 +25,7 @@ const app = new Elysia({
25
25
  ip: true,
26
26
  logFilePath: './logs/example.log',
27
27
  customLogFormat:
28
- '🦊 {now} {level} {duration} {method} {pathname} {status} {message} {ip}',
28
+ '🦊 {now} {level} {duration} {method} {pathname} {status} {message} {ip} {epoch}',
29
29
  logFilter: {
30
30
  level: ['ERROR', 'WARNING'],
31
31
  status: [500, 404],
@@ -38,6 +38,9 @@ const app = new Elysia({
38
38
  app.listen(3000)
39
39
  ```
40
40
 
41
+ > [!NOTE]
42
+ > You can discover more about example in the [example](example) directory.
43
+
41
44
  ## `📚` Documentation
42
45
 
43
46
  ### Options
@@ -50,6 +53,20 @@ app.listen(3000)
50
53
  | `logFilter` | `object` | Filter the logs based on the level, method, and status | `null` |
51
54
  | `logFilePath` | `string` | Path to the log file | `./logs/elysia.log` |
52
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 |
69
+
53
70
  ## `📄` License
54
71
 
55
72
  Licensed under the [MIT License](LICENSE).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "logixlysia",
3
- "version": "3.5.0",
3
+ "version": "3.6.0",
4
4
  "description": "🦊 Logixlysia is a logger for Elysia",
5
5
  "type": "module",
6
6
  "module": "src/index.ts",
@@ -71,25 +71,25 @@
71
71
  ],
72
72
  "dependencies": {
73
73
  "chalk": "^5.3.0",
74
- "elysia": "^1.0.24"
74
+ "elysia": "^1.1.4"
75
75
  },
76
76
  "devDependencies": {
77
- "@elysiajs/eden": "^1.0.14",
78
- "@eslint/js": "^9.5.0",
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.13.1",
81
- "@typescript-eslint/parser": "^7.13.1",
82
- "bun-types": "^1.1.13",
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.x",
86
- "eslint-plugin-simple-import-sort": "^12.1.0",
87
- "husky": "^9.0.11",
85
+ "eslint": "^9.7.0",
86
+ "eslint-plugin-simple-import-sort": "^12.1.1",
87
+ "husky": "^9.1.1",
88
88
  "lint-staged": "^15.2.7",
89
- "prettier": "^3.3.2",
90
- "typescript-eslint": "^7.13.1"
89
+ "prettier": "^3.3.3",
90
+ "typescript-eslint": "^7.17.0"
91
91
  },
92
92
  "peerDependencies": {
93
- "typescript": "^5.0.0"
93
+ "typescript": "^5.2.2"
94
94
  }
95
95
  }
@@ -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(new Date().toLocaleString()))
31
- : new Date().toLocaleString()
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)
@@ -1,3 +1,4 @@
1
+ import { logToTransports } from '~/transports'
1
2
  import {
2
3
  LogData,
3
4
  Logger,
@@ -42,6 +43,8 @@ async function log(
42
43
  options
43
44
  )
44
45
  }
46
+
47
+ await logToTransports(level, request, data, store, options)
45
48
  }
46
49
 
47
50
  /**
@@ -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,7 +69,9 @@ interface Options {
54
69
  method?: string | string[]
55
70
  status?: number | number[]
56
71
  } | null
72
+ ip?: boolean
57
73
  showBanner?: boolean
74
+ transports?: Transport[]
58
75
  }
59
76
  }
60
77
 
@@ -67,5 +84,7 @@ export {
67
84
  Options,
68
85
  RequestInfo,
69
86
  Server,
70
- StoreData
87
+ StoreData,
88
+ Transport,
89
+ TransportFunction
71
90
  }