elysia-wide-event 0.1.0 → 0.1.2

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/README.md CHANGED
@@ -14,6 +14,7 @@ Inspired by [Logging Sucks](https://loggingsucks.com/) - the wide event pattern
14
14
 
15
15
  ## Features
16
16
 
17
+ - **Server Start Log**: Log custom data on server startup with `start` option
17
18
  - **Context Accumulation**: Collect data throughout request lifecycle via `wideEvent.set()`
18
19
  - **Flexible Output**: Pretty colored output or JSON - you choose
19
20
  - **Request ID**: Auto-generates or extracts from `x-request-id` header
@@ -55,14 +56,16 @@ wideEvent({
55
56
  generateRequestId: () => crypto.randomUUID(),
56
57
  requestIdHeader: "x-request-id",
57
58
  json: false,
59
+ start: { env: "production", version: "1.0.0" },
58
60
  });
59
61
  ```
60
62
 
61
- | Option | Type | Default | Description |
62
- | ------------------- | -------------- | ------------------- | --------------------------------------- |
63
- | `generateRequestId` | `() => string` | `crypto.randomUUID` | Custom request ID generator |
64
- | `requestIdHeader` | `string` | `"x-request-id"` | Header for incoming request ID |
65
- | `json` | `boolean` | `false` | Output as JSON instead of pretty format |
63
+ | Option | Type | Default | Description |
64
+ | ------------------- | -------------- | ------------------- | ----------------------------------------------------- |
65
+ | `generateRequestId` | `() => string` | `crypto.randomUUID` | Custom request ID generator |
66
+ | `requestIdHeader` | `string` | `"x-request-id"` | Header for incoming request ID |
67
+ | `json` | `boolean` | `false` | Output as JSON instead of pretty format |
68
+ | `start` | `LogData` | `undefined` | Custom data to log on server startup (URL auto-added) |
66
69
 
67
70
  ## API
68
71
 
@@ -1,5 +1,5 @@
1
1
  import type { LogData } from "./types";
2
- export declare const colors: {
2
+ declare const colors: {
3
3
  readonly reset: "\u001B[0m";
4
4
  readonly dim: "\u001B[2m";
5
5
  readonly green: "\u001B[32m";
@@ -7,6 +7,7 @@ export declare const colors: {
7
7
  readonly yellow: "\u001B[33m";
8
8
  readonly blue: "\u001B[34m";
9
9
  };
10
- export declare function formatTime(): string;
11
- export declare function formatData(data: LogData): string;
10
+ declare function formatTime(): string;
11
+ declare function formatData(data: LogData): string;
12
+ export { colors, formatTime, formatData };
12
13
  //# sourceMappingURL=formatter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"formatter.d.ts","sourceRoot":"","sources":["../src/formatter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAEvC,eAAO,MAAM,MAAM;;;;;;;CAOT,CAAC;AAEX,wBAAgB,UAAU,IAAI,MAAM,CAEnC;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAIhD"}
1
+ {"version":3,"file":"formatter.d.ts","sourceRoot":"","sources":["../src/formatter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAEvC,QAAA,MAAM,MAAM;;;;;;;CAOF,CAAC;AAEX,iBAAS,UAAU,IAAI,MAAM,CAE5B;AAED,iBAAS,UAAU,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAIzC;AAED,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAGjE,YAAY,EACV,OAAO,EACP,SAAS,EACT,eAAe,EACf,gBAAgB,GACjB,MAAM,SAAS,CAAC;AAEjB,eAAO,MAAM,SAAS,GAAI,UAAS,gBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA0BvD,CAAC;AAEF,eAAe,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAGjE,YAAY,EACV,OAAO,EACP,SAAS,EACT,eAAe,EACf,gBAAgB,GACjB,MAAM,SAAS,CAAC;AAEjB,eAAO,MAAM,SAAS,GAAI,UAAS,gBAAqB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAgCvD,CAAC;AAEF,eAAe,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -85,15 +85,39 @@ ${colors.dim}[${this.headerTime}]${colors.reset} ` + `${colors.yellow}${this.met
85
85
  function createLogger(requestId, method, path, useJson) {
86
86
  return new RequestEventLogger(requestId, method, path, useJson);
87
87
  }
88
+ function logServerStart(serverUrl, userData, useJson) {
89
+ const time = formatTime();
90
+ if (useJson) {
91
+ const record = {
92
+ timestamp: new Date().toISOString(),
93
+ event: "server_start",
94
+ url: serverUrl,
95
+ ...userData
96
+ };
97
+ console.log(JSON.stringify(record));
98
+ } else {
99
+ console.log(`
100
+ ${colors.dim}[${time}]${colors.reset} ` + `${colors.green}SERVER STARTED${colors.reset}`);
101
+ console.log(` ${colors.blue}url:${colors.reset} ${serverUrl}`);
102
+ for (const [key, value] of Object.entries(userData)) {
103
+ console.log(` ${colors.blue}${key}:${colors.reset} ${JSON.stringify(value)}`);
104
+ }
105
+ }
106
+ }
88
107
 
89
108
  // src/index.ts
90
109
  var wideEvent = (options = {}) => {
91
110
  const {
92
111
  generateRequestId = () => crypto.randomUUID(),
93
112
  requestIdHeader = "x-request-id",
94
- json = false
113
+ json = false,
114
+ start
95
115
  } = options;
96
- return new Elysia({ name: "elysia-wide-event" }).derive({ as: "global" }, (ctx) => {
116
+ return new Elysia({ name: "elysia-wide-event" }).onStart(({ server }) => {
117
+ if (start && server) {
118
+ logServerStart(server.url.href, start, json);
119
+ }
120
+ }).derive({ as: "global" }, (ctx) => {
97
121
  const requestId = ctx.request.headers.get(requestIdHeader) ?? generateRequestId();
98
122
  const method = ctx.request.method;
99
123
  const path = new URL(ctx.request.url).pathname;
package/dist/logger.d.ts CHANGED
@@ -1,19 +1,5 @@
1
- import type { LogData, ErrorData, WideEventLogger, FlushableLogger } from "./types";
2
- export declare class RequestEventLogger implements WideEventLogger {
3
- private requestId;
4
- private method;
5
- private path;
6
- private useJson;
7
- private startTime;
8
- private fields;
9
- private errorData?;
10
- private headerTime;
11
- constructor(requestId: string, method: string, path: string, useJson: boolean);
12
- set(key: string, data: LogData): void;
13
- error(error: ErrorData): void;
14
- flush(status: number): void;
15
- private flushDev;
16
- private flushJson;
17
- }
18
- export declare function createLogger(requestId: string, method: string, path: string, useJson: boolean): FlushableLogger;
1
+ import type { LogData, FlushableLogger } from "./types";
2
+ declare function createLogger(requestId: string, method: string, path: string, useJson: boolean): FlushableLogger;
3
+ declare function logServerStart(serverUrl: string, userData: LogData, useJson: boolean): void;
4
+ export { createLogger, logServerStart };
19
5
  //# sourceMappingURL=logger.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,SAAS,EACT,eAAe,EAEf,eAAe,EAChB,MAAM,SAAS,CAAC;AAGjB,qBAAa,kBAAmB,YAAW,eAAe;IAOtD,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,IAAI;IACZ,OAAO,CAAC,OAAO;IATjB,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,MAAM,CAAmC;IACjD,OAAO,CAAC,SAAS,CAAC,CAAY;IAC9B,OAAO,CAAC,UAAU,CAAS;gBAGjB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO;IAM1B,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,IAAI;IAIrC,KAAK,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI;IAI7B,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAU3B,OAAO,CAAC,QAAQ;IAsBhB,OAAO,CAAC,SAAS;CAqBlB;AAED,wBAAgB,YAAY,CAC1B,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO,GACf,eAAe,CAEjB"}
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EAIP,eAAe,EAChB,MAAM,SAAS,CAAC;AAkFjB,iBAAS,YAAY,CACnB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO,GACf,eAAe,CAEjB;AAED,iBAAS,cAAc,CACrB,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,OAAO,EACjB,OAAO,EAAE,OAAO,GACf,IAAI,CAqBN;AAED,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,CAAC"}
package/dist/types.d.ts CHANGED
@@ -13,20 +13,48 @@ export interface WideEventLogger {
13
13
  }
14
14
  export interface WideEventOptions {
15
15
  /**
16
- * Custom request ID generator
16
+ * Custom request ID generator.
17
17
  * @default crypto.randomUUID()
18
+ * @example
19
+ * ```ts
20
+ * wideEvent({
21
+ * generateRequestId: () => `req-${Date.now()}`
22
+ * })
23
+ * ```
18
24
  */
19
25
  generateRequestId?: () => string;
20
26
  /**
21
- * Request ID header name to use from incoming request
27
+ * Header name for incoming request ID.
22
28
  * @default "x-request-id"
29
+ * @example
30
+ * ```ts
31
+ * wideEvent({
32
+ * requestIdHeader: "x-correlation-id"
33
+ * })
34
+ * ```
23
35
  */
24
36
  requestIdHeader?: string;
25
37
  /**
26
- * Output format: true for JSON, false for pretty colored output
38
+ * Output as JSON instead of pretty colored format.
27
39
  * @default false
40
+ * @example
41
+ * ```ts
42
+ * wideEvent({
43
+ * json: true
44
+ * })
45
+ * ```
28
46
  */
29
47
  json?: boolean;
48
+ /**
49
+ * Custom data to log on server startup. Server URL is auto-included.
50
+ * @example
51
+ * ```ts
52
+ * wideEvent({
53
+ * start: { env: "production", version: "1.0.0" }
54
+ * })
55
+ * ```
56
+ */
57
+ start?: LogData;
30
58
  }
31
59
  export interface WideEventRecord {
32
60
  timestamp: string;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,OAAO;IACtB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,eAAe;IAC9B,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,KAAK,IAAI,CAAC;IAC1C,KAAK,EAAE,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,CAAC;CACnC;AAED,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,iBAAiB,CAAC,EAAE,MAAM,MAAM,CAAC;IAEjC;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB;;;OAGG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,SAAS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AAC;CACxB;AAED,MAAM,MAAM,eAAe,GAAG,eAAe,GAAG;IAC9C,KAAK,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;CACjC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elysia-wide-event",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Wide event logging plugin for Elysia - structured logging with request context",
5
5
  "author": "Jay Choi",
6
6
  "license": "MIT",