@youpaichris/logger 6.0.1 → 6.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/index.js +45 -5
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -8,11 +8,20 @@
8
8
  const fs = require('fs');
9
9
  const path = require('path');
10
10
 
11
+ let e = __filename.split("/");
11
12
 
12
- // const logFilePath = path.join(__dirname, `${path.basename(path.dirname(__filename))}.log`);
13
- const logFilePath = path.join(`${path.basename(path.dirname(module.parent ? module.parent.filename : __filename))}.log`);
13
+ const logFilePath = e.at(e.indexOf("node_modules") - 1) === 'logger' ? `log.log` : e.at(e.indexOf("node_modules") - 1) + `.log`;
14
14
  const logFile = fs.createWriteStream(logFilePath, {flags: 'a'});
15
15
 
16
+ const successsFilePath = e.at(e.indexOf("node_modules") - 1) === 'logger' ? `successs.log` : e.at(e.indexOf("node_modules") - 1) + `-successs.log`;
17
+ const successsFile = fs.createWriteStream(successsFilePath, {flags: 'a'});
18
+
19
+ const errorFilePath = e.at(e.indexOf("node_modules") - 1) === 'logger' ? `error.log` : e.at(e.indexOf("node_modules") - 1) + `-error.log`;
20
+ const errorFile = fs.createWriteStream(errorFilePath, {flags: 'a'});
21
+
22
+ const criticalFilePath = e.at(e.indexOf("node_modules") - 1) === 'logger' ? `critical.log` : e.at(e.indexOf("node_modules") - 1) + `-critical.log`;
23
+ const criticalFile = fs.createWriteStream(criticalFilePath, {flags: 'a'});
24
+
16
25
  const __log = console.log;
17
26
  const __log__ = function () {
18
27
  if (arguments[3] === '\x1B[41m') {
@@ -125,12 +134,23 @@ function getCallerPath() {
125
134
  }
126
135
 
127
136
  class Logger {
137
+ #prefix;
138
+
128
139
  constructor({path = null, save = false}) {
129
140
  this.path = path ? path : getCallerPath();
130
141
  this.save = save;
131
142
  }
132
143
 
144
+ setPrefix(prefix) {
145
+ this.#prefix = prefix;
146
+ }
147
+
148
+ unsetPrefix() {
149
+ this.#prefix = null;
150
+ }
151
+
133
152
  info(...msg) {
153
+ this.#prefix && msg.unshift(this.#prefix);
134
154
  let pathLine = this?.path ? `[${this.path}:${__line}]` : "Anonymous";
135
155
  loggerConfig.log(consoleStyles['green'][0] + dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date()),
136
156
  consoleStyles['red'][0] + consoleStyles['bold'][0] + "| " +
@@ -143,6 +163,7 @@ class Logger {
143
163
  };
144
164
 
145
165
  warn(...msg) {
166
+ this.#prefix && msg.unshift(this.#prefix);
146
167
  let pathLine = this?.path ? `[${this.path}:${__line}]` : "Anonymous";
147
168
  loggerConfig.log(consoleStyles['green'][0] + dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date()),
148
169
  consoleStyles['red'][0] + consoleStyles['bold'][0] + "| " +
@@ -155,6 +176,7 @@ class Logger {
155
176
  };
156
177
 
157
178
  error(...msg) {
179
+ this.#prefix && msg.unshift(this.#prefix);
158
180
  let pathLine = this?.path ? `[${this.path}:${__line}]` : "Anonymous";
159
181
  loggerConfig.error(consoleStyles['green'][0] + dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date()),
160
182
  consoleStyles['red'][0] + consoleStyles['bold'][0] + "| " +
@@ -163,10 +185,16 @@ class Logger {
163
185
  ...msg,
164
186
  consoleStyles['red'][1] + consoleStyles['bold'][1] + '\b'
165
187
  );
166
- this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | ERROR | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
188
+ if (this.save) {
189
+ let _msg = `${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | ERROR | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n';
190
+ logFile.write(_msg);
191
+ errorFile.write(_msg);
192
+ }
193
+
167
194
  };
168
195
 
169
196
  success(...msg) {
197
+ this.#prefix && msg.unshift(this.#prefix);
170
198
  let pathLine = this?.path ? `[${this.path}:${__line}]` : "Anonymous";
171
199
  loggerConfig.log(consoleStyles['green'][0] + dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date()),
172
200
  consoleStyles['red'][0] + consoleStyles['bold'][0] + "| " +
@@ -175,11 +203,17 @@ class Logger {
175
203
  ...msg,
176
204
  consoleStyles['green'][1] + consoleStyles['bold'][1] + '\b'
177
205
  );
178
- this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | SUCCESS | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
206
+ if (this.save) {
207
+ let _msg = `${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | SUCCESS | ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n';
208
+ logFile.write(_msg);
209
+ successsFile.write(_msg);
210
+ }
211
+
179
212
 
180
213
  };
181
214
 
182
215
  debug(...msg) {
216
+ this.#prefix && msg.unshift(this.#prefix);
183
217
  let pathLine = this?.path ? `[${this.path}:${__line}]` : "Anonymous";
184
218
  loggerConfig.log(consoleStyles['green'][0] + dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date()),
185
219
  consoleStyles['red'][0] + consoleStyles['bold'][0] + "| " +
@@ -193,6 +227,7 @@ class Logger {
193
227
  };
194
228
 
195
229
  call(...msg) {
230
+ this.#prefix && msg.unshift(this.#prefix);
196
231
  let pathLine = this?.path ? `[${this.path}:${__line}]` : "Anonymous";
197
232
  loggerConfig.log(consoleStyles['green'][0] + dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date()),
198
233
  consoleStyles['red'][0] + consoleStyles['bold'][0] + "| " +
@@ -206,6 +241,7 @@ class Logger {
206
241
  };
207
242
 
208
243
  critical(...msg) {
244
+ this.#prefix && msg.unshift(this.#prefix);
209
245
  let pathLine = this?.path ? `[${this.path}:${__line}]` : "Anonymous";
210
246
  loggerConfig.log(consoleStyles['green'][0] + dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date()),
211
247
  consoleStyles['red'][0] + consoleStyles['bold'][0] + "| " +
@@ -214,7 +250,11 @@ class Logger {
214
250
  ...msg
215
251
  // consoleStyles['redBG'][1] + consoleStyles['bold'][1] + '\b'
216
252
  );
217
- this.save && logFile.write(`${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | CRITICAL| ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n');
253
+ if (this.save) {
254
+ let _msg = `${dateFormat("YYYY-mm-dd HH:MM:SS.ms", new loggerConfig.Date())} | CRITICAL| ${pathLine.padEnd(22, " ")}${[...msg]}` + '\n';
255
+ logFile.write(_msg);
256
+ criticalFile.write(_msg);
257
+ }
218
258
 
219
259
  };
220
260
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@youpaichris/logger",
3
- "version": "6.0.1",
3
+ "version": "6.0.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {