hb-smart-logger 1.0.5 → 1.0.7

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