@safaricom-mxl/log 0.0.3
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 +1040 -0
- package/dist/_http-DmaJ426Z.mjs +76 -0
- package/dist/_http-DmaJ426Z.mjs.map +1 -0
- package/dist/_severity-D_IU9-90.mjs +17 -0
- package/dist/_severity-D_IU9-90.mjs.map +1 -0
- package/dist/adapters/axiom.d.mts +64 -0
- package/dist/adapters/axiom.d.mts.map +1 -0
- package/dist/adapters/axiom.mjs +100 -0
- package/dist/adapters/axiom.mjs.map +1 -0
- package/dist/adapters/better-stack.d.mts +63 -0
- package/dist/adapters/better-stack.d.mts.map +1 -0
- package/dist/adapters/better-stack.mjs +98 -0
- package/dist/adapters/better-stack.mjs.map +1 -0
- package/dist/adapters/otlp.d.mts +85 -0
- package/dist/adapters/otlp.d.mts.map +1 -0
- package/dist/adapters/otlp.mjs +196 -0
- package/dist/adapters/otlp.mjs.map +1 -0
- package/dist/adapters/posthog.d.mts +107 -0
- package/dist/adapters/posthog.d.mts.map +1 -0
- package/dist/adapters/posthog.mjs +166 -0
- package/dist/adapters/posthog.mjs.map +1 -0
- package/dist/adapters/sentry.d.mts +80 -0
- package/dist/adapters/sentry.d.mts.map +1 -0
- package/dist/adapters/sentry.mjs +221 -0
- package/dist/adapters/sentry.mjs.map +1 -0
- package/dist/browser.d.mts +63 -0
- package/dist/browser.d.mts.map +1 -0
- package/dist/browser.mjs +95 -0
- package/dist/browser.mjs.map +1 -0
- package/dist/enrichers.d.mts +74 -0
- package/dist/enrichers.d.mts.map +1 -0
- package/dist/enrichers.mjs +172 -0
- package/dist/enrichers.mjs.map +1 -0
- package/dist/error.d.mts +65 -0
- package/dist/error.d.mts.map +1 -0
- package/dist/error.mjs +112 -0
- package/dist/error.mjs.map +1 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.mjs +6 -0
- package/dist/logger.d.mts +46 -0
- package/dist/logger.d.mts.map +1 -0
- package/dist/logger.mjs +287 -0
- package/dist/logger.mjs.map +1 -0
- package/dist/next/client.d.mts +55 -0
- package/dist/next/client.d.mts.map +1 -0
- package/dist/next/client.mjs +44 -0
- package/dist/next/client.mjs.map +1 -0
- package/dist/next/index.d.mts +169 -0
- package/dist/next/index.d.mts.map +1 -0
- package/dist/next/index.mjs +280 -0
- package/dist/next/index.mjs.map +1 -0
- package/dist/nitro/errorHandler.d.mts +15 -0
- package/dist/nitro/errorHandler.d.mts.map +1 -0
- package/dist/nitro/errorHandler.mjs +41 -0
- package/dist/nitro/errorHandler.mjs.map +1 -0
- package/dist/nitro/module.d.mts +11 -0
- package/dist/nitro/module.d.mts.map +1 -0
- package/dist/nitro/module.mjs +23 -0
- package/dist/nitro/module.mjs.map +1 -0
- package/dist/nitro/plugin.d.mts +7 -0
- package/dist/nitro/plugin.d.mts.map +1 -0
- package/dist/nitro/plugin.mjs +145 -0
- package/dist/nitro/plugin.mjs.map +1 -0
- package/dist/nitro/v3/errorHandler.d.mts +24 -0
- package/dist/nitro/v3/errorHandler.d.mts.map +1 -0
- package/dist/nitro/v3/errorHandler.mjs +36 -0
- package/dist/nitro/v3/errorHandler.mjs.map +1 -0
- package/dist/nitro/v3/index.d.mts +5 -0
- package/dist/nitro/v3/index.mjs +5 -0
- package/dist/nitro/v3/middleware.d.mts +25 -0
- package/dist/nitro/v3/middleware.d.mts.map +1 -0
- package/dist/nitro/v3/middleware.mjs +45 -0
- package/dist/nitro/v3/middleware.mjs.map +1 -0
- package/dist/nitro/v3/module.d.mts +10 -0
- package/dist/nitro/v3/module.d.mts.map +1 -0
- package/dist/nitro/v3/module.mjs +22 -0
- package/dist/nitro/v3/module.mjs.map +1 -0
- package/dist/nitro/v3/plugin.d.mts +14 -0
- package/dist/nitro/v3/plugin.d.mts.map +1 -0
- package/dist/nitro/v3/plugin.mjs +162 -0
- package/dist/nitro/v3/plugin.mjs.map +1 -0
- package/dist/nitro/v3/useLogger.d.mts +24 -0
- package/dist/nitro/v3/useLogger.d.mts.map +1 -0
- package/dist/nitro/v3/useLogger.mjs +27 -0
- package/dist/nitro/v3/useLogger.mjs.map +1 -0
- package/dist/nitro-CrFBjY1Y.d.mts +42 -0
- package/dist/nitro-CrFBjY1Y.d.mts.map +1 -0
- package/dist/nitro-Dsv6dSzv.mjs +39 -0
- package/dist/nitro-Dsv6dSzv.mjs.map +1 -0
- package/dist/nuxt/module.d.mts +164 -0
- package/dist/nuxt/module.d.mts.map +1 -0
- package/dist/nuxt/module.mjs +84 -0
- package/dist/nuxt/module.mjs.map +1 -0
- package/dist/pipeline.d.mts +46 -0
- package/dist/pipeline.d.mts.map +1 -0
- package/dist/pipeline.mjs +122 -0
- package/dist/pipeline.mjs.map +1 -0
- package/dist/routes-BNbrnm14.mjs +39 -0
- package/dist/routes-BNbrnm14.mjs.map +1 -0
- package/dist/runtime/client/log.d.mts +15 -0
- package/dist/runtime/client/log.d.mts.map +1 -0
- package/dist/runtime/client/log.mjs +92 -0
- package/dist/runtime/client/log.mjs.map +1 -0
- package/dist/runtime/client/plugin.d.mts +5 -0
- package/dist/runtime/client/plugin.d.mts.map +1 -0
- package/dist/runtime/client/plugin.mjs +17 -0
- package/dist/runtime/client/plugin.mjs.map +1 -0
- package/dist/runtime/server/routes/_mxllog/ingest.post.d.mts +7 -0
- package/dist/runtime/server/routes/_mxllog/ingest.post.d.mts.map +1 -0
- package/dist/runtime/server/routes/_mxllog/ingest.post.mjs +123 -0
- package/dist/runtime/server/routes/_mxllog/ingest.post.mjs.map +1 -0
- package/dist/runtime/server/useLogger.d.mts +39 -0
- package/dist/runtime/server/useLogger.d.mts.map +1 -0
- package/dist/runtime/server/useLogger.mjs +43 -0
- package/dist/runtime/server/useLogger.mjs.map +1 -0
- package/dist/runtime/utils/parseError.d.mts +7 -0
- package/dist/runtime/utils/parseError.d.mts.map +1 -0
- package/dist/runtime/utils/parseError.mjs +29 -0
- package/dist/runtime/utils/parseError.mjs.map +1 -0
- package/dist/types.d.mts +496 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +1 -0
- package/dist/utils.d.mts +34 -0
- package/dist/utils.d.mts.map +1 -0
- package/dist/utils.mjs +78 -0
- package/dist/utils.mjs.map +1 -0
- package/dist/workers.d.mts +46 -0
- package/dist/workers.d.mts.map +1 -0
- package/dist/workers.mjs +81 -0
- package/dist/workers.mjs.map +1 -0
- package/package.json +195 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { TransportConfig } from "../types.mjs";
|
|
2
|
+
import { clearIdentity, log as _clientLog, setIdentity } from "../runtime/client/log.mjs";
|
|
3
|
+
import * as react from "react";
|
|
4
|
+
|
|
5
|
+
//#region src/next/client.d.ts
|
|
6
|
+
interface MxllogProviderProps {
|
|
7
|
+
/**
|
|
8
|
+
* Service name for client-side logs.
|
|
9
|
+
* @default 'client'
|
|
10
|
+
*/
|
|
11
|
+
service?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Enable pretty printing in the browser console.
|
|
14
|
+
* @default true
|
|
15
|
+
*/
|
|
16
|
+
pretty?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Transport configuration for sending client logs to the server.
|
|
19
|
+
*/
|
|
20
|
+
transport?: TransportConfig;
|
|
21
|
+
/**
|
|
22
|
+
* Enable or disable client-side logging.
|
|
23
|
+
* @default true
|
|
24
|
+
*/
|
|
25
|
+
enabled?: boolean;
|
|
26
|
+
children: React.ReactNode;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* React provider that initializes mxllog client-side logging.
|
|
30
|
+
* Place this in your root layout to enable client logging throughout your app.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```tsx
|
|
34
|
+
* // app/layout.tsx
|
|
35
|
+
* import { MxllogProvider } from '@safaricom-mxl/log/next/client'
|
|
36
|
+
*
|
|
37
|
+
* export default function Layout({ children }) {
|
|
38
|
+
* return (
|
|
39
|
+
* <MxllogProvider service="my-app" transport={{ enabled: true }}>
|
|
40
|
+
* {children}
|
|
41
|
+
* </MxllogProvider>
|
|
42
|
+
* )
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
declare function MxllogProvider({
|
|
47
|
+
service,
|
|
48
|
+
pretty,
|
|
49
|
+
transport,
|
|
50
|
+
enabled,
|
|
51
|
+
children
|
|
52
|
+
}: MxllogProviderProps): react.ReactNode;
|
|
53
|
+
//#endregion
|
|
54
|
+
export { MxllogProvider, MxllogProviderProps, clearIdentity, _clientLog as log, setIdentity };
|
|
55
|
+
//# sourceMappingURL=client.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.mts","names":[],"sources":["../../src/next/client.ts"],"mappings":";;;;;UAQiB,mBAAA;;;AAAjB;;EAKE,OAAA;EAmByB;;;;EAbzB,MAAA;EAWA;;;EANA,SAAA,GAAY,eAAA;EAQa;;AAsB3B;;EAxBE,OAAA;EAEA,QAAA,EAAU,KAAA,CAAM,SAAA;AAAA;;;;;;;;;;;;;;;;;;;iBAsBF,cAAA,CAAA;EAAiB,OAAA;EAAS,MAAA;EAAQ,SAAA;EAAW,OAAA;EAAS;AAAA,GAAY,mBAAA,GAAmB,KAAA,CAAA,SAAA"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { clearIdentity, initLog, log as _clientLog, setIdentity } from "../runtime/client/log.mjs";
|
|
4
|
+
import { useEffect } from "react";
|
|
5
|
+
|
|
6
|
+
//#region src/next/client.ts
|
|
7
|
+
/**
|
|
8
|
+
* React provider that initializes mxllog client-side logging.
|
|
9
|
+
* Place this in your root layout to enable client logging throughout your app.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* // app/layout.tsx
|
|
14
|
+
* import { MxllogProvider } from '@safaricom-mxl/log/next/client'
|
|
15
|
+
*
|
|
16
|
+
* export default function Layout({ children }) {
|
|
17
|
+
* return (
|
|
18
|
+
* <MxllogProvider service="my-app" transport={{ enabled: true }}>
|
|
19
|
+
* {children}
|
|
20
|
+
* </MxllogProvider>
|
|
21
|
+
* )
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
function MxllogProvider({ service, pretty, transport, enabled, children }) {
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
initLog({
|
|
28
|
+
enabled,
|
|
29
|
+
pretty,
|
|
30
|
+
service,
|
|
31
|
+
transport
|
|
32
|
+
});
|
|
33
|
+
}, [
|
|
34
|
+
enabled,
|
|
35
|
+
pretty,
|
|
36
|
+
service,
|
|
37
|
+
transport
|
|
38
|
+
]);
|
|
39
|
+
return children;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
export { MxllogProvider, clearIdentity, _clientLog as log, setIdentity };
|
|
44
|
+
//# sourceMappingURL=client.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.mjs","names":[],"sources":["../../src/next/client.ts"],"sourcesContent":["'use client'\n\nimport { useEffect } from 'react'\nimport type { TransportConfig } from '../types'\nimport { initLog, log, setIdentity, clearIdentity } from '../runtime/client/log'\n\nexport { log, setIdentity, clearIdentity } from '../runtime/client/log'\n\nexport interface MxllogProviderProps {\n /**\n * Service name for client-side logs.\n * @default 'client'\n */\n service?: string\n\n /**\n * Enable pretty printing in the browser console.\n * @default true\n */\n pretty?: boolean\n\n /**\n * Transport configuration for sending client logs to the server.\n */\n transport?: TransportConfig\n\n /**\n * Enable or disable client-side logging.\n * @default true\n */\n enabled?: boolean\n\n children: React.ReactNode\n}\n\n/**\n * React provider that initializes mxllog client-side logging.\n * Place this in your root layout to enable client logging throughout your app.\n *\n * @example\n * ```tsx\n * // app/layout.tsx\n * import { MxllogProvider } from '@safaricom-mxl/log/next/client'\n *\n * export default function Layout({ children }) {\n * return (\n * <MxllogProvider service=\"my-app\" transport={{ enabled: true }}>\n * {children}\n * </MxllogProvider>\n * )\n * }\n * ```\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport function MxllogProvider({ service, pretty, transport, enabled, children }: MxllogProviderProps) {\n useEffect(() => {\n initLog({\n enabled,\n pretty,\n service,\n transport,\n })\n }, [enabled, pretty, service, transport])\n\n return children\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAsDA,SAAgB,eAAe,EAAE,SAAS,QAAQ,WAAW,SAAS,YAAiC;AACrG,iBAAgB;AACd,UAAQ;GACN;GACA;GACA;GACA;GACD,CAAC;IACD;EAAC;EAAS;EAAQ;EAAS;EAAU,CAAC;AAEzC,QAAO"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { DrainContext, EnrichContext, EnvironmentContext, Log, RequestLogger, RouteConfig, SamplingConfig, TailSamplingContext } from "../types.mjs";
|
|
2
|
+
import { createError } from "../error.mjs";
|
|
3
|
+
import { log as _log } from "../logger.mjs";
|
|
4
|
+
import "../index.mjs";
|
|
5
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
6
|
+
|
|
7
|
+
//#region src/next/types.d.ts
|
|
8
|
+
interface NextMxllogOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Service name for all logged events.
|
|
11
|
+
* @default auto-detected from SERVICE_NAME env or 'app'
|
|
12
|
+
*/
|
|
13
|
+
service?: string;
|
|
14
|
+
/**
|
|
15
|
+
* Environment context overrides.
|
|
16
|
+
*/
|
|
17
|
+
env?: Partial<EnvironmentContext>;
|
|
18
|
+
/**
|
|
19
|
+
* Enable pretty printing.
|
|
20
|
+
* @default true in development, false in production
|
|
21
|
+
*/
|
|
22
|
+
pretty?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Enable or disable all logging globally.
|
|
25
|
+
* @default true
|
|
26
|
+
*/
|
|
27
|
+
enabled?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Sampling configuration for filtering logs.
|
|
30
|
+
*/
|
|
31
|
+
sampling?: SamplingConfig;
|
|
32
|
+
/**
|
|
33
|
+
* Route patterns to include in logging.
|
|
34
|
+
* Supports glob patterns like '/api/**'.
|
|
35
|
+
* If not set, all routes are logged.
|
|
36
|
+
*/
|
|
37
|
+
include?: string[];
|
|
38
|
+
/**
|
|
39
|
+
* Route patterns to exclude from logging.
|
|
40
|
+
* Supports glob patterns like '/_next/**'.
|
|
41
|
+
* Exclusions take precedence over inclusions.
|
|
42
|
+
*/
|
|
43
|
+
exclude?: string[];
|
|
44
|
+
/**
|
|
45
|
+
* Route-specific service configuration.
|
|
46
|
+
*/
|
|
47
|
+
routes?: Record<string, RouteConfig>;
|
|
48
|
+
/**
|
|
49
|
+
* Drain callback called with every emitted event (fire-and-forget).
|
|
50
|
+
* Compatible with drain adapters and pipeline-wrapped drains.
|
|
51
|
+
*/
|
|
52
|
+
drain?: (ctx: DrainContext) => void | Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Enrich callback called after emit, before drain.
|
|
55
|
+
* Use this to add derived context (e.g. geo, deployment info).
|
|
56
|
+
*/
|
|
57
|
+
enrich?: (ctx: EnrichContext) => void | Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Custom tail sampling callback called before emit.
|
|
60
|
+
* Set `ctx.shouldKeep = true` to force-keep the log regardless of head sampling.
|
|
61
|
+
* Equivalent to Nuxt's `mxllog:emit:keep` hook.
|
|
62
|
+
*/
|
|
63
|
+
keep?: (ctx: TailSamplingContext) => void | Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* When pretty is disabled, emit JSON strings (default) or raw objects.
|
|
66
|
+
* @default true
|
|
67
|
+
*/
|
|
68
|
+
stringify?: boolean;
|
|
69
|
+
}
|
|
70
|
+
interface MxllogMiddlewareConfig {
|
|
71
|
+
/**
|
|
72
|
+
* Route patterns to include in middleware processing.
|
|
73
|
+
* Supports glob patterns like '/api/**'.
|
|
74
|
+
*/
|
|
75
|
+
include?: string[];
|
|
76
|
+
/**
|
|
77
|
+
* Route patterns to exclude from middleware processing.
|
|
78
|
+
* Supports glob patterns like '/_next/**'.
|
|
79
|
+
*/
|
|
80
|
+
exclude?: string[];
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/next/storage.d.ts
|
|
84
|
+
/**
|
|
85
|
+
* Get the current request-scoped logger.
|
|
86
|
+
* Must be called inside a `withMxllog()` wrapper.
|
|
87
|
+
*
|
|
88
|
+
* @throws {Error} if called outside of `withMxllog()` context
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```ts
|
|
92
|
+
* export const POST = withMxllog(async (request) => {
|
|
93
|
+
* const log = useLogger()
|
|
94
|
+
* log.set({ user: { id: '123' } })
|
|
95
|
+
* return Response.json({ ok: true })
|
|
96
|
+
* })
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
declare function useLogger<T extends object = Record<string, unknown>>(): RequestLogger<T>;
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/next/middleware.d.ts
|
|
102
|
+
type NextRequest = {
|
|
103
|
+
nextUrl: {
|
|
104
|
+
pathname: string;
|
|
105
|
+
};
|
|
106
|
+
headers: {
|
|
107
|
+
get(name: string): string | null;
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
type NextResponse = {
|
|
111
|
+
headers: {
|
|
112
|
+
set(name: string, value: string): void;
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
116
|
+
* Create an mxllog middleware for Next.js.
|
|
117
|
+
* Sets `x-request-id` and `x-mxllog-start` headers so `withMxllog()` can reuse them
|
|
118
|
+
* for timing consistency across the middleware -> handler chain.
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```ts
|
|
122
|
+
* // middleware.ts
|
|
123
|
+
* import { mxllogMiddleware } from '@safaricom-mxl/log/next'
|
|
124
|
+
* export const middleware = mxllogMiddleware()
|
|
125
|
+
* export const config = { matcher: ['/api/:path*'] }
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
declare function mxllogMiddleware(config?: MxllogMiddlewareConfig): (request: NextRequest) => Promise<NextResponse>;
|
|
129
|
+
//#endregion
|
|
130
|
+
//#region src/next/index.d.ts
|
|
131
|
+
/**
|
|
132
|
+
* Create an mxllog instance configured for Next.js.
|
|
133
|
+
* Returns all helpers needed for server-side logging.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```ts
|
|
137
|
+
* // lib/mxllog.ts
|
|
138
|
+
* import { createMxllog } from '@safaricom-mxl/log/next'
|
|
139
|
+
* import { createAxiomDrain } from '@safaricom-mxl/log/axiom'
|
|
140
|
+
* import { createDrainPipeline } from '@safaricom-mxl/log/pipeline'
|
|
141
|
+
*
|
|
142
|
+
* const pipeline = createDrainPipeline({ batch: { size: 50 } })
|
|
143
|
+
*
|
|
144
|
+
* export const { withMxllog, useLogger, log, createMxllogError } = createMxllog({
|
|
145
|
+
* service: 'my-app',
|
|
146
|
+
* sampling: {
|
|
147
|
+
* rates: { info: 10 },
|
|
148
|
+
* keep: [{ status: 400 }, { duration: 1000 }],
|
|
149
|
+
* },
|
|
150
|
+
* drain: pipeline(createAxiomDrain({
|
|
151
|
+
* dataset: 'logs',
|
|
152
|
+
* token: process.env.AXIOM_TOKEN!,
|
|
153
|
+
* })),
|
|
154
|
+
* enrich: (ctx) => {
|
|
155
|
+
* ctx.event.deploymentId = process.env.VERCEL_DEPLOYMENT_ID
|
|
156
|
+
* },
|
|
157
|
+
* })
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
declare function createMxllog(options?: NextMxllogOptions): {
|
|
161
|
+
withMxllog: <TArgs extends unknown[], TReturn>(handler: (...args: TArgs) => TReturn) => (...args: TArgs) => Promise<Awaited<TReturn>>;
|
|
162
|
+
useLogger: typeof useLogger;
|
|
163
|
+
log: Log;
|
|
164
|
+
createError: typeof createError;
|
|
165
|
+
createMxllogError: typeof createError;
|
|
166
|
+
};
|
|
167
|
+
//#endregion
|
|
168
|
+
export { type MxllogMiddlewareConfig, type NextMxllogOptions, createError, createError as createMxllogError, createMxllog, _log as log, mxllogMiddleware, useLogger };
|
|
169
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../../src/next/types.ts","../../src/next/storage.ts","../../src/next/middleware.ts","../../src/next/index.ts"],"mappings":";;;;;;;UAEiB,iBAAA;;;;;EAKf,OAAA;EALe;;;EAUf,GAAA,GAAM,OAAA,CAAQ,kBAAA;EAAR;;;;EAMN,MAAA;EAoCsC;;;;EA9BtC,OAAA;EA2CmD;;;EAtCnD,QAAA,GAAW,cAAA;EAjBL;;;;;EAwBN,OAAA;EAAA;;;;;EAOA,OAAA;EAWc;;;EANd,MAAA,GAAS,MAAA,SAAe,WAAA;EAYT;;;;EANf,KAAA,IAAS,GAAA,EAAK,YAAA,YAAwB,OAAA;EAa9B;;;;EAPR,MAAA,IAAU,GAAA,EAAK,aAAA,YAAyB,OAAA;EAgBzB;;;;;EATf,IAAA,IAAQ,GAAA,EAAK,mBAAA,YAA+B,OAAA;;;AC/C9C;;EDqDE,SAAA;AAAA;AAAA,UAGe,sBAAA;ECxDwD;;;;ED6DvE,OAAA;EC7DuE;;;;EDmEvE,OAAA;AAAA;;;;;;;AArFF;;;;;;;;;;;iBCkBgB,SAAA,oBAA6B,MAAA,kBAAA,CAAA,GAA4B,aAAA,CAAc,CAAA;;;KCjBlF,WAAA;EACH,OAAA;IAAW,QAAA;EAAA;EACX,OAAA;IAAW,GAAA,CAAI,IAAA;EAAA;AAAA;AAAA,KAGZ,YAAA;EACH,OAAA;IAAW,GAAA,CAAI,IAAA,UAAc,KAAA;EAAA;AAAA;;;;;;;;;;;;;;iBAoBf,gBAAA,CAAiB,MAAA,GAAS,sBAAA,IAC1B,OAAA,EAAS,WAAA,KAAW,OAAA,CAAA,YAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCYpB,YAAA,CAAa,OAAA,GAAS,iBAAA;8DAWo5D,IAAA,EAAA,KAAA,KAAA,OAAA,SAAkC,IAAA,EAAA,KAAA,KAAA,OAAA,CAAA,OAAA,CAAA,OAAA"}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { filterSafeHeaders } from "../utils.mjs";
|
|
2
|
+
import { MxllogError, createError } from "../error.mjs";
|
|
3
|
+
import { createRequestLogger, initLogger, isEnabled, log as _log } from "../logger.mjs";
|
|
4
|
+
import { n as shouldLog, t as getServiceForPath } from "../routes-BNbrnm14.mjs";
|
|
5
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
6
|
+
|
|
7
|
+
//#region src/next/storage.ts
|
|
8
|
+
const mxllogStorage = new AsyncLocalStorage();
|
|
9
|
+
/**
|
|
10
|
+
* Get the current request-scoped logger.
|
|
11
|
+
* Must be called inside a `withMxllog()` wrapper.
|
|
12
|
+
*
|
|
13
|
+
* @throws {Error} if called outside of `withMxllog()` context
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* export const POST = withMxllog(async (request) => {
|
|
18
|
+
* const log = useLogger()
|
|
19
|
+
* log.set({ user: { id: '123' } })
|
|
20
|
+
* return Response.json({ ok: true })
|
|
21
|
+
* })
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
function useLogger() {
|
|
25
|
+
const logger = mxllogStorage.getStore();
|
|
26
|
+
if (!logger) throw new Error("[mxllog] useLogger() was called outside of a withMxllog() context. Wrap your route handler or server action with withMxllog().");
|
|
27
|
+
return logger;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/next/handler.ts
|
|
32
|
+
const state = {
|
|
33
|
+
initialized: false,
|
|
34
|
+
options: {}
|
|
35
|
+
};
|
|
36
|
+
function configureHandler(options) {
|
|
37
|
+
state.options = options;
|
|
38
|
+
state.initialized = true;
|
|
39
|
+
initLogger({
|
|
40
|
+
enabled: options.enabled,
|
|
41
|
+
env: {
|
|
42
|
+
service: options.service,
|
|
43
|
+
...options.env
|
|
44
|
+
},
|
|
45
|
+
pretty: options.pretty,
|
|
46
|
+
sampling: options.sampling,
|
|
47
|
+
stringify: options.stringify
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function extractRequestInfo(request) {
|
|
51
|
+
const { method } = request;
|
|
52
|
+
const path = new URL(request.url, "http://localhost").pathname;
|
|
53
|
+
const headers = {};
|
|
54
|
+
request.headers.forEach((value, key) => {
|
|
55
|
+
headers[key] = value;
|
|
56
|
+
});
|
|
57
|
+
return {
|
|
58
|
+
method,
|
|
59
|
+
path,
|
|
60
|
+
headers: filterSafeHeaders(headers)
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
async function callEnrichAndDrain(emittedEvent, requestInfo, headers, responseStatus) {
|
|
64
|
+
if (!emittedEvent) return;
|
|
65
|
+
const { enrich, drain } = state.options;
|
|
66
|
+
const run = async () => {
|
|
67
|
+
if (enrich) {
|
|
68
|
+
const enrichCtx = {
|
|
69
|
+
event: emittedEvent,
|
|
70
|
+
request: requestInfo,
|
|
71
|
+
headers,
|
|
72
|
+
response: { status: responseStatus }
|
|
73
|
+
};
|
|
74
|
+
try {
|
|
75
|
+
await enrich(enrichCtx);
|
|
76
|
+
} catch (err) {
|
|
77
|
+
console.error("[mxllog] enrich failed:", err);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (drain) {
|
|
81
|
+
const drainCtx = {
|
|
82
|
+
event: emittedEvent,
|
|
83
|
+
request: requestInfo,
|
|
84
|
+
headers
|
|
85
|
+
};
|
|
86
|
+
try {
|
|
87
|
+
await drain(drainCtx);
|
|
88
|
+
} catch (err) {
|
|
89
|
+
console.error("[mxllog] drain failed:", err);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
try {
|
|
94
|
+
const { after } = await import("next/server");
|
|
95
|
+
if (typeof after === "function") {
|
|
96
|
+
after(run);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
} catch {}
|
|
100
|
+
run().catch(() => {});
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Wrap a Next.js route handler or server action with mxllog request-scoped logging.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* // Route handler
|
|
108
|
+
* export const POST = withMxllog(async (request: NextRequest) => {
|
|
109
|
+
* const log = useLogger()
|
|
110
|
+
* log.set({ user: { id: '123' } })
|
|
111
|
+
* return Response.json({ success: true })
|
|
112
|
+
* })
|
|
113
|
+
*
|
|
114
|
+
* // Server action
|
|
115
|
+
* export const checkout = withMxllog(async (formData: FormData) => {
|
|
116
|
+
* const log = useLogger()
|
|
117
|
+
* log.set({ action: 'checkout' })
|
|
118
|
+
* })
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
function createWithMxllog(options) {
|
|
122
|
+
configureHandler(options);
|
|
123
|
+
return function withMxllog(handler) {
|
|
124
|
+
return async (...args) => {
|
|
125
|
+
if (!isEnabled()) return await handler(...args);
|
|
126
|
+
const [firstArg] = args;
|
|
127
|
+
const isRequest = firstArg instanceof Request;
|
|
128
|
+
let method = "UNKNOWN";
|
|
129
|
+
let path = "/";
|
|
130
|
+
let headers = {};
|
|
131
|
+
let requestId = crypto.randomUUID();
|
|
132
|
+
if (isRequest) {
|
|
133
|
+
({method, path, headers} = extractRequestInfo(firstArg));
|
|
134
|
+
const middlewareRequestId = firstArg.headers.get("x-request-id");
|
|
135
|
+
if (middlewareRequestId) requestId = middlewareRequestId;
|
|
136
|
+
}
|
|
137
|
+
if (!shouldLog(path, state.options.include, state.options.exclude)) return await handler(...args);
|
|
138
|
+
const logger = createRequestLogger({
|
|
139
|
+
method,
|
|
140
|
+
path,
|
|
141
|
+
requestId
|
|
142
|
+
});
|
|
143
|
+
const routeService = getServiceForPath(path, state.options.routes);
|
|
144
|
+
if (routeService) logger.set({ service: routeService });
|
|
145
|
+
if (isRequest) {
|
|
146
|
+
const startHeader = firstArg.headers.get("x-mxllog-start");
|
|
147
|
+
if (startHeader) logger.set({ middlewareStart: Number(startHeader) });
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
const result = await mxllogStorage.run(logger, () => handler(...args));
|
|
151
|
+
let { status } = { status: 200 };
|
|
152
|
+
if (result instanceof Response) ({status} = result);
|
|
153
|
+
logger.set({ status });
|
|
154
|
+
let forceKeep = false;
|
|
155
|
+
if (state.options.keep) try {
|
|
156
|
+
const tailCtx = {
|
|
157
|
+
status,
|
|
158
|
+
path,
|
|
159
|
+
method,
|
|
160
|
+
context: logger.getContext(),
|
|
161
|
+
shouldKeep: false
|
|
162
|
+
};
|
|
163
|
+
await state.options.keep(tailCtx);
|
|
164
|
+
forceKeep = tailCtx.shouldKeep ?? false;
|
|
165
|
+
} catch (err) {
|
|
166
|
+
console.error("[mxllog] keep callback failed:", err);
|
|
167
|
+
}
|
|
168
|
+
await callEnrichAndDrain(logger.emit({ _forceKeep: forceKeep }), {
|
|
169
|
+
method,
|
|
170
|
+
path,
|
|
171
|
+
requestId
|
|
172
|
+
}, headers, status);
|
|
173
|
+
return result;
|
|
174
|
+
} catch (error) {
|
|
175
|
+
logger.error(error instanceof Error ? error : new Error(String(error)));
|
|
176
|
+
const errorStatus = error.status ?? error.statusCode ?? 500;
|
|
177
|
+
logger.set({ status: errorStatus });
|
|
178
|
+
let forceKeep = false;
|
|
179
|
+
if (state.options.keep) try {
|
|
180
|
+
const tailCtx = {
|
|
181
|
+
status: errorStatus,
|
|
182
|
+
path,
|
|
183
|
+
method,
|
|
184
|
+
context: logger.getContext(),
|
|
185
|
+
shouldKeep: false
|
|
186
|
+
};
|
|
187
|
+
await state.options.keep(tailCtx);
|
|
188
|
+
forceKeep = tailCtx.shouldKeep ?? false;
|
|
189
|
+
} catch (err) {
|
|
190
|
+
console.error("[mxllog] keep callback failed:", err);
|
|
191
|
+
}
|
|
192
|
+
await callEnrichAndDrain(logger.emit({ _forceKeep: forceKeep }), {
|
|
193
|
+
method,
|
|
194
|
+
path,
|
|
195
|
+
requestId
|
|
196
|
+
}, headers, errorStatus);
|
|
197
|
+
if (isRequest && error instanceof MxllogError) return Response.json(error.toJSON(), { status: error.status });
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
//#endregion
|
|
205
|
+
//#region src/next/middleware.ts
|
|
206
|
+
/**
|
|
207
|
+
* Create an mxllog middleware for Next.js.
|
|
208
|
+
* Sets `x-request-id` and `x-mxllog-start` headers so `withMxllog()` can reuse them
|
|
209
|
+
* for timing consistency across the middleware -> handler chain.
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```ts
|
|
213
|
+
* // middleware.ts
|
|
214
|
+
* import { mxllogMiddleware } from '@safaricom-mxl/log/next'
|
|
215
|
+
* export const middleware = mxllogMiddleware()
|
|
216
|
+
* export const config = { matcher: ['/api/:path*'] }
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
function mxllogMiddleware(config) {
|
|
220
|
+
return async (request) => {
|
|
221
|
+
const path = request.nextUrl.pathname;
|
|
222
|
+
if (!shouldLog(path, config?.include, config?.exclude)) {
|
|
223
|
+
const { NextResponse: nextResponse } = await import("next/server");
|
|
224
|
+
return nextResponse.next();
|
|
225
|
+
}
|
|
226
|
+
const requestId = request.headers.get("x-request-id") || crypto.randomUUID();
|
|
227
|
+
const requestHeaders = new Headers(request.headers);
|
|
228
|
+
requestHeaders.set("x-request-id", requestId);
|
|
229
|
+
requestHeaders.set("x-mxllog-start", String(Date.now()));
|
|
230
|
+
const { NextResponse: nextResponse } = await import("next/server");
|
|
231
|
+
const response = nextResponse.next({ request: { headers: requestHeaders } });
|
|
232
|
+
response.headers.set("x-request-id", requestId);
|
|
233
|
+
return response;
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
//#endregion
|
|
238
|
+
//#region src/next/index.ts
|
|
239
|
+
/**
|
|
240
|
+
* Create an mxllog instance configured for Next.js.
|
|
241
|
+
* Returns all helpers needed for server-side logging.
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```ts
|
|
245
|
+
* // lib/mxllog.ts
|
|
246
|
+
* import { createMxllog } from '@safaricom-mxl/log/next'
|
|
247
|
+
* import { createAxiomDrain } from '@safaricom-mxl/log/axiom'
|
|
248
|
+
* import { createDrainPipeline } from '@safaricom-mxl/log/pipeline'
|
|
249
|
+
*
|
|
250
|
+
* const pipeline = createDrainPipeline({ batch: { size: 50 } })
|
|
251
|
+
*
|
|
252
|
+
* export const { withMxllog, useLogger, log, createMxllogError } = createMxllog({
|
|
253
|
+
* service: 'my-app',
|
|
254
|
+
* sampling: {
|
|
255
|
+
* rates: { info: 10 },
|
|
256
|
+
* keep: [{ status: 400 }, { duration: 1000 }],
|
|
257
|
+
* },
|
|
258
|
+
* drain: pipeline(createAxiomDrain({
|
|
259
|
+
* dataset: 'logs',
|
|
260
|
+
* token: process.env.AXIOM_TOKEN!,
|
|
261
|
+
* })),
|
|
262
|
+
* enrich: (ctx) => {
|
|
263
|
+
* ctx.event.deploymentId = process.env.VERCEL_DEPLOYMENT_ID
|
|
264
|
+
* },
|
|
265
|
+
* })
|
|
266
|
+
* ```
|
|
267
|
+
*/
|
|
268
|
+
function createMxllog(options = {}) {
|
|
269
|
+
return {
|
|
270
|
+
withMxllog: createWithMxllog(options),
|
|
271
|
+
useLogger,
|
|
272
|
+
log: _log,
|
|
273
|
+
createError,
|
|
274
|
+
createMxllogError: createError
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
//#endregion
|
|
279
|
+
export { createError, createError as createMxllogError, createMxllog, _log as log, mxllogMiddleware, useLogger };
|
|
280
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/next/storage.ts","../../src/next/handler.ts","../../src/next/middleware.ts","../../src/next/index.ts"],"sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks'\nimport type { RequestLogger } from '../types'\n\nexport const mxllogStorage = new AsyncLocalStorage<RequestLogger>()\n\n/**\n * Get the current request-scoped logger.\n * Must be called inside a `withMxllog()` wrapper.\n *\n * @throws {Error} if called outside of `withMxllog()` context\n *\n * @example\n * ```ts\n * export const POST = withMxllog(async (request) => {\n * const log = useLogger()\n * log.set({ user: { id: '123' } })\n * return Response.json({ ok: true })\n * })\n * ```\n */\nexport function useLogger<T extends object = Record<string, unknown>>(): RequestLogger<T> {\n const logger = mxllogStorage.getStore()\n if (!logger) {\n throw new Error(\n '[mxllog] useLogger() was called outside of a withMxllog() context. '\n + 'Wrap your route handler or server action with withMxllog().',\n )\n }\n return logger as RequestLogger<T>\n}\n","import type { DrainContext, EnrichContext, TailSamplingContext, WideEvent } from '../types'\nimport { createRequestLogger, initLogger, isEnabled } from '../logger'\nimport { filterSafeHeaders } from '../utils'\nimport { shouldLog, getServiceForPath } from '../shared/routes'\nimport { MxllogError } from '../error'\nimport type { NextMxllogOptions } from './types'\nimport { mxllogStorage } from './storage'\n\ninterface WithMxllogState {\n initialized: boolean\n options: NextMxllogOptions\n}\n\nconst state: WithMxllogState = {\n initialized: false,\n options: {},\n}\n\nexport function configureHandler(options: NextMxllogOptions): void {\n state.options = options\n state.initialized = true\n\n // Don't pass drain to initLogger — the global drain fires inside emitWideEvent\n // which doesn't have request/header context. Instead, we call drain ourselves\n // in callEnrichAndDrain after enrich, with full context.\n initLogger({\n enabled: options.enabled,\n env: {\n service: options.service,\n ...options.env,\n },\n pretty: options.pretty,\n sampling: options.sampling,\n stringify: options.stringify,\n })\n}\n\nfunction extractRequestInfo(request: Request): { method: string, path: string, headers: Record<string, string> } {\n const { method } = request\n const url = new URL(request.url, 'http://localhost')\n const path = url.pathname\n\n const headers: Record<string, string> = {}\n request.headers.forEach((value, key) => {\n headers[key] = value\n })\n\n return { method, path, headers: filterSafeHeaders(headers) }\n}\n\nasync function callEnrichAndDrain(\n emittedEvent: WideEvent | null,\n requestInfo: { method: string, path: string, requestId: string },\n headers: Record<string, string>,\n responseStatus?: number,\n): Promise<void> {\n if (!emittedEvent) return\n\n const { enrich, drain } = state.options\n\n const run = async () => {\n if (enrich) {\n const enrichCtx: EnrichContext = {\n event: emittedEvent,\n request: requestInfo,\n headers,\n response: { status: responseStatus },\n }\n try {\n await enrich(enrichCtx)\n } catch (err) {\n console.error('[mxllog] enrich failed:', err)\n }\n }\n\n if (drain) {\n const drainCtx: DrainContext = {\n event: emittedEvent,\n request: requestInfo,\n headers,\n }\n try {\n await drain(drainCtx)\n } catch (err) {\n console.error('[mxllog] drain failed:', err)\n }\n }\n }\n\n // Use next/server after() if available to run enrich+drain after response\n try {\n const { after } = await import('next/server')\n if (typeof after === 'function') {\n after(run)\n return\n }\n } catch {\n // next/server not available or after() not exported — run inline\n }\n\n // Fallback: fire-and-forget (enrich still awaited for correctness)\n run().catch(() => {})\n}\n\n/**\n * Wrap a Next.js route handler or server action with mxllog request-scoped logging.\n *\n * @example\n * ```ts\n * // Route handler\n * export const POST = withMxllog(async (request: NextRequest) => {\n * const log = useLogger()\n * log.set({ user: { id: '123' } })\n * return Response.json({ success: true })\n * })\n *\n * // Server action\n * export const checkout = withMxllog(async (formData: FormData) => {\n * const log = useLogger()\n * log.set({ action: 'checkout' })\n * })\n * ```\n */\nexport function createWithMxllog(options: NextMxllogOptions) {\n configureHandler(options)\n\n return function withMxllog<TArgs extends unknown[], TReturn>(\n handler: (...args: TArgs) => TReturn,\n ): (...args: TArgs) => Promise<Awaited<TReturn>> {\n return async (...args: TArgs): Promise<Awaited<TReturn>> => {\n if (!isEnabled()) {\n return await handler(...args) as Awaited<TReturn>\n }\n\n // Extract request info from first argument if it's a Request\n const [firstArg] = args\n const isRequest = firstArg instanceof Request\n\n let method = 'UNKNOWN'\n let path = '/'\n let headers: Record<string, string> = {}\n let requestId = crypto.randomUUID()\n\n if (isRequest) {\n ({ method, path, headers } = extractRequestInfo(firstArg))\n\n // Reuse request-id from middleware if present\n const middlewareRequestId = firstArg.headers.get('x-request-id')\n if (middlewareRequestId) requestId = middlewareRequestId\n }\n\n // Check include/exclude patterns\n if (!shouldLog(path, state.options.include, state.options.exclude)) {\n return await handler(...args) as Awaited<TReturn>\n }\n\n const logger = createRequestLogger({ method, path, requestId })\n\n // Apply route-based service configuration\n const routeService = getServiceForPath(path, state.options.routes)\n if (routeService) {\n logger.set({ service: routeService })\n }\n\n // Apply start time from middleware if present\n if (isRequest) {\n const startHeader = firstArg.headers.get('x-mxllog-start')\n if (startHeader) {\n logger.set({ middlewareStart: Number(startHeader) })\n }\n }\n\n try {\n const result = await mxllogStorage.run(logger, () => handler(...args))\n\n // Extract response status\n let { status } = { status: 200 }\n if (result instanceof Response) {\n ({ status } = result)\n }\n logger.set({ status })\n\n // Build tail sampling context and call keep callback\n let forceKeep = false\n if (state.options.keep) {\n try {\n const tailCtx: TailSamplingContext = {\n status,\n path,\n method,\n context: logger.getContext(),\n shouldKeep: false,\n }\n await state.options.keep(tailCtx)\n forceKeep = tailCtx.shouldKeep ?? false\n } catch (err) {\n console.error('[mxllog] keep callback failed:', err)\n }\n }\n\n const emittedEvent = logger.emit({ _forceKeep: forceKeep })\n await callEnrichAndDrain(emittedEvent, { method, path, requestId }, headers, status)\n\n return result as Awaited<TReturn>\n } catch (error) {\n logger.error(error instanceof Error ? error : new Error(String(error)))\n\n const errorStatus = (error as { status?: number }).status\n ?? (error as { statusCode?: number }).statusCode\n ?? 500\n logger.set({ status: errorStatus })\n\n // Build tail sampling context and call keep callback\n let forceKeep = false\n if (state.options.keep) {\n try {\n const tailCtx: TailSamplingContext = {\n status: errorStatus,\n path,\n method,\n context: logger.getContext(),\n shouldKeep: false,\n }\n await state.options.keep(tailCtx)\n forceKeep = tailCtx.shouldKeep ?? false\n } catch (err) {\n console.error('[mxllog] keep callback failed:', err)\n }\n }\n\n const emittedEvent = logger.emit({ _forceKeep: forceKeep })\n await callEnrichAndDrain(emittedEvent, { method, path, requestId }, headers, errorStatus)\n\n // Return structured JSON response for MxllogErrors (like H3 does for Nuxt)\n if (isRequest && error instanceof MxllogError) {\n return Response.json(error.toJSON(), { status: error.status }) as Awaited<TReturn>\n }\n\n throw error\n }\n }\n }\n}\n","import { shouldLog } from '../shared/routes'\nimport type { MxllogMiddlewareConfig } from './types'\n\ntype NextRequest = {\n nextUrl: { pathname: string }\n headers: { get(name: string): string | null }\n}\n\ntype NextResponse = {\n headers: { set(name: string, value: string): void }\n}\n\ntype NextResponseStatic = {\n next(options?: { request?: { headers: Headers } }): NextResponse\n}\n\n/**\n * Create an mxllog middleware for Next.js.\n * Sets `x-request-id` and `x-mxllog-start` headers so `withMxllog()` can reuse them\n * for timing consistency across the middleware -> handler chain.\n *\n * @example\n * ```ts\n * // middleware.ts\n * import { mxllogMiddleware } from '@safaricom-mxl/log/next'\n * export const middleware = mxllogMiddleware()\n * export const config = { matcher: ['/api/:path*'] }\n * ```\n */\nexport function mxllogMiddleware(config?: MxllogMiddlewareConfig) {\n return async (request: NextRequest) => {\n const path = request.nextUrl.pathname\n\n // Check include/exclude patterns\n if (!shouldLog(path, config?.include, config?.exclude)) {\n const { NextResponse: nextResponse } = await import('next/server') as { NextResponse: NextResponseStatic }\n return nextResponse.next()\n }\n\n // Generate or reuse request ID\n const existingId = request.headers.get('x-request-id')\n const requestId = existingId || crypto.randomUUID()\n\n // Forward modified headers to the route handler\n const requestHeaders = new Headers(request.headers as HeadersInit)\n\n requestHeaders.set('x-request-id', requestId)\n requestHeaders.set('x-mxllog-start', String(Date.now()))\n\n const { NextResponse: nextResponse } = await import('next/server') as { NextResponse: NextResponseStatic }\n const response = nextResponse.next({\n request: { headers: requestHeaders },\n })\n\n // Also set on response for downstream consumers\n response.headers.set('x-request-id', requestId)\n\n return response\n }\n}\n","import { log } from '../logger'\nimport { createError, createMxllogError } from '../error'\nimport type { NextMxllogOptions } from './types'\nimport { createWithMxllog } from './handler'\nimport { useLogger } from './storage'\n\nexport type { NextMxllogOptions, MxllogMiddlewareConfig } from './types'\n\nexport { mxllogMiddleware } from './middleware'\nexport { useLogger } from './storage'\nexport { log } from '../logger'\nexport { createError, createMxllogError } from '../error'\n\n/**\n * Create an mxllog instance configured for Next.js.\n * Returns all helpers needed for server-side logging.\n *\n * @example\n * ```ts\n * // lib/mxllog.ts\n * import { createMxllog } from '@safaricom-mxl/log/next'\n * import { createAxiomDrain } from '@safaricom-mxl/log/axiom'\n * import { createDrainPipeline } from '@safaricom-mxl/log/pipeline'\n *\n * const pipeline = createDrainPipeline({ batch: { size: 50 } })\n *\n * export const { withMxllog, useLogger, log, createMxllogError } = createMxllog({\n * service: 'my-app',\n * sampling: {\n * rates: { info: 10 },\n * keep: [{ status: 400 }, { duration: 1000 }],\n * },\n * drain: pipeline(createAxiomDrain({\n * dataset: 'logs',\n * token: process.env.AXIOM_TOKEN!,\n * })),\n * enrich: (ctx) => {\n * ctx.event.deploymentId = process.env.VERCEL_DEPLOYMENT_ID\n * },\n * })\n * ```\n */\nexport function createMxllog(options: NextMxllogOptions = {}) {\n const withMxllog = createWithMxllog(options)\n\n return {\n withMxllog,\n useLogger,\n log,\n createError,\n createMxllogError,\n }\n}\n"],"mappings":";;;;;;;AAGA,MAAa,gBAAgB,IAAI,mBAAkC;;;;;;;;;;;;;;;;AAiBnE,SAAgB,YAA0E;CACxF,MAAM,SAAS,cAAc,UAAU;AACvC,KAAI,CAAC,OACH,OAAM,IAAI,MACR,iIAED;AAEH,QAAO;;;;;ACfT,MAAM,QAAyB;CAC7B,aAAa;CACb,SAAS,EAAE;CACZ;AAED,SAAgB,iBAAiB,SAAkC;AACjE,OAAM,UAAU;AAChB,OAAM,cAAc;AAKpB,YAAW;EACT,SAAS,QAAQ;EACjB,KAAK;GACH,SAAS,QAAQ;GACjB,GAAG,QAAQ;GACZ;EACD,QAAQ,QAAQ;EAChB,UAAU,QAAQ;EAClB,WAAW,QAAQ;EACpB,CAAC;;AAGJ,SAAS,mBAAmB,SAAqF;CAC/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C;EACA;EACA;EACA;EACD"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as nitropack from "nitropack";
|
|
2
|
+
|
|
3
|
+
//#region src/nitro/errorHandler.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Custom Nitro error handler that properly serializes MxllogError.
|
|
6
|
+
* This ensures that 'data' (containing 'why', 'fix', 'link') is preserved
|
|
7
|
+
* in the JSON response regardless of the underlying HTTP framework.
|
|
8
|
+
*
|
|
9
|
+
* For non-MxllogError, it preserves Nitro's default response shape while
|
|
10
|
+
* sanitizing internal error details in production for 5xx errors.
|
|
11
|
+
*/
|
|
12
|
+
declare const _default: nitropack.NitroErrorHandler;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { _default as default };
|
|
15
|
+
//# sourceMappingURL=errorHandler.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errorHandler.d.mts","names":[],"sources":["../../src/nitro/errorHandler.ts"],"mappings":""}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { n as resolveMxllogError, r as serializeMxllogErrorResponse, t as extractErrorStatus } from "../nitro-Dsv6dSzv.mjs";
|
|
2
|
+
import { getRequestURL, send, setResponseHeader, setResponseStatus } from "h3";
|
|
3
|
+
import { defineNitroErrorHandler } from "nitropack/runtime/internal/error/utils";
|
|
4
|
+
|
|
5
|
+
//#region src/nitro/errorHandler.ts
|
|
6
|
+
/**
|
|
7
|
+
* Custom Nitro error handler that properly serializes MxllogError.
|
|
8
|
+
* This ensures that 'data' (containing 'why', 'fix', 'link') is preserved
|
|
9
|
+
* in the JSON response regardless of the underlying HTTP framework.
|
|
10
|
+
*
|
|
11
|
+
* For non-MxllogError, it preserves Nitro's default response shape while
|
|
12
|
+
* sanitizing internal error details in production for 5xx errors.
|
|
13
|
+
*/
|
|
14
|
+
var errorHandler_default = defineNitroErrorHandler((error, event) => {
|
|
15
|
+
const mxllogError = resolveMxllogError(error);
|
|
16
|
+
const isDev = process.env.NODE_ENV === "development";
|
|
17
|
+
const url = getRequestURL(event, { xForwardedHost: true }).pathname;
|
|
18
|
+
if (!mxllogError) {
|
|
19
|
+
const status = extractErrorStatus(error);
|
|
20
|
+
const rawMessage = (error.statusText ?? error.statusMessage ?? error.message) || "Internal Server Error";
|
|
21
|
+
const message = isDev ? rawMessage : status >= 500 ? "Internal Server Error" : rawMessage;
|
|
22
|
+
setResponseStatus(event, status);
|
|
23
|
+
setResponseHeader(event, "Content-Type", "application/json");
|
|
24
|
+
return send(event, JSON.stringify({
|
|
25
|
+
url,
|
|
26
|
+
status,
|
|
27
|
+
statusCode: status,
|
|
28
|
+
statusText: message,
|
|
29
|
+
statusMessage: message,
|
|
30
|
+
message,
|
|
31
|
+
error: true
|
|
32
|
+
}));
|
|
33
|
+
}
|
|
34
|
+
setResponseStatus(event, extractErrorStatus(mxllogError));
|
|
35
|
+
setResponseHeader(event, "Content-Type", "application/json");
|
|
36
|
+
return send(event, JSON.stringify(serializeMxllogErrorResponse(mxllogError, url)));
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
export { errorHandler_default as default };
|
|
41
|
+
//# sourceMappingURL=errorHandler.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errorHandler.mjs","names":[],"sources":["../../src/nitro/errorHandler.ts"],"sourcesContent":["// Import from specific subpath — the barrel 'nitropack/runtime' re-exports from\n// internal/app.mjs which imports virtual modules that crash outside rollup builds.\nimport { defineNitroErrorHandler } from 'nitropack/runtime/internal/error/utils'\nimport { getRequestURL, setResponseHeader, setResponseStatus, send } from 'h3'\nimport { resolveMxllogError, extractErrorStatus, serializeMxllogErrorResponse } from '../nitro'\n\n/**\n * Custom Nitro error handler that properly serializes MxllogError.\n * This ensures that 'data' (containing 'why', 'fix', 'link') is preserved\n * in the JSON response regardless of the underlying HTTP framework.\n *\n * For non-MxllogError, it preserves Nitro's default response shape while\n * sanitizing internal error details in production for 5xx errors.\n */\nexport default defineNitroErrorHandler((error, event) => {\n const mxllogError = resolveMxllogError(error)\n\n const isDev = process.env.NODE_ENV === 'development'\n const url = getRequestURL(event, { xForwardedHost: true }).pathname\n\n // For non-MxllogError, preserve Nitro's default response shape\n if (!mxllogError) {\n const status = extractErrorStatus(error)\n\n // Derive message from statusText/statusMessage/message for cross-version compatibility\n const rawMessage = ((error as { statusText?: string }).statusText\n ?? (error as { statusMessage?: string }).statusMessage\n ?? error.message) || 'Internal Server Error'\n\n // Sanitize internal error details in production for 5xx errors\n const message = isDev\n ? rawMessage\n : (status >= 500 ? 'Internal Server Error' : rawMessage)\n\n setResponseStatus(event, status)\n setResponseHeader(event, 'Content-Type', 'application/json')\n\n return send(event, JSON.stringify({\n url,\n status,\n statusCode: status,\n statusText: message,\n statusMessage: message,\n message,\n error: true,\n }))\n }\n\n const status = extractErrorStatus(mxllogError)\n\n setResponseStatus(event, status)\n setResponseHeader(event, 'Content-Type', 'application/json')\n\n return send(event, JSON.stringify(serializeMxllogErrorResponse(mxllogError, url)))\n})\n"],"mappings":";;;;;;;;;;;;;AAcA,2BAAe,yBAAyB,OAAO,UAAU;CACvD,MAAM,cAAc,mBAAmB,MAAM;CAE7C,MAAM,QAAQ,QAAQ,IAAI,aAAa;CACvC,MAAM,MAAM,cAAc,OAAO,EAAE,gBAAgB,MAAM,CAAC,CAAC;AAG3D,KAAI,CAAC,aAAa;EAChB,MAAM,SAAS,mBAAmB,MAAM;EAGxC,MAAM,cAAe,MAAkC,cACjD,MAAqC,iBACtC,MAAM,YAAY;EAGvB,MAAM,UAAU,QACZ,aACC,UAAU,MAAM,0BAA0B;AAE/C,oBAAkB,OAAO,OAAO;AAChC,oBAAkB,OAAO,gBAAgB,mBAAmB;AAE5D,SAAO,KAAK,OAAO,KAAK,UAAU;GAChC;GACA;GACA,YAAY;GACZ,YAAY;GACZ,eAAe;GACf;GACA,OAAO;GACR,CAAC,CAAC;;AAKL,mBAAkB,OAFH,mBAAmB,YAAY,CAEd;AAChC,mBAAkB,OAAO,gBAAgB,mBAAmB;AAE5D,QAAO,KAAK,OAAO,KAAK,UAAU,6BAA6B,aAAa,IAAI,CAAC,CAAC;EAClF"}
|