hb-smart-logger 1.0.4 → 1.0.6
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 +5 -2
- package/src/index.js +19 -27
package/package.json
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hb-smart-logger",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Production-ready Winston logger with daily rotation, safe JSON handling, and logger.log() alias",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
-
"publishConfig": {
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
7
9
|
"keywords": [
|
|
8
10
|
"logger",
|
|
9
11
|
"winston",
|
|
@@ -15,6 +17,7 @@
|
|
|
15
17
|
"author": "Hafiz Bilal",
|
|
16
18
|
"license": "MIT",
|
|
17
19
|
"dependencies": {
|
|
20
|
+
"dotenv": "^17.2.3",
|
|
18
21
|
"winston": "^3.13.0",
|
|
19
22
|
"winston-daily-rotate-file": "^5.0.0"
|
|
20
23
|
}
|
package/src/index.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
const fs = require("fs");
|
|
2
2
|
const path = require("path");
|
|
3
|
+
|
|
4
|
+
/* ---------------------------------------------------
|
|
5
|
+
LOAD ENV (dotenv)
|
|
6
|
+
--------------------------------------------------- */
|
|
7
|
+
const dotenv = require("dotenv");
|
|
8
|
+
dotenv.config();
|
|
9
|
+
|
|
3
10
|
const winston = require("winston");
|
|
4
11
|
const DailyRotateFile = require("winston-daily-rotate-file");
|
|
5
12
|
|
|
6
|
-
//
|
|
13
|
+
// Explicit debug opt-in
|
|
7
14
|
const DEBUG_ENABLED = String(process.env.DEBUG).toLowerCase() === "true";
|
|
8
15
|
|
|
9
16
|
// Ensure logs directory exists
|
|
@@ -58,10 +65,10 @@ const logFormat = winston.format.printf(
|
|
|
58
65
|
);
|
|
59
66
|
|
|
60
67
|
/* ---------------------------------------------------
|
|
61
|
-
BASE LOGGER
|
|
68
|
+
BASE LOGGER
|
|
62
69
|
--------------------------------------------------- */
|
|
63
70
|
const baseLogger = winston.createLogger({
|
|
64
|
-
level: "
|
|
71
|
+
level: "debug", // allow all levels internally
|
|
65
72
|
format: winston.format.combine(
|
|
66
73
|
winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
|
|
67
74
|
winston.format.errors({ stack: true }),
|
|
@@ -71,15 +78,15 @@ const baseLogger = winston.createLogger({
|
|
|
71
78
|
});
|
|
72
79
|
|
|
73
80
|
/* ---------------------------------------------------
|
|
74
|
-
|
|
81
|
+
FILE TRANSPORTS
|
|
75
82
|
--------------------------------------------------- */
|
|
76
83
|
|
|
77
|
-
// Combined logs
|
|
84
|
+
// Combined logs
|
|
78
85
|
baseLogger.add(
|
|
79
86
|
new DailyRotateFile({
|
|
80
87
|
dirname: path.join(logDir, "combined"),
|
|
81
88
|
filename: "combined-%DATE%.log",
|
|
82
|
-
level: "info",
|
|
89
|
+
level: DEBUG_ENABLED ? "debug" : "info",
|
|
83
90
|
datePattern: "YYYY-MM-DD",
|
|
84
91
|
zippedArchive: true,
|
|
85
92
|
maxSize: "10m",
|
|
@@ -87,7 +94,7 @@ baseLogger.add(
|
|
|
87
94
|
})
|
|
88
95
|
);
|
|
89
96
|
|
|
90
|
-
//
|
|
97
|
+
// Always-on per-level logs
|
|
91
98
|
["error", "warn", "info"].forEach((level) => {
|
|
92
99
|
baseLogger.add(
|
|
93
100
|
new DailyRotateFile({
|
|
@@ -102,7 +109,7 @@ baseLogger.add(
|
|
|
102
109
|
);
|
|
103
110
|
});
|
|
104
111
|
|
|
105
|
-
// Debug
|
|
112
|
+
// Debug file ONLY when enabled
|
|
106
113
|
if (DEBUG_ENABLED) {
|
|
107
114
|
baseLogger.add(
|
|
108
115
|
new DailyRotateFile({
|
|
@@ -118,13 +125,11 @@ if (DEBUG_ENABLED) {
|
|
|
118
125
|
}
|
|
119
126
|
|
|
120
127
|
/* ---------------------------------------------------
|
|
121
|
-
CONSOLE
|
|
128
|
+
CONSOLE TRANSPORT (NO DUPLICATES)
|
|
122
129
|
--------------------------------------------------- */
|
|
123
|
-
|
|
124
|
-
// Info+ always to console
|
|
125
130
|
baseLogger.add(
|
|
126
131
|
new winston.transports.Console({
|
|
127
|
-
level: "info",
|
|
132
|
+
level: DEBUG_ENABLED ? "debug" : "info",
|
|
128
133
|
format: winston.format.combine(
|
|
129
134
|
winston.format.colorize(),
|
|
130
135
|
winston.format.simple()
|
|
@@ -132,19 +137,6 @@ baseLogger.add(
|
|
|
132
137
|
})
|
|
133
138
|
);
|
|
134
139
|
|
|
135
|
-
// Debug to console ONLY if enabled
|
|
136
|
-
if (DEBUG_ENABLED) {
|
|
137
|
-
baseLogger.add(
|
|
138
|
-
new winston.transports.Console({
|
|
139
|
-
level: "debug",
|
|
140
|
-
format: winston.format.combine(
|
|
141
|
-
winston.format.colorize(),
|
|
142
|
-
winston.format.simple()
|
|
143
|
-
),
|
|
144
|
-
})
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
140
|
/* ---------------------------------------------------
|
|
149
141
|
WRAPPER (MULTI-ARGS + logger.log)
|
|
150
142
|
--------------------------------------------------- */
|
|
@@ -152,10 +144,10 @@ const wrapLogger = (logger) => {
|
|
|
152
144
|
const levels = ["info", "warn", "error", "debug"];
|
|
153
145
|
|
|
154
146
|
for (const level of levels) {
|
|
155
|
-
const original = logger[level]
|
|
147
|
+
const original = logger[level].bind(logger);
|
|
156
148
|
|
|
157
149
|
logger[level] = (...args) => {
|
|
158
|
-
// Hard-disable debug
|
|
150
|
+
// Hard-disable debug if DEBUG=false
|
|
159
151
|
if (level === "debug" && !DEBUG_ENABLED) return;
|
|
160
152
|
|
|
161
153
|
const merged = args
|