@levrbet/shared 0.2.16 → 0.2.18
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.
|
@@ -2,7 +2,7 @@ import winston from "winston";
|
|
|
2
2
|
export interface LoggerOptions {
|
|
3
3
|
service?: string;
|
|
4
4
|
level?: string;
|
|
5
|
-
additionalMeta?: Record<string,
|
|
5
|
+
additionalMeta?: Record<string, unknown>;
|
|
6
6
|
}
|
|
7
7
|
export declare const createLogger: (options?: LoggerOptions) => winston.Logger;
|
|
8
8
|
declare const logger: winston.Logger;
|
|
@@ -6,6 +6,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.stream = exports.createStream = exports.createLogger = void 0;
|
|
7
7
|
const winston_1 = __importDefault(require("winston"));
|
|
8
8
|
const dotenv_1 = require("./dotenv");
|
|
9
|
+
const types_1 = require("../../core/types");
|
|
10
|
+
const util_1 = __importDefault(require("util"));
|
|
11
|
+
const SPLAT = Symbol.for("splat");
|
|
9
12
|
/**
|
|
10
13
|
* @dev exporting a configured Winston logger instance
|
|
11
14
|
*
|
|
@@ -31,93 +34,85 @@ const dotenv_1 = require("./dotenv");
|
|
|
31
34
|
logger.info("Using default logger")
|
|
32
35
|
*
|
|
33
36
|
*/
|
|
34
|
-
const { combine, timestamp, errors, json,
|
|
37
|
+
const { combine, timestamp, errors, json, splat, colorize, printf } = winston_1.default.format;
|
|
35
38
|
// Determine log level based on environment
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
http: "\x1b[35m", // magenta
|
|
56
|
-
verbose: "\x1b[90m", // gray
|
|
57
|
-
debug: "\x1b[34m", // blue
|
|
58
|
-
silly: "\x1b[90m", // gray
|
|
59
|
-
};
|
|
60
|
-
const reset = "\x1b[0m";
|
|
61
|
-
// Custom format for console output in non-production
|
|
62
|
-
const consoleFormat = printf((info) => {
|
|
63
|
-
const { level, message, timestamp, stack, service, ...metadata } = info;
|
|
64
|
-
const color = colors[level] || "";
|
|
65
|
-
const coloredLevel = dotenv_1.LEVR_ENV === "prod" ? level : `${color}${level}${reset}`;
|
|
66
|
-
let msg = `${timestamp} [${coloredLevel}]`;
|
|
67
|
-
if (service) {
|
|
68
|
-
msg += ` [${service}]`;
|
|
69
|
-
}
|
|
70
|
-
// Simple message handling
|
|
71
|
-
if (typeof message === "string") {
|
|
72
|
-
msg += `: ${message}`;
|
|
73
|
-
}
|
|
74
|
-
else if (message !== undefined && message !== null) {
|
|
75
|
-
try {
|
|
76
|
-
msg += `: ${JSON.stringify(message)}`;
|
|
77
|
-
}
|
|
78
|
-
catch {
|
|
79
|
-
msg += `: ${String(message)}`;
|
|
39
|
+
const LOG_LEVEL = {
|
|
40
|
+
local: "debug",
|
|
41
|
+
dev: "debug",
|
|
42
|
+
staging: "info",
|
|
43
|
+
prod: "error",
|
|
44
|
+
}[dotenv_1.LEVR_ENV];
|
|
45
|
+
const KEY_COLOR = "\x1b[33m"; // yellow
|
|
46
|
+
const RESET_COLOR = "\x1b[0m";
|
|
47
|
+
const readableFormat = printf((info) => {
|
|
48
|
+
const { level, message, timestamp, service, stack, ...rest } = info;
|
|
49
|
+
const ts = typeof timestamp === "string" ? timestamp : new Date().toISOString();
|
|
50
|
+
const lvl = typeof level === "string" ? level : "info";
|
|
51
|
+
const svc = typeof service === "string" ? service : undefined;
|
|
52
|
+
let msgStr = typeof message === "string" ? message : util_1.default.inspect(message, { depth: 4 });
|
|
53
|
+
const splatArgs = Array.isArray(info[SPLAT]) ? info[SPLAT] : [];
|
|
54
|
+
const metaToPrint = {};
|
|
55
|
+
for (const arg of splatArgs) {
|
|
56
|
+
if (typeof arg === "string" || typeof arg === "number" || typeof arg === "boolean") {
|
|
57
|
+
msgStr += " " + String(arg);
|
|
80
58
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
msg += `\n${stack}`;
|
|
85
|
-
}
|
|
86
|
-
// Handle splat metadata (printf-style arguments)
|
|
87
|
-
const splatData = metadata[Symbol.for("splat")];
|
|
88
|
-
if (Array.isArray(splatData) && splatData.length > 0) {
|
|
89
|
-
splatData.forEach((item) => {
|
|
90
|
-
try {
|
|
91
|
-
msg += ` ${typeof item === "string" ? item : JSON.stringify(item)}`;
|
|
59
|
+
else if (Array.isArray(arg)) {
|
|
60
|
+
if (!metaToPrint["args"]) {
|
|
61
|
+
metaToPrint["args"] = [];
|
|
92
62
|
}
|
|
93
|
-
|
|
94
|
-
|
|
63
|
+
;
|
|
64
|
+
metaToPrint["args"].push(arg);
|
|
65
|
+
}
|
|
66
|
+
else if (arg && typeof arg === "object") {
|
|
67
|
+
if (!metaToPrint["args"]) {
|
|
68
|
+
metaToPrint["args"] = [];
|
|
95
69
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
// Add remaining metadata if present
|
|
99
|
-
const relevantKeys = Object.keys(metadata).filter((key) => !["level", "message", "timestamp", "service", "environment", "splat"].includes(key));
|
|
100
|
-
if (relevantKeys.length > 0) {
|
|
101
|
-
const metaObj = Object.fromEntries(relevantKeys.map((key) => [key, metadata[key]]));
|
|
102
|
-
try {
|
|
103
|
-
msg += `\n${JSON.stringify(metaObj, null, 2)}`;
|
|
70
|
+
;
|
|
71
|
+
metaToPrint["args"].push(arg);
|
|
104
72
|
}
|
|
105
|
-
|
|
106
|
-
|
|
73
|
+
else {
|
|
74
|
+
if (!metaToPrint["args"]) {
|
|
75
|
+
metaToPrint["args"] = [];
|
|
76
|
+
}
|
|
77
|
+
;
|
|
78
|
+
metaToPrint["args"].push(arg);
|
|
107
79
|
}
|
|
108
80
|
}
|
|
109
|
-
|
|
81
|
+
for (const [key, value] of Object.entries(rest)) {
|
|
82
|
+
if (["service", "environment", "level", "message", "timestamp"].includes(key))
|
|
83
|
+
continue;
|
|
84
|
+
if (/^\d+$/.test(key))
|
|
85
|
+
continue;
|
|
86
|
+
metaToPrint[key] = value;
|
|
87
|
+
}
|
|
88
|
+
let output = `${ts} ${lvl}`;
|
|
89
|
+
if (svc) {
|
|
90
|
+
output += ` [${svc}]`;
|
|
91
|
+
}
|
|
92
|
+
output += `: ${msgStr}`;
|
|
93
|
+
if (stack && typeof stack === "string") {
|
|
94
|
+
output += `\n ${stack.split("\n").join("\n ")}`;
|
|
95
|
+
}
|
|
96
|
+
if (Object.keys(metaToPrint).length > 0) {
|
|
97
|
+
const inspectOpts = { depth: 6, colors: false, compact: false, maxArrayLength: 100 };
|
|
98
|
+
// Later:
|
|
99
|
+
const pretty = util_1.default.inspect(metaToPrint, inspectOpts);
|
|
100
|
+
// Expand regex to catch more key forms:
|
|
101
|
+
const coloured = pretty.replace(/(^\s*)(["']?)([^"'\s]+)\2(?=\s*[:])/gm, (_match, indent, quote, key) => `${indent}${KEY_COLOR}${quote}${key}${quote}${RESET_COLOR}`);
|
|
102
|
+
output += `\n ${coloured.split("\n").join("\n ")}`;
|
|
103
|
+
}
|
|
104
|
+
return output;
|
|
110
105
|
});
|
|
111
|
-
|
|
112
|
-
const productionFormat = combine(timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), errors({ stack: true }), splat(), json());
|
|
113
|
-
//
|
|
114
|
-
const developmentFormat = combine(timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), errors({ stack: true }), splat(),
|
|
106
|
+
/// @dev Production format (structured JSON for OTEL)
|
|
107
|
+
const productionFormat = combine(timestamp({ format: "YYYY-MM-DD HH:mm:ss.SSS" }), errors({ stack: true }), splat(), json());
|
|
108
|
+
// Local development format (human-readable with colors)
|
|
109
|
+
const developmentFormat = combine(timestamp({ format: "YYYY-MM-DD HH:mm:ss.SSS" }), errors({ stack: true }), splat(), colorize({ all: false, level: true, message: false }), readableFormat);
|
|
115
110
|
const createLogger = (options = {}) => {
|
|
116
|
-
const { service = "@levrbet/shared", level =
|
|
111
|
+
const { service = "@levrbet/shared", level = LOG_LEVEL, additionalMeta = {} } = options;
|
|
117
112
|
const winstonLogger = winston_1.default.createLogger({
|
|
118
113
|
level,
|
|
119
114
|
levels: winston_1.default.config.npm.levels,
|
|
120
|
-
format: dotenv_1.LEVR_ENV ===
|
|
115
|
+
format: dotenv_1.LEVR_ENV === types_1.LevrEnv.PROD ? productionFormat : developmentFormat,
|
|
121
116
|
defaultMeta: {
|
|
122
117
|
service,
|
|
123
118
|
environment: dotenv_1.LEVR_ENV,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"winston.js","sourceRoot":"","sources":["../../../src/server/config/winston.ts"],"names":[],"mappings":";;;;;;AAAA,sDAA6B;AAC7B,qCAAmC;
|
|
1
|
+
{"version":3,"file":"winston.js","sourceRoot":"","sources":["../../../src/server/config/winston.ts"],"names":[],"mappings":";;;;;;AAAA,sDAA6B;AAC7B,qCAAmC;AACnC,4CAA0C;AAC1C,gDAAuB;AACvB,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;AAEjC;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,iBAAO,CAAC,MAAM,CAAA;AAEpF,2CAA2C;AAC3C,MAAM,SAAS,GAAG;IACd,KAAK,EAAE,OAAO;IACd,GAAG,EAAE,OAAO;IACZ,OAAO,EAAE,MAAM;IACf,IAAI,EAAE,OAAO;CAChB,CAAC,iBAAQ,CAAC,CAAA;AAEX,MAAM,SAAS,GAAG,UAAU,CAAA,CAAC,SAAS;AACtC,MAAM,WAAW,GAAG,SAAS,CAAA;AAE7B,MAAM,cAAc,GAAG,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE;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