logixlysia 3.7.0 → 4.0.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,11 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## [4.0.0](https://github.com/PunGrumpy/logixlysia/compare/v3.7.0...v4.0.0) (2024-10-28)
4
+
5
+
6
+ ### ⚠ BREAKING CHANGES
7
+
8
+ * **timestamp:** The timestamp format in logs will now respect the new timestamp configuration options when provided
9
+
10
+ ### Features
11
+
12
+ * **timestamp:** add customize timestamp formatting ([5a4e2a5](https://github.com/PunGrumpy/logixlysia/commit/5a4e2a5230443761d8c2ca99d9ebc586ff26a8c0)), closes [#69](https://github.com/PunGrumpy/logixlysia/issues/69)
13
+
3
14
  ## [3.7.0](https://github.com/PunGrumpy/logixlysia/compare/v3.6.2...v3.7.0) (2024-09-26)
4
15
 
5
16
 
6
17
  ### Features
7
18
 
8
- * **optional:** add startup message config ([c7a76db](https://github.com/PunGrumpy/logixlysia/commit/c7a76dbc0f30f4c9c607b52dedc49979e26b67ec))
19
+ * **optional:** add startup message config ([#66](https://github.com/PunGrumpy/logixlysia/pull/66)) ([c7a76db](https://github.com/PunGrumpy/logixlysia/commit/c7a76dbc0f30f4c9c607b52dedc49979e26b67ec)) by [@n0ky4](https://github.com/n0ky4)
9
20
 
10
21
  ## [3.6.2](https://github.com/PunGrumpy/logixlysia/compare/v3.6.1...v3.6.2) (2024-09-18)
11
22
 
package/README.md CHANGED
@@ -23,6 +23,9 @@ const app = new Elysia({
23
23
  config: {
24
24
  showStartupMessage: true,
25
25
  startupMessageFormat: 'simple',
26
+ timestamp: {
27
+ translateTime: 'yyyy-mm-dd HH:MM:ss'
28
+ },
26
29
  ip: true,
27
30
  logFilePath: './logs/example.log',
28
31
  customLogFormat:
@@ -50,6 +53,7 @@ app.listen(3000)
50
53
  | ---------------------- | ------------------------ | --------------------------------------------------------------------- | ------------------------------------------------------------------------- |
51
54
  | `showStartupMessage` | `boolean` | Display the startup message | `true` |
52
55
  | `startupMessageFormat` | `"banner"` \| `"simple"` | Choose the startup message format | `"banner"` |
56
+ | `timestamp` | `object` | Display the timestamp in the logs | `{ translateTime: 'yyyy-mm-dd HH:MM:ss' }` |
53
57
  | `ip` | `boolean` | Display the incoming IP address based on the `X-Forwarded-For` header | `false` |
54
58
  | `customLogMessage` | `string` | Custom log message to display | `🦊 {now} {level} {duration} {method} {pathname} {status} {message} {ip}` |
55
59
  | `logFilter` | `object` | Filter the logs based on the level, method, and status | `null` |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "logixlysia",
3
- "version": "3.7.0",
3
+ "version": "4.0.0",
4
4
  "description": "🦊 Logixlysia is a logger for Elysia",
5
5
  "type": "module",
6
6
  "module": "src/index.ts",
@@ -10,6 +10,12 @@
10
10
  "maintainers": [
11
11
  "PunGrumpy"
12
12
  ],
13
+ "contributors": [
14
+ {
15
+ "name": "n0ky4",
16
+ "url": "https://github.com/n0ky4"
17
+ }
18
+ ],
13
19
  "publishConfig": {
14
20
  "access": "public"
15
21
  },
@@ -71,21 +77,21 @@
71
77
  ],
72
78
  "dependencies": {
73
79
  "chalk": "^5.3.0",
74
- "elysia": "^1.1.16"
80
+ "elysia": "^1.1.19"
75
81
  },
76
82
  "devDependencies": {
77
83
  "@elysiajs/eden": "^1.1.3",
78
- "@eslint/js": "^9.11.1",
84
+ "@eslint/js": "^9.12.0",
79
85
  "@trunkio/launcher": "^1.3.2",
80
- "@typescript-eslint/eslint-plugin": "^8.7.0",
81
- "@typescript-eslint/parser": "^8.7.0",
82
- "bun-types": "^1.1.29",
86
+ "@typescript-eslint/eslint-plugin": "^8.8.1",
87
+ "@typescript-eslint/parser": "^8.8.1",
88
+ "bun-types": "^1.1.30",
83
89
  "eslint-plugin-simple-import-sort": "^12.1.1",
84
- "globals": "^15.9.0",
90
+ "globals": "^15.11.0",
85
91
  "husky": "^9.1.6",
86
92
  "lint-staged": "^15.2.10",
87
93
  "prettier": "^3.3.3",
88
- "typescript-eslint": "^8.7.0"
94
+ "typescript-eslint": "^8.8.1"
89
95
  },
90
96
  "peerDependencies": {
91
97
  "typescript": "^5.2.2"
@@ -10,6 +10,7 @@ import {
10
10
  } from '../types'
11
11
  import {
12
12
  durationString,
13
+ formatTimestamp,
13
14
  logString,
14
15
  methodString,
15
16
  pathString,
@@ -38,14 +39,16 @@ export function buildLogMessage(
38
39
  const now = new Date()
39
40
  const components: LogComponents = {
40
41
  now: actuallyUseColors
41
- ? chalk.bgYellow(chalk.black(now.toLocaleString()))
42
- : now.toLocaleString(),
42
+ ? chalk.bgYellow(
43
+ chalk.black(formatTimestamp(now, options?.config?.timestamp))
44
+ )
45
+ : formatTimestamp(now, options?.config?.timestamp),
43
46
  epoch: Math.floor(now.getTime() / 1000).toString(),
44
- level: logString(level, actuallyUseColors),
45
- duration: durationString(store.beforeTime, actuallyUseColors),
46
- method: methodString(request.method, actuallyUseColors),
47
+ level: logString(level, useColors),
48
+ duration: durationString(store.beforeTime, useColors),
49
+ method: methodString(request.method, useColors),
47
50
  pathname: pathString(request),
48
- status: statusString(data.status || 200, actuallyUseColors),
51
+ status: statusString(data.status || 200, useColors),
49
52
  message: data.message || '',
50
53
  ip:
51
54
  options?.config?.ip && request.headers.get('x-forwarded-for')
@@ -77,6 +77,10 @@ export interface Transport {
77
77
  log: TransportFunction
78
78
  }
79
79
 
80
+ export interface TimestampConfig {
81
+ translateTime?: boolean | string
82
+ }
83
+
80
84
  export interface Options {
81
85
  config?: {
82
86
  customLogFormat?: string
@@ -91,5 +95,6 @@ export interface Options {
91
95
  showStartupMessage?: boolean
92
96
  startupMessageFormat?: 'banner' | 'simple'
93
97
  transports?: Transport[]
98
+ timestamp?: TimestampConfig // Add this new option
94
99
  }
95
100
  }
@@ -4,3 +4,4 @@ export { default as logString } from './log'
4
4
  export { default as methodString } from './method'
5
5
  export { default as pathString } from './path'
6
6
  export { default as statusString } from './status'
7
+ export { formatTimestamp } from './timestamp'
@@ -0,0 +1,48 @@
1
+ import { TimestampConfig } from '../types'
2
+
3
+ // const DEFAULT_TIMESTAMP_FORMAT = 'yyyy-mm-dd HH:MM:ss'
4
+ const SYS_TIME = 'SYS:STANDARD'
5
+
6
+ const pad = (n: number): string => n.toString().padStart(2, '0')
7
+
8
+ function formatSystemTime(date: Date): string {
9
+ const year = date.getFullYear()
10
+ const month = pad(date.getMonth() + 1)
11
+ const day = pad(date.getDate())
12
+ const hours = pad(date.getHours())
13
+ const minutes = pad(date.getMinutes())
14
+ const seconds = pad(date.getSeconds())
15
+ const ms = date.getMilliseconds().toString().padStart(3, '0')
16
+
17
+ return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}.${ms}`
18
+ }
19
+
20
+ function formatCustomTime(date: Date, format: string): string {
21
+ const tokens: { [key: string]: string | number } = {
22
+ yyyy: date.getFullYear(),
23
+ yy: date.getFullYear().toString().slice(-2),
24
+ mm: pad(date.getMonth() + 1),
25
+ dd: pad(date.getDate()),
26
+ HH: pad(date.getHours()),
27
+ MM: pad(date.getMinutes()),
28
+ ss: pad(date.getSeconds()),
29
+ SSS: pad(date.getMilliseconds()),
30
+ Z: -date.getTimezoneOffset() / 60
31
+ }
32
+
33
+ return format.replace(/yyyy|yy|mm|dd|HH|MM|ss|SSS|Z/g, match =>
34
+ (tokens[match] ?? '').toString()
35
+ )
36
+ }
37
+
38
+ export function formatTimestamp(date: Date, config?: TimestampConfig): string {
39
+ if (!config || !config.translateTime) {
40
+ return date.toISOString()
41
+ }
42
+
43
+ if (config.translateTime === true || config.translateTime === SYS_TIME) {
44
+ return formatSystemTime(date)
45
+ }
46
+
47
+ return formatCustomTime(date, config.translateTime)
48
+ }
@@ -0,0 +1,35 @@
1
+ import { expect, test } from 'bun:test'
2
+
3
+ import { formatTimestamp } from '../../src/utils/timestamp'
4
+
5
+ test('formatTimestamp with different configurations', () => {
6
+ const testDate = new Date('2024-01-15T14:30:45.123Z')
7
+
8
+ // Test default format
9
+ expect(formatTimestamp(testDate)).toBe(testDate.toISOString())
10
+
11
+ // Test system time format
12
+ expect(formatTimestamp(testDate, { translateTime: true })).toMatch(
13
+ /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}$/
14
+ )
15
+
16
+ // Test custom format
17
+ expect(
18
+ formatTimestamp(testDate, {
19
+ translateTime: 'yyyy-mm-dd HH:MM:ss.SSS'
20
+ })
21
+ ).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}$/)
22
+
23
+ // Test different custom formats
24
+ expect(
25
+ formatTimestamp(testDate, {
26
+ translateTime: 'HH:MM:ss'
27
+ })
28
+ ).toMatch(/^\d{2}:\d{2}:\d{2}$/)
29
+
30
+ expect(
31
+ formatTimestamp(testDate, {
32
+ translateTime: 'yyyy/mm/dd'
33
+ })
34
+ ).toMatch(/^\d{4}\/\d{2}\/\d{2}$/)
35
+ })