@ricdotnet/logger 0.0.7 → 0.0.8
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/dist/src/Constants.d.ts +25 -0
- package/dist/src/Constants.js +30 -0
- package/dist/src/Constants.js.map +1 -0
- package/dist/src/Logger.d.ts +10 -0
- package/dist/src/Logger.js +55 -0
- package/dist/src/Logger.js.map +1 -0
- package/dist/src/Types.d.ts +4 -0
- package/dist/src/Types.js +3 -0
- package/dist/src/Types.js.map +1 -0
- package/package.json +1 -1
- package/src/Constants.ts +27 -0
- package/src/Logger.ts +61 -0
- package/src/Types.ts +5 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare enum Constants {
|
|
2
|
+
RESET = "\u001B[0m",
|
|
3
|
+
BRIGHT = "\u001B[1m",
|
|
4
|
+
DIM = "\u001B[2m",
|
|
5
|
+
UNDERSCORE = "\u001B[4m",
|
|
6
|
+
BLINK = "\u001B[5m",
|
|
7
|
+
REVERSE = "\u001B[7m",
|
|
8
|
+
HIDDEN = "\u001B[8m",
|
|
9
|
+
TEXT_BLACK = "\u001B[30m",
|
|
10
|
+
TEXT_RED = "\u001B[31m",
|
|
11
|
+
TEXT_GREEN = "\u001B[32m",
|
|
12
|
+
TEXT_YELLOW = "\u001B[33m",
|
|
13
|
+
TEXT_BLUE = "\u001B[34m",
|
|
14
|
+
TEXT_MAGENTA = "\u001B[35m",
|
|
15
|
+
TEXT_CYAN = "\u001B[36m",
|
|
16
|
+
TEXT_WHITE = "\u001B[37m",
|
|
17
|
+
BG_BLACK = "\u001B[40m",
|
|
18
|
+
BG_RED = "\u001B[41m",
|
|
19
|
+
BG_GREEN = "\u001B[42m",
|
|
20
|
+
BG_YELLOW = "\u001B[43m",
|
|
21
|
+
BG_BLUE = "\u001B[44m",
|
|
22
|
+
BG_MAGENTA = "\u001B[45m",
|
|
23
|
+
BG_CYAN = "\u001B[46m",
|
|
24
|
+
BG_WHITE = "\u001B[47m"
|
|
25
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Constants = void 0;
|
|
4
|
+
var Constants;
|
|
5
|
+
(function (Constants) {
|
|
6
|
+
Constants["RESET"] = "\u001B[0m";
|
|
7
|
+
Constants["BRIGHT"] = "\u001B[1m";
|
|
8
|
+
Constants["DIM"] = "\u001B[2m";
|
|
9
|
+
Constants["UNDERSCORE"] = "\u001B[4m";
|
|
10
|
+
Constants["BLINK"] = "\u001B[5m";
|
|
11
|
+
Constants["REVERSE"] = "\u001B[7m";
|
|
12
|
+
Constants["HIDDEN"] = "\u001B[8m";
|
|
13
|
+
Constants["TEXT_BLACK"] = "\u001B[30m";
|
|
14
|
+
Constants["TEXT_RED"] = "\u001B[31m";
|
|
15
|
+
Constants["TEXT_GREEN"] = "\u001B[32m";
|
|
16
|
+
Constants["TEXT_YELLOW"] = "\u001B[33m";
|
|
17
|
+
Constants["TEXT_BLUE"] = "\u001B[34m";
|
|
18
|
+
Constants["TEXT_MAGENTA"] = "\u001B[35m";
|
|
19
|
+
Constants["TEXT_CYAN"] = "\u001B[36m";
|
|
20
|
+
Constants["TEXT_WHITE"] = "\u001B[37m";
|
|
21
|
+
Constants["BG_BLACK"] = "\u001B[40m";
|
|
22
|
+
Constants["BG_RED"] = "\u001B[41m";
|
|
23
|
+
Constants["BG_GREEN"] = "\u001B[42m";
|
|
24
|
+
Constants["BG_YELLOW"] = "\u001B[43m";
|
|
25
|
+
Constants["BG_BLUE"] = "\u001B[44m";
|
|
26
|
+
Constants["BG_MAGENTA"] = "\u001B[45m";
|
|
27
|
+
Constants["BG_CYAN"] = "\u001B[46m";
|
|
28
|
+
Constants["BG_WHITE"] = "\u001B[47m";
|
|
29
|
+
})(Constants = exports.Constants || (exports.Constants = {}));
|
|
30
|
+
//# sourceMappingURL=Constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Constants.js","sourceRoot":"./","sources":["src/Constants.ts"],"names":[],"mappings":";;;AAAA,IAAY,SA0BX;AA1BD,WAAY,SAAS;IACnB,gCAAwB,CAAA;IACxB,iCAAwB,CAAA;IACxB,8BAAwB,CAAA;IACxB,qCAAwB,CAAA;IACxB,gCAAwB,CAAA;IACxB,kCAAwB,CAAA;IACxB,iCAAwB,CAAA;IAExB,sCAAyB,CAAA;IACzB,oCAAyB,CAAA;IACzB,sCAAyB,CAAA;IACzB,uCAAyB,CAAA;IACzB,qCAAyB,CAAA;IACzB,wCAAyB,CAAA;IACzB,qCAAyB,CAAA;IACzB,sCAAyB,CAAA;IAEzB,oCAAyB,CAAA;IACzB,kCAAyB,CAAA;IACzB,oCAAyB,CAAA;IACzB,qCAAyB,CAAA;IACzB,mCAAyB,CAAA;IACzB,sCAAyB,CAAA;IACzB,mCAAyB,CAAA;IACzB,oCAAyB,CAAA;AAC3B,CAAC,EA1BW,SAAS,GAAT,iBAAS,KAAT,iBAAS,QA0BpB"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ILogger, Primitives } from './Types';
|
|
2
|
+
export declare class Logger {
|
|
3
|
+
private readonly logDir;
|
|
4
|
+
constructor(options?: ILogger);
|
|
5
|
+
checkLogDir(): Promise<void>;
|
|
6
|
+
info<T = string>(message: T | Primitives): Promise<void>;
|
|
7
|
+
warn<T = string>(message: T | Primitives): Promise<void>;
|
|
8
|
+
error<T = string>(message: T | Primitives): Promise<void>;
|
|
9
|
+
writeToFile(message: any, type: string): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Logger = void 0;
|
|
4
|
+
const fs = require("fs/promises");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const Constants_1 = require("./Constants");
|
|
7
|
+
class Logger {
|
|
8
|
+
logDir;
|
|
9
|
+
constructor(options) {
|
|
10
|
+
this.logDir = path.join(process.cwd(), options?.directory ?? 'Logs');
|
|
11
|
+
this.checkLogDir();
|
|
12
|
+
}
|
|
13
|
+
async checkLogDir() {
|
|
14
|
+
try {
|
|
15
|
+
await fs.mkdir(this.logDir);
|
|
16
|
+
await this.info(`Logs folder generated. Logging to: ${this.logDir}`);
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
await this.info(`Logs folder already exists. Logging to: ${this.logDir}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
async info(message) {
|
|
23
|
+
await this.writeToFile(message, 'info');
|
|
24
|
+
console.log(`${Constants_1.Constants.TEXT_BLUE}[INFO]: ${message}`);
|
|
25
|
+
}
|
|
26
|
+
async warn(message) {
|
|
27
|
+
await this.writeToFile(message, 'warn');
|
|
28
|
+
console.warn(`${Constants_1.Constants.TEXT_YELLOW}[WARN]: ${message}`);
|
|
29
|
+
}
|
|
30
|
+
async error(message) {
|
|
31
|
+
await this.writeToFile(message, 'error');
|
|
32
|
+
console.error(`${Constants_1.Constants.TEXT_RED}[ERROR]: ${message}`);
|
|
33
|
+
}
|
|
34
|
+
async writeToFile(message, type) {
|
|
35
|
+
const DAY = new Date().getDate();
|
|
36
|
+
const MONTH = new Date().getMonth() + 1;
|
|
37
|
+
const YEAR = new Date().getFullYear();
|
|
38
|
+
const HOURS = new Date().getHours();
|
|
39
|
+
const MINUTES = new Date().getMinutes();
|
|
40
|
+
const FULL_DATE = [DAY, MONTH, YEAR].join('-');
|
|
41
|
+
const FULL_TIME = [HOURS, MINUTES].join(':');
|
|
42
|
+
let logFile;
|
|
43
|
+
try {
|
|
44
|
+
logFile = await fs.readFile(path.join(this.logDir, `${FULL_DATE}-${type}.log`));
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
const data = 'Log Time: ' + FULL_TIME + '\n' + message + '\n\n';
|
|
48
|
+
await fs.writeFile(path.join(this.logDir, `${FULL_DATE}-${type}.log`), data);
|
|
49
|
+
}
|
|
50
|
+
const data = logFile + 'Log Time: ' + FULL_TIME + '\n' + message + '\n\n';
|
|
51
|
+
await fs.writeFile(path.join(this.logDir, `${FULL_DATE}-${type}.log`), data);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.Logger = Logger;
|
|
55
|
+
//# sourceMappingURL=Logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Logger.js","sourceRoot":"./","sources":["src/Logger.ts"],"names":[],"mappings":";;;AAAA,kCAAkC;AAClC,6BAA6B;AAC7B,2CAAwC;AAGxC,MAAa,MAAM;IAEA,MAAM,CAAS;IAEhC,YAAY,OAAiB;QAC3B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,OAAO,EAAE,SAAS,IAAI,MAAM,CAAC,CAAC;QACrE,IAAI,CAAC,WAAW,EAAE,CAAC;IACrB,CAAC;IAED,KAAK,CAAC,WAAW;QACf,IAAI;YACF,MAAM,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAC5B,MAAM,IAAI,CAAC,IAAI,CAAC,sCAAsC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;SACtE;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,IAAI,CAAC,IAAI,CAAC,2CAA2C,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;SAC3E;IACH,CAAC;IAED,KAAK,CAAC,IAAI,CAAa,OAAuB;QAC5C,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QACxC,OAAO,CAAC,GAAG,CAAC,GAAG,qBAAS,CAAC,SAAS,WAAW,OAAO,EAAE,CAAC,CAAC;IAC1D,CAAC;IAED,KAAK,CAAC,IAAI,CAAa,OAAuB;QAC5C,MAAM,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QACxC,OAAO,CAAC,IAAI,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,MAAM,EAAE,GAAG,SAAS,IAAI,IAAI,MAAM,CAAC,EAAE,IAAI,CAAC,CAAC;SAC9E;QAED,MAAM,IAAI,GAAG,OAAO,GAAG,YAAY,GAAG,SAAS,GAAG,IAAI,GAAG,OAAO,GAAG,MAAM,CAAC;QAC1E,MAAM,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,SAAS,IAAI,IAAI,MAAM,CAAC,EAAE,IAAI,CAAC,CAAC;IAC/E,CAAC;CAEF;AAvDD,wBAuDC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Types.js","sourceRoot":"./","sources":["src/Types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
package/src/Constants.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export enum Constants {
|
|
2
|
+
RESET = '\x1b[0m',
|
|
3
|
+
BRIGHT = '\x1b[1m',
|
|
4
|
+
DIM = '\x1b[2m',
|
|
5
|
+
UNDERSCORE = '\x1b[4m',
|
|
6
|
+
BLINK = '\x1b[5m',
|
|
7
|
+
REVERSE = '\x1b[7m',
|
|
8
|
+
HIDDEN = '\x1b[8m',
|
|
9
|
+
|
|
10
|
+
TEXT_BLACK = '\x1b[30m',
|
|
11
|
+
TEXT_RED = '\x1b[31m',
|
|
12
|
+
TEXT_GREEN = '\x1b[32m',
|
|
13
|
+
TEXT_YELLOW = '\x1b[33m',
|
|
14
|
+
TEXT_BLUE = '\x1b[34m',
|
|
15
|
+
TEXT_MAGENTA = '\x1b[35m',
|
|
16
|
+
TEXT_CYAN = '\x1b[36m',
|
|
17
|
+
TEXT_WHITE = '\x1b[37m',
|
|
18
|
+
|
|
19
|
+
BG_BLACK = '\x1b[40m',
|
|
20
|
+
BG_RED = '\x1b[41m',
|
|
21
|
+
BG_GREEN = '\x1b[42m',
|
|
22
|
+
BG_YELLOW = '\x1b[43m',
|
|
23
|
+
BG_BLUE = '\x1b[44m',
|
|
24
|
+
BG_MAGENTA = '\x1b[45m',
|
|
25
|
+
BG_CYAN = '\x1b[46m',
|
|
26
|
+
BG_WHITE = '\x1b[47m',
|
|
27
|
+
}
|
package/src/Logger.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import * as fs from 'fs/promises';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import { Constants } from './Constants';
|
|
4
|
+
import { ILogger, Primitives } from './Types';
|
|
5
|
+
|
|
6
|
+
export class Logger {
|
|
7
|
+
|
|
8
|
+
private readonly logDir: string;
|
|
9
|
+
|
|
10
|
+
constructor(options?: ILogger) {
|
|
11
|
+
this.logDir = path.join(process.cwd(), options?.directory ?? 'Logs');
|
|
12
|
+
this.checkLogDir();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async checkLogDir() {
|
|
16
|
+
try {
|
|
17
|
+
await fs.mkdir(this.logDir);
|
|
18
|
+
await this.info(`Logs folder generated. Logging to: ${this.logDir}`);
|
|
19
|
+
} catch (error) {
|
|
20
|
+
await this.info(`Logs folder already exists. Logging to: ${this.logDir}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async info<T = string>(message: T | Primitives) {
|
|
25
|
+
await this.writeToFile(message, 'info');
|
|
26
|
+
console.log(`${Constants.TEXT_BLUE}[INFO]: ${message}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async warn<T = string>(message: T | Primitives) {
|
|
30
|
+
await this.writeToFile(message, 'warn');
|
|
31
|
+
console.warn(`${Constants.TEXT_YELLOW}[WARN]: ${message}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async error<T = string>(message: T | Primitives) {
|
|
35
|
+
await this.writeToFile(message, 'error');
|
|
36
|
+
console.error(`${Constants.TEXT_RED}[ERROR]: ${message}`);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async writeToFile(message: any, type: string) {
|
|
40
|
+
const DAY: number = new Date().getDate();
|
|
41
|
+
const MONTH: number = new Date().getMonth() + 1; // +1 because it goes 0-11
|
|
42
|
+
const YEAR: number = new Date().getFullYear();
|
|
43
|
+
const HOURS: number = new Date().getHours();
|
|
44
|
+
const MINUTES: number = new Date().getMinutes();
|
|
45
|
+
|
|
46
|
+
const FULL_DATE: string = [DAY, MONTH, YEAR].join('-');
|
|
47
|
+
const FULL_TIME: string = [HOURS, MINUTES].join(':');
|
|
48
|
+
|
|
49
|
+
let logFile;
|
|
50
|
+
try {
|
|
51
|
+
logFile = await fs.readFile(path.join(this.logDir, `${FULL_DATE}-${type}.log`));
|
|
52
|
+
} catch (e) {
|
|
53
|
+
const data = 'Log Time: ' + FULL_TIME + '\n' + message + '\n\n';
|
|
54
|
+
await fs.writeFile(path.join(this.logDir, `${FULL_DATE}-${type}.log`), data);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const data = logFile + 'Log Time: ' + FULL_TIME + '\n' + message + '\n\n';
|
|
58
|
+
await fs.writeFile(path.join(this.logDir, `${FULL_DATE}-${type}.log`), data);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
}
|