@peerbit/logger 1.0.3-ff87533 → 1.0.4

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.
@@ -1,8 +1,15 @@
1
- export declare const getEnv: (key: any) => string;
2
- export declare const getLogLevel: () => string;
1
+ type LogLevel = "fatal" | "error" | "warn" | "info" | "debug" | "trace" | "silent";
2
+ /**
3
+ * Safely read an env var from:
4
+ * - Node: process.env
5
+ * - Webpack/CRA (browser/worker): globalThis.process.env (if defined by bundler)
6
+ * - Vite (browser/worker): import.meta.env
7
+ */
8
+ export declare const getEnv: (key: string) => string | undefined;
9
+ export declare const getLogLevel: () => LogLevel | undefined;
3
10
  declare const logger: (options?: {
4
11
  module?: string;
5
- level?: string;
12
+ level?: LogLevel;
6
13
  }) => import("pino").Logger<never>;
7
14
  export { logger };
8
15
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,MAAM,GAAI,KAAK,GAAG,WAO9B,CAAC;AACF,eAAO,MAAM,WAAW,cAevB,CAAC;AAEF,QAAA,MAAM,MAAM,GAAI,UAAU;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,iCAc5D,CAAC;AAEF,OAAO,EAAE,MAAM,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAKA,KAAK,QAAQ,GACV,OAAO,GACP,OAAO,GACP,MAAM,GACN,MAAM,GACN,OAAO,GACP,OAAO,GACP,QAAQ,CAAC;AAEZ;;;;;GAKG;AACH,eAAO,MAAM,MAAM,GAAI,KAAK,MAAM,KAAG,MAAM,GAAG,SAyB7C,CAAC;AAEF,eAAO,MAAM,WAAW,QAAO,QAAQ,GAAG,SAmBzC,CAAC;AAEF,QAAA,MAAM,MAAM,GAAI,UAAU;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,QAAQ,CAAA;CAAE,iCAc9D,CAAC;AAEF,OAAO,EAAE,MAAM,EAAE,CAAC"}
package/dist/src/index.js CHANGED
@@ -1,42 +1,65 @@
1
1
  import pino from "pino";
2
- const isNode = typeof window === "undefined";
2
+ // Robust Node detection (true only in Node.js)
3
+ const isNode = typeof process !== "undefined" && !!process.versions?.node;
4
+ /**
5
+ * Safely read an env var from:
6
+ * - Node: process.env
7
+ * - Webpack/CRA (browser/worker): globalThis.process.env (if defined by bundler)
8
+ * - Vite (browser/worker): import.meta.env
9
+ */
3
10
  export const getEnv = (key) => {
4
11
  if (isNode) {
5
- // node
6
- return process.env[key];
12
+ return process.env?.[key];
7
13
  }
8
- // browser
9
- return window.process?.env?.[key];
14
+ // Some bundlers (Webpack/CRA) inject a process shim
15
+ const g = typeof globalThis !== "undefined" ? globalThis : {};
16
+ if (g.process?.env && typeof g.process.env[key] !== "undefined") {
17
+ return g.process.env[key];
18
+ }
19
+ // Vite exposes import.meta.env
20
+ let metaEnv;
21
+ try {
22
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
23
+ // @ts-ignore - import.meta may not exist in TS/node type context
24
+ metaEnv = import.meta?.env;
25
+ }
26
+ catch {
27
+ // ignore if not supported
28
+ }
29
+ if (metaEnv && typeof metaEnv[key] !== "undefined") {
30
+ return metaEnv[key];
31
+ }
32
+ return undefined;
10
33
  };
11
34
  export const getLogLevel = () => {
12
35
  const level = getEnv("LOG_LEVEL") || getEnv("REACT_APP_LOG_LEVEL");
13
- if (!level) {
36
+ if (!level)
14
37
  return undefined;
15
- }
16
- const levels = ["fatal", "error", "warn", "info", "debug", "trace"];
38
+ const levels = [
39
+ "fatal",
40
+ "error",
41
+ "warn",
42
+ "info",
43
+ "debug",
44
+ "trace",
45
+ "silent",
46
+ ];
17
47
  if (!levels.includes(level)) {
18
- throw new Error("Unexpected LOG_LEVEL: " +
19
- level +
20
- ". Expecting one of: " +
21
- JSON.stringify(levels));
48
+ throw new Error(`Unexpected LOG_LEVEL: ${level}. Expecting one of: ${JSON.stringify(levels)}`);
22
49
  }
23
50
  return level;
24
51
  };
25
52
  const logger = (options) => {
26
- let logger = pino();
53
+ // In browsers/workers, pino's browser build logs to console by default.
54
+ let base = pino();
27
55
  if (options?.module) {
28
- logger = logger.child({ module: options.module });
29
- }
30
- if (options?.level) {
31
- logger.level = options.level;
56
+ base = base.child({ module: options.module });
32
57
  }
33
- else {
34
- const logLevel = getLogLevel();
35
- if (logLevel) {
36
- logger.level = logLevel;
37
- }
58
+ const level = options?.level ?? getLogLevel();
59
+ if (level) {
60
+ base.level = level;
38
61
  }
39
- return logger;
62
+ return base;
40
63
  };
41
64
  export { logger };
42
65
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,MAAM,MAAM,GAAG,OAAO,MAAM,KAAK,WAAW,CAAC;AAE7C,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,GAAQ,EAAE,EAAE;IAClC,IAAI,MAAM,EAAE,CAAC;QACZ,OAAO;QACP,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACzB,CAAC;IACD,UAAU;IACV,OAAO,MAAM,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC;AACnC,CAAC,CAAC;AACF,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,EAAE;IAC/B,MAAM,KAAK,GAAG,MAAM,CAAC,WAAW,CAAC,IAAI,MAAM,CAAC,qBAAqB,CAAC,CAAC;IACnE,IAAI,CAAC,KAAK,EAAE,CAAC;QACZ,OAAO,SAAS,CAAC;IAClB,CAAC;IACD,MAAM,MAAM,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,E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AC,CAAC;AAEF,MAAM,MAAM,GAAG,CAAC,OAA+C,EAAE,EAAE;IAClE,wEAAwE;IACxE,IAAI,IAAI,GAAG,IAAI,EAAE,CAAC;IAElB,IAAI,OAAO,EAAE,MAAM,EAAE,CAAC;QACrB,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;IAC/C,CAAC;IAED,MAAM,KAAK,GAAG,OAAO,EAAE,KAAK,IAAI,WAAW,EAAE,CAAC;IAC9C,IAAI,KAAK,EAAE,CAAC;QACX,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACpB,CAAC;IAED,OAAO,IAAI,CAAC;AACb,CAAC,CAAC;AAEF,OAAO,EAAE,MAAM,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,62 +1,62 @@
1
1
  {
2
- "name": "@peerbit/logger",
3
- "version": "1.0.3-ff87533",
4
- "description": "Logging utils",
5
- "sideEffects": false,
6
- "type": "module",
7
- "types": "./dist/src/index.d.ts",
8
- "typesVersions": {
9
- "*": {
10
- "*": [
11
- "*",
12
- "dist/*",
13
- "dist/src/*",
14
- "dist/src/*/index"
15
- ],
16
- "src/*": [
17
- "*",
18
- "dist/*",
19
- "dist/src/*",
20
- "dist/src/*/index"
21
- ]
22
- }
23
- },
24
- "files": [
25
- "src",
26
- "dist",
27
- "!dist/test",
28
- "!**/*.tsbuildinfo"
29
- ],
30
- "exports": {
31
- ".": {
32
- "types": "./dist/src/index.d.ts",
33
- "import": "./dist/src/index.js"
34
- }
35
- },
36
- "eslintConfig": {
37
- "extends": "peerbit",
38
- "parserOptions": {
39
- "project": true,
40
- "sourceType": "module"
41
- },
42
- "ignorePatterns": [
43
- "!.aegir.js",
44
- "test/ts-use",
45
- "*.d.ts"
46
- ]
47
- },
48
- "publishConfig": {
49
- "access": "public"
50
- },
51
- "dependencies": {
52
- "pino": "^8.14.1"
53
- },
54
- "scripts": {
55
- "clean": "aegir clean",
56
- "build": "aegir build --no-bundle",
57
- "test": "aegir test --target node",
58
- "lint": "aegir lint"
59
- },
60
- "author": "dao.xyz",
61
- "license": "MIT"
2
+ "name": "@peerbit/logger",
3
+ "version": "1.0.4",
4
+ "description": "Logging utils",
5
+ "sideEffects": false,
6
+ "type": "module",
7
+ "types": "./dist/src/index.d.ts",
8
+ "typesVersions": {
9
+ "*": {
10
+ "*": [
11
+ "*",
12
+ "dist/*",
13
+ "dist/src/*",
14
+ "dist/src/*/index"
15
+ ],
16
+ "src/*": [
17
+ "*",
18
+ "dist/*",
19
+ "dist/src/*",
20
+ "dist/src/*/index"
21
+ ]
22
+ }
23
+ },
24
+ "files": [
25
+ "src",
26
+ "dist",
27
+ "!dist/test",
28
+ "!**/*.tsbuildinfo"
29
+ ],
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/src/index.d.ts",
33
+ "import": "./dist/src/index.js"
34
+ }
35
+ },
36
+ "eslintConfig": {
37
+ "extends": "peerbit",
38
+ "parserOptions": {
39
+ "project": true,
40
+ "sourceType": "module"
41
+ },
42
+ "ignorePatterns": [
43
+ "!.aegir.js",
44
+ "test/ts-use",
45
+ "*.d.ts"
46
+ ]
47
+ },
48
+ "publishConfig": {
49
+ "access": "public"
50
+ },
51
+ "dependencies": {
52
+ "pino": "^8.14.1"
53
+ },
54
+ "scripts": {
55
+ "clean": "aegir clean",
56
+ "build": "aegir build --no-bundle",
57
+ "test": "aegir test --target node",
58
+ "lint": "aegir lint"
59
+ },
60
+ "author": "dao.xyz",
61
+ "license": "MIT"
62
62
  }
package/src/index.ts CHANGED
@@ -1,46 +1,85 @@
1
1
  import pino from "pino";
2
2
 
3
- const isNode = typeof window === "undefined";
3
+ // Robust Node detection (true only in Node.js)
4
+ const isNode = typeof process !== "undefined" && !!process.versions?.node;
4
5
 
5
- export const getEnv = (key: any) => {
6
+ type LogLevel =
7
+ | "fatal"
8
+ | "error"
9
+ | "warn"
10
+ | "info"
11
+ | "debug"
12
+ | "trace"
13
+ | "silent";
14
+
15
+ /**
16
+ * Safely read an env var from:
17
+ * - Node: process.env
18
+ * - Webpack/CRA (browser/worker): globalThis.process.env (if defined by bundler)
19
+ * - Vite (browser/worker): import.meta.env
20
+ */
21
+ export const getEnv = (key: string): string | undefined => {
6
22
  if (isNode) {
7
- // node
8
- return process.env[key];
23
+ return process.env?.[key];
24
+ }
25
+
26
+ // Some bundlers (Webpack/CRA) inject a process shim
27
+ const g: any = typeof globalThis !== "undefined" ? (globalThis as any) : {};
28
+ if (g.process?.env && typeof g.process.env[key] !== "undefined") {
29
+ return g.process.env[key];
9
30
  }
10
- // browser
11
- return window.process?.env?.[key];
31
+
32
+ // Vite exposes import.meta.env
33
+ let metaEnv: any;
34
+ try {
35
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
36
+ // @ts-ignore - import.meta may not exist in TS/node type context
37
+ metaEnv = import.meta?.env;
38
+ } catch {
39
+ // ignore if not supported
40
+ }
41
+ if (metaEnv && typeof metaEnv[key] !== "undefined") {
42
+ return metaEnv[key];
43
+ }
44
+
45
+ return undefined;
12
46
  };
13
- export const getLogLevel = () => {
47
+
48
+ export const getLogLevel = (): LogLevel | undefined => {
14
49
  const level = getEnv("LOG_LEVEL") || getEnv("REACT_APP_LOG_LEVEL");
15
- if (!level) {
16
- return undefined;
17
- }
18
- const levels = ["fatal", "error", "warn", "info", "debug", "trace"];
19
- if (!levels.includes(level)) {
50
+ if (!level) return undefined;
51
+
52
+ const levels: LogLevel[] = [
53
+ "fatal",
54
+ "error",
55
+ "warn",
56
+ "info",
57
+ "debug",
58
+ "trace",
59
+ "silent",
60
+ ];
61
+ if (!levels.includes(level as LogLevel)) {
20
62
  throw new Error(
21
- "Unexpected LOG_LEVEL: " +
22
- level +
23
- ". Expecting one of: " +
24
- JSON.stringify(levels),
63
+ `Unexpected LOG_LEVEL: ${level}. Expecting one of: ${JSON.stringify(levels)}`,
25
64
  );
26
65
  }
27
- return level;
66
+ return level as LogLevel;
28
67
  };
29
68
 
30
- const logger = (options?: { module?: string; level?: string }) => {
31
- let logger = pino();
69
+ const logger = (options?: { module?: string; level?: LogLevel }) => {
70
+ // In browsers/workers, pino's browser build logs to console by default.
71
+ let base = pino();
72
+
32
73
  if (options?.module) {
33
- logger = logger.child({ module: options.module });
74
+ base = base.child({ module: options.module });
34
75
  }
35
- if (options?.level) {
36
- logger.level = options.level;
37
- } else {
38
- const logLevel = getLogLevel();
39
- if (logLevel) {
40
- logger.level = logLevel;
41
- }
76
+
77
+ const level = options?.level ?? getLogLevel();
78
+ if (level) {
79
+ base.level = level;
42
80
  }
43
- return logger;
81
+
82
+ return base;
44
83
  };
45
84
 
46
85
  export { logger };