@vrplatform/log 1.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/src/index.ts ADDED
@@ -0,0 +1,107 @@
1
+ import { createBaseLog } from './baselog';
2
+ import {
3
+ AXIOM_AUTH_TOKEN_ENV,
4
+ AXIOM_DATASET_ENV,
5
+ AXIOM_ORG_ID_ENV,
6
+ LOG_ENV,
7
+ TASK_QUEUE_HEADER,
8
+ WORKFLOW_ID_HEADER,
9
+ getCorrelationId,
10
+ isDev,
11
+ requestToContext,
12
+ } from './common';
13
+ import type { RequestLike, WorkerLog } from './type';
14
+
15
+ export * from './common';
16
+ export * from './baselog';
17
+ export * from './type';
18
+
19
+ export function useLog({
20
+ name,
21
+ request,
22
+ body,
23
+ env,
24
+ showStackTrace,
25
+ correlationId: cid,
26
+ version,
27
+ executionContext,
28
+ dataset: _dataset,
29
+ }: {
30
+ name: string;
31
+ request?: RequestLike & { cf?: any };
32
+ body?: any;
33
+ showStackTrace?: boolean;
34
+ correlationId?: string;
35
+ version?: string;
36
+ dataset?: string;
37
+ env?: Record<string, any>;
38
+ executionContext?: {
39
+ waitUntil(promise: Promise<any>): void;
40
+ };
41
+ }): WorkerLog {
42
+ const colorLogs = env?.[LOG_ENV] === '*';
43
+ if (showStackTrace === undefined) showStackTrace = isDev(request);
44
+ const correlationId = cid || getCorrelationId(request); //const logger = nodeColorLog;
45
+
46
+ const axiomToken = env?.[AXIOM_AUTH_TOKEN_ENV];
47
+
48
+ const axiom = createBaseLog(
49
+ {
50
+ token: axiomToken,
51
+ orgId: env?.[AXIOM_ORG_ID_ENV],
52
+ dataset: _dataset || env?.[AXIOM_DATASET_ENV] || 'default',
53
+ consoleLog: colorLogs ? 'color' : false,
54
+ },
55
+ {
56
+ environment: isDev(request) ? 'development' : 'production',
57
+ type: 'worker',
58
+ executionContext,
59
+ workerId: env?.MACHINE_NAME || name || 'default',
60
+ app: name || 'default',
61
+ version: version || 'default',
62
+ correlationId,
63
+ context: {
64
+ rootCorrelationId: correlationId,
65
+ correlationId,
66
+ taskQueue: request?.headers?.get(TASK_QUEUE_HEADER) || undefined,
67
+ workflowId: request?.headers?.get(WORKFLOW_ID_HEADER) || undefined,
68
+ child: undefined as any as string,
69
+ },
70
+ }
71
+ ) as WorkerLog;
72
+
73
+ axiom.info('Request', {
74
+ request: requestToContext(request, body),
75
+ });
76
+
77
+ axiom.respond = async (res) => {
78
+ const temp = await (typeof res === 'function' ? res() : res);
79
+
80
+ const { response, ...arg } =
81
+ temp instanceof Response
82
+ ? {
83
+ body: undefined,
84
+ response: temp,
85
+ status: temp.status,
86
+ statusText: temp.statusText,
87
+ headers: temp.headers,
88
+ }
89
+ : temp;
90
+ const resp =
91
+ response ||
92
+ new Response(
93
+ typeof arg.body === 'object' ? JSON.stringify(arg.body) : arg.body,
94
+ {
95
+ status: arg.status,
96
+ statusText: arg.statusText,
97
+ headers: arg.headers,
98
+ }
99
+ );
100
+ axiom?.info('Response', {
101
+ response,
102
+ });
103
+ await axiom?.flush();
104
+ return resp;
105
+ };
106
+ return axiom;
107
+ }
package/src/type.ts ADDED
@@ -0,0 +1,43 @@
1
+ export type LogFn = (message: any, ...args: any[]) => void;
2
+ export type Log = {
3
+ error: LogFn;
4
+ info: LogFn;
5
+ debug: LogFn;
6
+ warn: LogFn;
7
+ child: (data: ChildLogOptions) => Log;
8
+ addContext: (data: Record<string, any>) => void;
9
+ };
10
+
11
+ export type ChildLogOptions = {
12
+ correlationId?: string;
13
+ name?: string;
14
+ request?: any;
15
+ context?: Record<string, any>;
16
+ };
17
+
18
+ type Res =
19
+ | Response
20
+ | {
21
+ status: number;
22
+ statusText?: string;
23
+ headers?: Record<string, string>;
24
+ body?: any;
25
+ response?: Response;
26
+ };
27
+ export type BaseLog = Log & {
28
+ flush(): Promise<void> | void;
29
+ };
30
+ export type WorkerLog = BaseLog & {
31
+ respond(
32
+ response: Res | Promise<Res> | (() => Res | Promise<Res>)
33
+ ): Promise<Response>;
34
+ };
35
+
36
+ export type RequestLike = {
37
+ headers: {
38
+ get: (key: string) => string | null;
39
+ };
40
+ method: string;
41
+ url: string;
42
+ json: () => Promise<any>;
43
+ };