@youpaichris/logger 6.0.4 → 6.0.6

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 +22 -14
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -8,19 +8,7 @@
8
8
  const fs = require('fs');
9
9
  const path = require('path');
10
10
 
11
- let e = __filename.split("/");
12
-
13
- const logFilePath = e.at(e.indexOf("node_modules") - 1) === 'logger' ? `log.log` : e.at(e.indexOf("node_modules") - 1) + `.log`;
14
- const logFile = fs.createWriteStream(logFilePath, {flags: 'a'});
15
-
16
- const successFilePath = e.at(e.indexOf("node_modules") - 1) === 'logger' ? `success.log` : e.at(e.indexOf("node_modules") - 1) + `-successs.log`;
17
- const successFile = fs.createWriteStream(successFilePath, {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'});
11
+ let logFilePath, logFile, successFilePath, successFile, errorFilePath, errorFile, criticalFilePath, criticalFile;
24
12
 
25
13
  const __log = console.log;
26
14
  const __log__ = function () {
@@ -139,6 +127,9 @@ class Logger {
139
127
  constructor({path = null, save = false}) {
140
128
  this.path = path ? path : getCallerPath();
141
129
  this.save = save;
130
+ if (save) {
131
+ this.initLogPath();
132
+ }
142
133
  }
143
134
 
144
135
  setPrefix(prefix) {
@@ -149,6 +140,23 @@ class Logger {
149
140
  this.#prefix = null;
150
141
  }
151
142
 
143
+ initLogPath() {
144
+
145
+ let e = process.platform === `win32` ? __filename.split("\\") : __filename.split("/");
146
+
147
+ logFilePath = e.at(e.indexOf("node_modules") - 1) === 'logger' ? `log.log` : e.at(e.indexOf("node_modules") - 1) + `.log`;
148
+ logFile = fs.createWriteStream(logFilePath, {flags: 'a'});
149
+
150
+ successFilePath = e.at(e.indexOf("node_modules") - 1) === 'logger' ? `success.log` : e.at(e.indexOf("node_modules") - 1) + `-success.log`;
151
+ successFile = fs.createWriteStream(successFilePath, {flags: 'a'});
152
+
153
+ errorFilePath = e.at(e.indexOf("node_modules") - 1) === 'logger' ? `error.log` : e.at(e.indexOf("node_modules") - 1) + `-error.log`;
154
+ errorFile = fs.createWriteStream(errorFilePath, {flags: 'a'});
155
+
156
+ criticalFilePath = e.at(e.indexOf("node_modules") - 1) === 'logger' ? `critical.log` : e.at(e.indexOf("node_modules") - 1) + `-critical.log`;
157
+ criticalFile = fs.createWriteStream(criticalFilePath, {flags: 'a'});
158
+ }
159
+
152
160
  info(...msg) {
153
161
  this.#prefix && msg.unshift(this.#prefix);
154
162
  let pathLine = this?.path ? `[${this.path}:${__line}]` : "Anonymous";
@@ -278,7 +286,7 @@ class Logger {
278
286
  logger.call("Hello World!");
279
287
  */
280
288
  process.on('exit', () => {
281
- logFile.end();
289
+ logFile?.end();
282
290
  });
283
291
  module.exports = Logger;
284
292
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@youpaichris/logger",
3
- "version": "6.0.4",
3
+ "version": "6.0.6",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {