@nexload-sdk/logger 1.0.1

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.
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Represents a value that may be a Promise or not.
3
+ */
4
+ type MaybePromise<T> = T | Promise<T>;
5
+ /**
6
+ * Operation type definitions
7
+ */
8
+ type LoggerRenderType = {
9
+ type: "render";
10
+ };
11
+ type LoggerGetDataType = {
12
+ type: "get-data";
13
+ functionName?: string;
14
+ };
15
+ export type LoggerOptions = {
16
+ route: string;
17
+ } & (LoggerRenderType | LoggerGetDataType);
18
+ export type LoggerTimingShotFunction = (name: string) => void;
19
+ /**
20
+ * Structured performance-aware logger wrapper.
21
+ * Supports both sync and async functions (MaybePromise).
22
+ */
23
+ export declare function withLogger<T>(options: LoggerOptions, fn: (timingShot: LoggerTimingShotFunction) => MaybePromise<T>): Promise<T>;
24
+ export {};
25
+ //# sourceMappingURL=next-js.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"next-js.d.ts","sourceRoot":"","sources":["../src/next-js.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,KAAK,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;AAEtC;;GAEG;AACH,KAAK,gBAAgB,GAAG;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,CAAC;AAC3C,KAAK,iBAAiB,GAAG;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAErE,MAAM,MAAM,aAAa,GAAG;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG,CAC5C,gBAAgB,GAChB,iBAAiB,CACpB,CAAC;AACF,MAAM,MAAM,wBAAwB,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;AAiC9D;;;GAGG;AACH,wBAAsB,UAAU,CAAC,CAAC,EAChC,OAAO,EAAE,aAAa,EACtB,EAAE,EAAE,CAAC,UAAU,EAAE,wBAAwB,KAAK,YAAY,CAAC,CAAC,CAAC,GAC5D,OAAO,CAAC,CAAC,CAAC,CA+CZ"}
@@ -0,0 +1,5 @@
1
+ import { createBundler } from "@nexload-sdk/bundler";
2
+
3
+ const esbuildBundler = createBundler();
4
+
5
+ esbuildBundler();
@@ -0,0 +1,4 @@
1
+ import { config } from "@nexload-sdk/eslint-config";
2
+
3
+ /** @type {import("eslint").Linter.Config} */
4
+ export default config;
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@nexload-sdk/logger",
3
+ "version": "1.0.1",
4
+ "exports": {
5
+ ".": {
6
+ "types": "./dist/index.d.ts",
7
+ "default": "./dist/index.cjs"
8
+ }
9
+ },
10
+ "license": "MIT",
11
+ "type": "module",
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "devDependencies": {
16
+ "@types/node": "^24.9.1",
17
+ "eslint": "^9.38.0",
18
+ "typescript": "5.9.3",
19
+ "@nexload-sdk/eslint-config": "1.0.0",
20
+ "@nexload-sdk/typescript-config": "1.0.0",
21
+ "@nexload-sdk/bundler": "1.0.0"
22
+ },
23
+ "dependencies": {
24
+ "@nexload-sdk/env": "^1.0.0",
25
+ "pino": "^10.0.0",
26
+ "pino-pretty": "^13.1.2"
27
+ },
28
+ "scripts": {
29
+ "lint": "eslint .",
30
+ "build": "node esbuild.config.mjs"
31
+ }
32
+ }
package/src/index.ts ADDED
@@ -0,0 +1,38 @@
1
+ import { EnvManager, merge } from "@nexload-sdk/env";
2
+ import { $NodePreset } from "@nexload-sdk/env/presets";
3
+ import pino from "pino";
4
+ import pretty from "pino-pretty";
5
+
6
+ const env = new EnvManager(
7
+ "logger", merge($NodePreset)
8
+ );
9
+ const serviceName = env.$("SERVICE_NAME");
10
+ const stream = pretty({
11
+ colorize: pretty.isColorSupported,
12
+ ignore: "pid,hostname",
13
+ });
14
+ const logger = pino(
15
+ {
16
+ name: serviceName,
17
+ level: env.$("LOG_LEVEL"),
18
+ timestamp: pino.stdTimeFunctions.isoTime,
19
+ formatters: {
20
+ level (label) {
21
+ return { level: label.toUpperCase(), };
22
+ },
23
+ },
24
+ base: { pid: process.pid, },
25
+ redact: {
26
+ paths: [
27
+ "req.headers.authorization",
28
+ "req.headers.cookie",
29
+ "user.password",
30
+ "user.secret"
31
+ ],
32
+ censor: "[REDACTED]",
33
+ },
34
+ }, stream
35
+ );
36
+
37
+ export { withLogger } from "./next-js";
38
+ export default logger;
package/src/next-js.ts ADDED
@@ -0,0 +1,105 @@
1
+ import logger from "./index";
2
+
3
+ /**
4
+ * Represents a value that may be a Promise or not.
5
+ */
6
+ type MaybePromise<T> = T | Promise<T>;
7
+
8
+ /**
9
+ * Operation type definitions
10
+ */
11
+ type LoggerRenderType = { type: "render" };
12
+ type LoggerGetDataType = { type: "get-data", functionName?: string };
13
+
14
+ export type LoggerOptions = { route: string } & (
15
+ | LoggerRenderType
16
+ | LoggerGetDataType
17
+ );
18
+ export type LoggerTimingShotFunction = (name: string) => void;
19
+
20
+ const getLogOptions = (
21
+ baseLog: Record<string, unknown>, duration: string
22
+ ) => {
23
+ if ("timing" in baseLog) {
24
+ const timing = baseLog.timing as Record<string, number>;
25
+
26
+ return {
27
+ ...baseLog,
28
+ timing: {
29
+ ...Object.fromEntries(Object.entries(timing).map(([
30
+ key,
31
+ value
32
+ ]) => [
33
+ key,
34
+ value.toFixed(2) + "ms"
35
+ ])),
36
+ total:
37
+ Object.values(timing)
38
+ .reduce(
39
+ (
40
+ acc, value
41
+ ) => acc + value, 0
42
+ )
43
+ .toFixed(2) + "ms",
44
+ },
45
+ };
46
+ }
47
+
48
+ return { ...baseLog, duration, };
49
+ };
50
+
51
+ /**
52
+ * Structured performance-aware logger wrapper.
53
+ * Supports both sync and async functions (MaybePromise).
54
+ */
55
+ export async function withLogger<T> (
56
+ options: LoggerOptions,
57
+ fn: (timingShot: LoggerTimingShotFunction) => MaybePromise<T>
58
+ ): Promise<T> {
59
+ let start = performance.now();
60
+ const operationName
61
+ = options.type === "render" ? "Rendering" : "Getting data";
62
+
63
+ const baseLog: Record<string, unknown> = {
64
+ route: options.route,
65
+ ...(options.type === "get-data" && options.functionName
66
+ ? { functionName: options.functionName, }
67
+ : {}),
68
+ };
69
+
70
+ logger.debug(
71
+ baseLog, `${operationName} start`
72
+ );
73
+
74
+ const timingShot: LoggerTimingShotFunction = (name) => {
75
+ const duration = performance.now() - start;
76
+ start = performance.now();
77
+
78
+ baseLog.timing ??= {};
79
+ (baseLog.timing as Record<string, number>)[String(name)] = duration;
80
+ };
81
+
82
+ try {
83
+ // Handle both sync and async results safely
84
+ const result = await Promise.resolve(fn(timingShot));
85
+ const duration = performance.now() - start;
86
+
87
+ logger.debug(
88
+ getLogOptions(
89
+ baseLog, `${duration.toFixed(2)}ms`
90
+ ), `${operationName} completed`
91
+ );
92
+
93
+ return result;
94
+ } catch (error) {
95
+ const duration = performance.now() - start;
96
+
97
+ logger.error(
98
+ getLogOptions(
99
+ baseLog, `${duration.toFixed(2)}ms`
100
+ ), `${operationName} failed`, error
101
+ );
102
+
103
+ throw error;
104
+ }
105
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "@nexload-sdk/typescript-config/base.json",
3
+ "include": ["."],
4
+ "compilerOptions": {
5
+ "outDir": "./dist",
6
+ "declaration": true,
7
+ "emitDeclarationOnly": true
8
+ },
9
+ "exclude": ["node_modules", "dist"]
10
+ }