lhisp-logger 2.2.7 → 3.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/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "lhisp-logger",
3
- "version": "2.2.7",
3
+ "version": "3.0.0",
4
4
  "description": "",
5
5
  "main": "src/lhisp-logger",
6
6
  "types": "src/lhisp-logger.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
+ "publish": "rm -rfv dist/ && npm run build && cd dist && cp ../package* . && npm publish && cd .. && rm -rfv dist/",
9
10
  "test": "echo NO TESTS"
10
11
  },
11
12
  "repository": {
@@ -0,0 +1 @@
1
+ export declare function deepSerializeAxiosErrors(value: unknown, seen?: WeakSet<object>): unknown;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ // Recursively serialize any axios error present in the provided value.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.deepSerializeAxiosErrors = deepSerializeAxiosErrors;
5
+ const axiosErrorSerializer_1 = require("./axiosErrorSerializer");
6
+ const isAxiosError_1 = require("./isAxiosError");
7
+ // Handles arrays, plain objects, and protects against circular structures.
8
+ function deepSerializeAxiosErrors(value, seen) {
9
+ // Guard against null which has typeof 'object' but is invalid for WeakSet
10
+ if (value === null || typeof value !== "object")
11
+ return value;
12
+ // axios errors have a distinctive shape; use isAxiosError to detect
13
+ if ((0, isAxiosError_1.isAxiosError)(value)) {
14
+ return (0, axiosErrorSerializer_1.axiosErrorSerializer)(value);
15
+ }
16
+ const obj = value;
17
+ const localSeen = seen || new WeakSet();
18
+ if (localSeen.has(obj)) {
19
+ // Avoid infinite recursion on circular structures
20
+ return "[Circular]";
21
+ }
22
+ localSeen.add(obj);
23
+ if (Array.isArray(value)) {
24
+ return value.map((item) => deepSerializeAxiosErrors(item, localSeen));
25
+ }
26
+ // Keep non-plain objects (Date, Buffer, Error, etc.) untouched except axios errors above
27
+ const proto = Object.getPrototypeOf(obj);
28
+ const isPlain = proto === Object.prototype || proto === null;
29
+ if (!isPlain) {
30
+ return value;
31
+ }
32
+ const out = {};
33
+ for (const [k, v] of Object.entries(value)) {
34
+ out[k] = deepSerializeAxiosErrors(v, localSeen);
35
+ }
36
+ return out;
37
+ }
@@ -0,0 +1 @@
1
+ export declare function getHostPackageName(): string | undefined;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getHostPackageName = getHostPackageName;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ function getHostPackageName() {
10
+ try {
11
+ const pkgPath = path_1.default.resolve(process.cwd(), "package.json");
12
+ const content = fs_1.default.readFileSync(pkgPath, "utf8");
13
+ const pkg = JSON.parse(content);
14
+ return typeof (pkg === null || pkg === void 0 ? void 0 : pkg.name) === "string" ? pkg.name : undefined;
15
+ }
16
+ catch (_a) {
17
+ return undefined;
18
+ }
19
+ }
@@ -1,4 +1,4 @@
1
- import { Logger } from "pino";
1
+ import pino from "pino";
2
2
  export interface LhLoggerOptions {
3
3
  name?: string;
4
4
  level?: string;
@@ -14,5 +14,6 @@ export interface LhLoggerOptions {
14
14
  labels?: Record<string, string>;
15
15
  propsToLabels?: string[];
16
16
  }
17
- export declare function lhLogger(options?: LhLoggerOptions): Logger;
18
- export default lhLogger;
17
+ declare const logger: pino.Logger<never, boolean>;
18
+ export type Logger = typeof logger;
19
+ export default logger;
@@ -3,24 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.lhLogger = lhLogger;
7
6
  const fs_1 = __importDefault(require("fs"));
8
7
  const os_1 = __importDefault(require("os"));
9
- const path_1 = __importDefault(require("path"));
10
8
  const pino_1 = __importDefault(require("pino"));
11
9
  const axiosErrorSerializer_1 = require("./axiosErrorSerializer");
10
+ const deepSerializeAxiosErrors_1 = require("./deepSerializeAxiosErrors");
11
+ const getHostPackageName_1 = require("./getHostPackageName");
12
12
  const isAxiosError_1 = require("./isAxiosError");
13
- function getHostPackageName() {
14
- try {
15
- const pkgPath = path_1.default.resolve(process.cwd(), "package.json");
16
- const content = fs_1.default.readFileSync(pkgPath, "utf8");
17
- const pkg = JSON.parse(content);
18
- return typeof (pkg === null || pkg === void 0 ? void 0 : pkg.name) === "string" ? pkg.name : undefined;
19
- }
20
- catch (_a) {
21
- return undefined;
22
- }
23
- }
24
13
  function getEtcHostname() {
25
14
  try {
26
15
  const content = fs_1.default.readFileSync("/etc/hostname", "utf8");
@@ -31,103 +20,55 @@ function getEtcHostname() {
31
20
  return undefined;
32
21
  }
33
22
  }
34
- function lhLogger(options = {}) {
35
- const { name = process.env.LOG_NAME || undefined, level = process.env.LOG_LEVEL || "info", destination = process.env.LOG_DESTINATION, sync = !!process.env.LOG_SYNC, pretty = true, colorize = true, ignore = "pid,hostname", translateTime = "yyyy-mm-dd HH:MM:ss", singleLine = true, lokiEnabled = (process.env.LOG_LOKI_ENABLED || "false").toLowerCase() !== "false", lokiUrl = process.env.LOG_LOKI_URL ||
36
- `${process.env.LOG_LOKI_PROTOCOL || "http"}://${process.env.LOG_LOKI_HOST || "logs.lhprovedor.com.br"}:${process.env.LOG_LOKI_PORT || "3100"}`, } = options;
37
- const baseConfig = {
38
- name,
23
+ const level = process.env.LOG_LEVEL || "info";
24
+ const targets = [];
25
+ targets.push({
26
+ level,
27
+ target: "pino-pretty",
28
+ options: {
29
+ colorize: true,
30
+ ignore: "pid,hostname",
31
+ translateTime: "yyyy-mm-dd HH:MM:ss",
32
+ singleLine: true,
33
+ },
34
+ });
35
+ const appName = (0, getHostPackageName_1.getHostPackageName)() || "lhisp-app";
36
+ const lokiEnabled = (process.env.LOG_LOKI_ENABLED || "false").toLowerCase() !== "false";
37
+ if (lokiEnabled) {
38
+ const lokiUrl = process.env.LOG_LOKI_URL ||
39
+ `${process.env.LOG_LOKI_PROTOCOL || "http"}://${process.env.LOG_LOKI_HOST || "logs.lhprovedor.com.br"}:${process.env.LOG_LOKI_PORT || "3100"}`;
40
+ targets.push({
39
41
  level,
40
- serializers: {
41
- err: (error) => ((0, isAxiosError_1.isAxiosError)(error) ? (0, axiosErrorSerializer_1.axiosErrorSerializer)(error) : pino_1.default.stdSerializers.err(error)),
42
- error: (error) => ((0, isAxiosError_1.isAxiosError)(error) ? (0, axiosErrorSerializer_1.axiosErrorSerializer)(error) : pino_1.default.stdSerializers.err(error)),
43
- axiosError: axiosErrorSerializer_1.axiosErrorSerializer,
44
- },
45
- formatters: {
46
- // Ensure axios errors anywhere in the log object are serialized
47
- log: (obj) => {
48
- return deepSerializeAxiosErrors(obj);
42
+ target: "pino-loki",
43
+ options: {
44
+ host: lokiUrl,
45
+ labels: {
46
+ app: appName,
47
+ env: process.env.NODE_ENV || "development",
48
+ servidor: process.env.SERVERTAG || getEtcHostname() || os_1.default.hostname(),
49
49
  },
50
+ propsToLabels: ["dbname", "traceId", ""],
51
+ interval: 5,
52
+ batching: false,
53
+ timeout: 5000,
50
54
  },
51
- };
52
- const targets = [];
53
- if (pretty) {
54
- targets.push({
55
- level,
56
- target: "pino-pretty",
57
- options: {
58
- colorize,
59
- ignore,
60
- translateTime,
61
- singleLine,
62
- },
63
- });
64
- }
65
- if (lokiEnabled) {
66
- const appName = getHostPackageName() || name || process.env.npm_package_name || "lhisp-app";
67
- targets.push({
68
- level,
69
- target: "pino-loki",
70
- options: {
71
- host: lokiUrl,
72
- labels: Object.assign({ app: appName, env: process.env.NODE_ENV || "development", servidor: process.env.SERVERTAG || getEtcHostname() || os_1.default.hostname() }, options.labels),
73
- propsToLabels: ["dbname", "traceId", ...(options.propsToLabels || [])],
74
- interval: 5,
75
- batching: true,
76
- },
77
- });
78
- }
79
- if (destination) {
80
- targets.push({
81
- level,
82
- target: "pino/file",
83
- options: {
84
- destination,
85
- mkdir: true,
86
- append: true,
87
- sync,
88
- },
89
- });
90
- }
91
- if (targets.length) {
92
- const transport = pino_1.default.transport({ targets });
93
- return (0, pino_1.default)(baseConfig, transport);
94
- }
95
- return (0, pino_1.default)(baseConfig, destination
96
- ? pino_1.default.destination({ dest: destination, sync })
97
- : sync
98
- ? pino_1.default.destination({ sync: true })
99
- : pino_1.default.destination(1));
100
- }
101
- exports.default = lhLogger;
102
- // Recursively serialize any axios error present in the provided value.
103
- // Handles arrays, plain objects, and protects against circular structures.
104
- function deepSerializeAxiosErrors(value, seen) {
105
- // Guard against null which has typeof 'object' but is invalid for WeakSet
106
- if (value === null || typeof value !== "object")
107
- return value;
108
- // axios errors have a distinctive shape; use isAxiosError to detect
109
- if ((0, isAxiosError_1.isAxiosError)(value)) {
110
- return (0, axiosErrorSerializer_1.axiosErrorSerializer)(value);
111
- }
112
- const obj = value;
113
- const localSeen = seen || new WeakSet();
114
- if (localSeen.has(obj)) {
115
- // Avoid infinite recursion on circular structures
116
- return "[Circular]";
117
- }
118
- localSeen.add(obj);
119
- if (Array.isArray(value)) {
120
- return value.map((item) => deepSerializeAxiosErrors(item, localSeen));
121
- }
122
- // Keep non-plain objects (Date, Buffer, Error, etc.) untouched except axios errors above
123
- const proto = Object.getPrototypeOf(obj);
124
- const isPlain = proto === Object.prototype || proto === null;
125
- if (!isPlain) {
126
- return value;
127
- }
128
- const out = {};
129
- for (const [k, v] of Object.entries(value)) {
130
- out[k] = deepSerializeAxiosErrors(v, localSeen);
131
- }
132
- return out;
55
+ });
133
56
  }
57
+ const name = process.env.LOG_NAME || undefined;
58
+ const transport = pino_1.default.transport({ targets });
59
+ const logger = (0, pino_1.default)({
60
+ name: appName,
61
+ level,
62
+ serializers: {
63
+ err: (error) => ((0, isAxiosError_1.isAxiosError)(error) ? (0, axiosErrorSerializer_1.axiosErrorSerializer)(error) : pino_1.default.stdSerializers.err(error)),
64
+ error: (error) => ((0, isAxiosError_1.isAxiosError)(error) ? (0, axiosErrorSerializer_1.axiosErrorSerializer)(error) : pino_1.default.stdSerializers.err(error)),
65
+ axiosError: axiosErrorSerializer_1.axiosErrorSerializer,
66
+ },
67
+ formatters: {
68
+ // Ensure axios errors anywhere in the log object are serialized
69
+ log: (obj) => {
70
+ return (0, deepSerializeAxiosErrors_1.deepSerializeAxiosErrors)(obj);
71
+ },
72
+ },
73
+ }, transport);
74
+ exports.default = logger;