emilsoftware-utilities 1.1.3 → 1.1.5

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/logger.d.ts CHANGED
@@ -6,15 +6,17 @@ export declare enum LogLevels {
6
6
  }
7
7
  export declare class Logger {
8
8
  private readonly winstonLogger;
9
- tag: string;
9
+ private readonly tag;
10
10
  private logFormat;
11
+ private replaceAll;
11
12
  constructor(tag: string);
12
13
  private getFileName;
13
- execStart(prefix?: string): any;
14
- execStop(prefix: string, startTime: any, error?: boolean): any;
15
- info(...data: any): void;
16
- debug(...data: any): void;
17
- log(...data: any): void;
18
- error(...data: any): void;
14
+ execStart(prefix?: string): number;
15
+ execStop(prefix: string, startTime: number, error?: boolean): void;
16
+ info(...data: Object[]): void;
17
+ debug(...data: Object[]): void;
18
+ log(...data: Object[]): void;
19
+ error(...data: Object[]): void;
20
+ private test;
19
21
  private print;
20
22
  }
package/logger.js CHANGED
@@ -48,10 +48,11 @@ var LogLevels;
48
48
  })(LogLevels || (exports.LogLevels = LogLevels = {}));
49
49
  var Logger = /** @class */ (function () {
50
50
  function Logger(tag) {
51
+ var _this = this;
51
52
  this.tag = "[UNTAGGED]";
52
53
  this.logFormat = winston_1.default.format.printf(function (tmp) {
53
54
  var time = tmp.time, file = tmp.file, level = tmp.level, message = tmp.message;
54
- return "".concat(JSON.stringify({ time: time, file: file === null || file === void 0 ? void 0 : file.replaceAll("\\", "/"), level: level, message: message }), ",");
55
+ return "".concat(JSON.stringify({ time: time, file: _this.replaceAll(file, "\\", "/"), level: level, message: message }), ",");
55
56
  });
56
57
  var fileName = this.getFileName();
57
58
  var logsDirectory = "logs";
@@ -65,6 +66,9 @@ var Logger = /** @class */ (function () {
65
66
  transports: [new winston_1.default.transports.File({ filename: logFilePath, format: this.logFormat })],
66
67
  });
67
68
  }
69
+ Logger.prototype.replaceAll = function (string, match, replacer) {
70
+ return string.replace(new RegExp(match, 'g'), replacer);
71
+ };
68
72
  Logger.prototype.getFileName = function () {
69
73
  var now = new Date();
70
74
  return now.getDate() + "-" + (now.getMonth() + 1) + "-" + now.getFullYear();
@@ -116,6 +120,13 @@ var Logger = /** @class */ (function () {
116
120
  }
117
121
  this.print.apply(this, __spreadArray([LogLevels.ERROR], data, false));
118
122
  };
123
+ Logger.prototype.test = function () {
124
+ var startTime = this.execStart("test");
125
+ this.execStop("test", startTime);
126
+ this.debug("test");
127
+ this.log("test");
128
+ this.error("test");
129
+ };
119
130
  Logger.prototype.print = function (level) {
120
131
  var _a;
121
132
  if (level === void 0) { level = LogLevels.INFO; }
@@ -124,13 +135,11 @@ var Logger = /** @class */ (function () {
124
135
  data[_i - 1] = arguments[_i];
125
136
  }
126
137
  var now = new Date();
127
- // Utilities.getNowDateString();
128
138
  this.winstonLogger.defaultMeta = {
129
139
  file: this.tag, time: now,
130
140
  level: level
131
141
  };
132
142
  (_a = this.winstonLogger)[level.toLowerCase()].apply(_a, data);
133
- // @ts-ignore
134
143
  console[level.toLowerCase()].apply(console, __spreadArray(["[".concat(level, "][").concat(now, "][").concat(this.tag.split("\\").pop(), "]")], data, false));
135
144
  };
136
145
  return Logger;
package/orm.js CHANGED
@@ -35,10 +35,12 @@ var testConnection = function (options) {
35
35
  return new Promise(function (resolve) {
36
36
  Firebird.attach(options, function (err, db) {
37
37
  if (err) {
38
- logger.error("La connessione con il DATABASE non è andata a buon fine.");
38
+ logger.error('La connessione con il DATABASE non è andata a buon fine.');
39
+ db.detach();
39
40
  return resolve(false);
40
41
  }
41
42
  logger.info("DATABASE connesso.");
43
+ db.detach();
42
44
  return resolve(true);
43
45
  });
44
46
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emilsoftware-utilities",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "description": "Utilities for EmilSoftware",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/logger.ts CHANGED
@@ -1,4 +1,3 @@
1
- import {Utilities} from "./utilities";
2
1
  import winston from "winston";
3
2
  import * as path from "path";
4
3
  import * as fs from "fs";
@@ -8,14 +7,18 @@ export enum LogLevels {
8
7
  }
9
8
 
10
9
  export class Logger {
11
- private readonly winstonLogger: any;
12
- public tag: string = "[UNTAGGED]";
10
+ private readonly winstonLogger: winston.Logger;
11
+ private readonly tag: string = "[UNTAGGED]";
13
12
 
14
13
  private logFormat: winston.Logform.Format = winston.format.printf((tmp: winston.Logform.TransformableInfo): string => {
15
14
  const {time, file, level, message} = tmp;
16
- return `${JSON.stringify({time, file: file?.replaceAll("\\", "/"), level, message})},`;
15
+ return `${JSON.stringify({time, file: this.replaceAll(file, "\\", "/"), level, message})},`;
17
16
  });
18
17
 
18
+ private replaceAll(string: string, match: string, replacer: string) {
19
+ return string.replace(new RegExp(match, 'g'), replacer);
20
+ }
21
+
19
22
  constructor(tag: string) {
20
23
  const fileName: string = this.getFileName();
21
24
  const logsDirectory = "logs";
@@ -31,17 +34,18 @@ export class Logger {
31
34
  });
32
35
  }
33
36
 
37
+
34
38
  private getFileName(): string {
35
39
  const now = new Date();
36
40
  return now.getDate() + "-" + (now.getMonth() + 1) + "-" + now.getFullYear();
37
41
  }
38
42
 
39
- public execStart(prefix: string = ""): any {
43
+ public execStart(prefix: string = ""): number {
40
44
  this.print(LogLevels.INFO, `${prefix} - Execution started`);
41
45
  return performance.now();
42
46
  }
43
47
 
44
- public execStop(prefix: string = "", startTime: any, error: boolean = false): any {
48
+ public execStop(prefix: string = "", startTime: number, error: boolean = false): void {
45
49
  switch (error) {
46
50
  case true: {
47
51
  this.print(LogLevels.ERROR, `${prefix} - Execution ended due to an error. Execution time: ${performance.now() - startTime} ms`);
@@ -54,30 +58,36 @@ export class Logger {
54
58
  }
55
59
  }
56
60
 
57
- public info(...data: any): void {
61
+ public info(...data: Object[]): void {
58
62
  this.print(LogLevels.INFO, ...data);
59
63
  }
60
64
 
61
- public debug(...data: any): void {
65
+ public debug(...data: Object[]): void {
62
66
  this.print(LogLevels.DEBUG, ...data);
63
67
  }
64
68
 
65
- public log(...data: any): void {
69
+ public log(...data: Object[]): void {
66
70
  this.print(LogLevels.LOG, ...data);
67
71
  }
68
72
 
69
- public error(...data: any): void {
73
+ public error(...data: Object[]): void {
70
74
  this.print(LogLevels.ERROR, ...data);
71
75
  }
72
76
 
73
- private print(level: LogLevels = LogLevels.INFO, ...data: any): void {
74
- const now = new Date();
75
- // Utilities.getNowDateString();
77
+ private test() {
78
+ let startTime = this.execStart("test");
79
+ this.execStop("test", startTime);
80
+ this.debug("test");
81
+ this.log("test");
82
+ this.error("test")
83
+ }
84
+
85
+ private print(level: LogLevels = LogLevels.INFO, ...data: Object[]): void {
86
+ const now: Date = new Date();
76
87
  this.winstonLogger.defaultMeta = {
77
88
  file: this.tag, time: now, level
78
89
  };
79
90
  this.winstonLogger[level.toLowerCase()](...data);
80
- // @ts-ignore
81
91
  console[level.toLowerCase()](`[${level}][${now}][${this.tag.split("\\").pop()}]`, ...data);
82
92
  }
83
93
  }
package/src/orm.ts CHANGED
@@ -10,12 +10,14 @@ const quote = (value: string): string => {
10
10
  const testConnection = (options: Options): Promise<any> => {
11
11
  const logger: Logger = new Logger(__filename);
12
12
  return new Promise((resolve): void => {
13
- Firebird.attach(options, (err: any, db: any): void => {
13
+ Firebird.attach(options, (err: Error, db: Database): void => {
14
14
  if (err) {
15
- logger.error("La connessione con il DATABASE non è andata a buon fine.");
15
+ logger.error('La connessione con il DATABASE non è andata a buon fine.');
16
+ db.detach();
16
17
  return resolve(false);
17
18
  }
18
19
  logger.info("DATABASE connesso.");
20
+ db.detach();
19
21
  return resolve(true);
20
22
  })
21
23
  })