@ricdotnet/logger 0.2.0 → 0.2.1
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/package.json +1 -1
- package/.prettierrc.json +0 -7
- package/Logs/28-12-2023-debug.log +0 -9
- package/Logs/28-12-2023-error.log +0 -3
- package/Logs/28-12-2023-info.log +0 -3
- package/Logs/28-12-2023-warn.log +0 -3
- package/Tests/Logger.spec.ts +0 -47
- package/Tests/WriteToFile.spec.ts +0 -43
- package/dist/src/Constants.d.ts +0 -25
- package/dist/src/Constants.js +0 -30
- package/dist/src/Constants.js.map +0 -1
- package/dist/src/Logger.d.ts +0 -15
- package/dist/src/Logger.js +0 -65
- package/dist/src/Logger.js.map +0 -1
- package/dist/src/Queue.d.ts +0 -11
- package/dist/src/Queue.js +0 -64
- package/dist/src/Queue.js.map +0 -1
- package/dist/src/Types.d.ts +0 -12
- package/dist/src/Types.js +0 -3
- package/dist/src/Types.js.map +0 -1
- package/src/Constants.ts +0 -27
- package/src/Logger.ts +0 -80
- package/src/Queue.ts +0 -78
- package/src/Types.ts +0 -14
package/package.json
CHANGED
package/.prettierrc.json
DELETED
package/Logs/28-12-2023-info.log
DELETED
package/Logs/28-12-2023-warn.log
DELETED
package/Tests/Logger.spec.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { Logger } from '../src/Logger';
|
|
2
|
-
|
|
3
|
-
const loggerSpy = {
|
|
4
|
-
debug: null,
|
|
5
|
-
info: null,
|
|
6
|
-
warn: null,
|
|
7
|
-
error: null,
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
describe('Logger test', () => {
|
|
11
|
-
beforeAll(() => {
|
|
12
|
-
new Logger();
|
|
13
|
-
|
|
14
|
-
loggerSpy.debug = jest.spyOn(Logger.get(), 'debug');
|
|
15
|
-
loggerSpy.info = jest.spyOn(Logger.get(), 'info');
|
|
16
|
-
loggerSpy.warn = jest.spyOn(Logger.get(), 'warn');
|
|
17
|
-
loggerSpy.error = jest.spyOn(Logger.get(), 'error');
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('can log a debug', async () => {
|
|
21
|
-
await Logger.get().debug('hello world');
|
|
22
|
-
|
|
23
|
-
expect(loggerSpy.debug).toHaveBeenCalledWith('hello world');
|
|
24
|
-
expect(loggerSpy.debug).toHaveBeenCalledTimes(1);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('can log an info', async () => {
|
|
28
|
-
await Logger.get().info('hello world');
|
|
29
|
-
|
|
30
|
-
expect(loggerSpy.info).toHaveBeenCalledWith('hello world');
|
|
31
|
-
expect(loggerSpy.info).toHaveBeenCalledTimes(1);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('can log a warning', async () => {
|
|
35
|
-
await Logger.get().warn('hello world');
|
|
36
|
-
|
|
37
|
-
expect(loggerSpy.warn).toHaveBeenCalledWith('hello world');
|
|
38
|
-
expect(loggerSpy.warn).toHaveBeenCalledTimes(1);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('can log an error', async () => {
|
|
42
|
-
await Logger.get().error('hello world');
|
|
43
|
-
|
|
44
|
-
expect(loggerSpy.error).toHaveBeenCalledWith('hello world');
|
|
45
|
-
expect(loggerSpy.error).toHaveBeenCalledTimes(1);
|
|
46
|
-
});
|
|
47
|
-
});
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs/promises';
|
|
2
|
-
import * as path from 'path';
|
|
3
|
-
import { Logger } from '../src/Logger';
|
|
4
|
-
import {Queue} from "../src/Queue";
|
|
5
|
-
|
|
6
|
-
const spy = {
|
|
7
|
-
writeToFile: null,
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
describe('Write to file test', () => {
|
|
11
|
-
beforeAll(() => {
|
|
12
|
-
new Logger({
|
|
13
|
-
logToFile: true,
|
|
14
|
-
logToConsole: false,
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
spy.writeToFile = jest.spyOn(Queue.prototype as any, 'writeToFile');
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
// afterAll(async () => {
|
|
21
|
-
// await fs.rm(path.join(process.cwd(), 'Logs'), { recursive: true });
|
|
22
|
-
// });
|
|
23
|
-
|
|
24
|
-
it('can write a debug log to file', async () => {
|
|
25
|
-
await Logger.get().debug('hello world');
|
|
26
|
-
expect(spy.writeToFile).toHaveBeenCalledTimes(1);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('can write an info log to file', async () => {
|
|
30
|
-
await Logger.get().info('hello world');
|
|
31
|
-
expect(spy.writeToFile).toHaveBeenCalledTimes(1);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('can write a warn log to file', async () => {
|
|
35
|
-
await Logger.get().warn('hello world');
|
|
36
|
-
expect(spy.writeToFile).toHaveBeenCalledTimes(1);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('can write an error log to file', async () => {
|
|
40
|
-
await Logger.get().error('hello world');
|
|
41
|
-
expect(spy.writeToFile).toHaveBeenCalledTimes(1);
|
|
42
|
-
});
|
|
43
|
-
});
|
package/dist/src/Constants.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
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
|
-
}
|
package/dist/src/Constants.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
package/dist/src/Logger.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { ILogger } from './Types';
|
|
2
|
-
export declare class Logger {
|
|
3
|
-
private readonly logDir;
|
|
4
|
-
private readonly logToConsole;
|
|
5
|
-
private readonly logToFile;
|
|
6
|
-
private readonly level;
|
|
7
|
-
private static logger;
|
|
8
|
-
private readonly logQueue;
|
|
9
|
-
constructor(options?: ILogger);
|
|
10
|
-
static get(): Logger;
|
|
11
|
-
debug(message: string): Promise<void>;
|
|
12
|
-
info(message: string): Promise<void>;
|
|
13
|
-
warn(message: string): Promise<void>;
|
|
14
|
-
error(message: string): Promise<void>;
|
|
15
|
-
}
|
package/dist/src/Logger.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Logger = void 0;
|
|
4
|
-
const Constants_1 = require("./Constants");
|
|
5
|
-
const Queue_1 = require("./Queue");
|
|
6
|
-
class Logger {
|
|
7
|
-
logDir;
|
|
8
|
-
logToConsole;
|
|
9
|
-
logToFile;
|
|
10
|
-
level;
|
|
11
|
-
static logger = null;
|
|
12
|
-
logQueue;
|
|
13
|
-
constructor(options) {
|
|
14
|
-
console.log('Initiating a logger...');
|
|
15
|
-
if (Logger.logger) {
|
|
16
|
-
console.warn('The logger was already initiated.');
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
this.logToConsole = options?.logToConsole ?? true;
|
|
20
|
-
this.logToFile = options?.logToFile ?? false;
|
|
21
|
-
this.level = options?.level ?? 'info';
|
|
22
|
-
Logger.logger = this;
|
|
23
|
-
if (this.logToFile) {
|
|
24
|
-
this.logQueue = new Queue_1.Queue(options);
|
|
25
|
-
}
|
|
26
|
-
console.log('Logger initiated.');
|
|
27
|
-
}
|
|
28
|
-
static get() {
|
|
29
|
-
return Logger.logger;
|
|
30
|
-
}
|
|
31
|
-
async debug(message) {
|
|
32
|
-
if (this.logToFile) {
|
|
33
|
-
this.logQueue.add(message, 'debug');
|
|
34
|
-
}
|
|
35
|
-
if (this.logToConsole && this.level === 'debug') {
|
|
36
|
-
console.debug(`${Constants_1.Constants.TEXT_CYAN}[DEBUG]:${Constants_1.Constants.RESET} ${message}`);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
async info(message) {
|
|
40
|
-
if (this.logToFile) {
|
|
41
|
-
this.logQueue.add(message, 'info');
|
|
42
|
-
}
|
|
43
|
-
if (this.logToConsole) {
|
|
44
|
-
console.log(`${Constants_1.Constants.TEXT_BLUE}[INFO]:${Constants_1.Constants.RESET} ${message}`);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
async warn(message) {
|
|
48
|
-
if (this.logToFile) {
|
|
49
|
-
this.logQueue.add(message, 'warn');
|
|
50
|
-
}
|
|
51
|
-
if (this.logToConsole) {
|
|
52
|
-
console.warn(`${Constants_1.Constants.TEXT_YELLOW}[WARN]:${Constants_1.Constants.RESET} ${message}`);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
async error(message) {
|
|
56
|
-
if (this.logToFile) {
|
|
57
|
-
this.logQueue.add(message, 'error');
|
|
58
|
-
}
|
|
59
|
-
if (this.logToConsole) {
|
|
60
|
-
console.error(`${Constants_1.Constants.TEXT_RED}[ERROR]:${Constants_1.Constants.RESET} ${message}`);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
exports.Logger = Logger;
|
|
65
|
-
//# sourceMappingURL=Logger.js.map
|
package/dist/src/Logger.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Logger.js","sourceRoot":"./","sources":["src/Logger.ts"],"names":[],"mappings":";;;AACA,2CAAwC;AAExC,mCAAgC;AAEhC,MAAa,MAAM;IACA,MAAM,CAAS;IACf,YAAY,CAAU;IACtB,SAAS,CAAU;IACnB,KAAK,CAAW;IACzB,MAAM,CAAC,MAAM,GAAW,IAAI,CAAC;IAEpB,QAAQ,CAAQ;IAEjC,YAAY,OAAiB;QAC3B,OAAO,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAC;QACtC,IAAI,MAAM,CAAC,MAAM,EAAE;YACjB,OAAO,CAAC,IAAI,CAAC,mCAAmC,CAAC,CAAC;YAClD,OAAO;SACR;QAED,IAAI,CAAC,YAAY,GAAG,OAAO,EAAE,YAAY,IAAI,IAAI,C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pC;QACD,IAAI,IAAI,CAAC,YAAY,EAAE;YACrB,OAAO,CAAC,IAAI,CACV,GAAG,qBAAS,CAAC,WAAW,UAAU,qBAAS,CAAC,KAAK,IAAI,OAAO,EAAE,CAC/D,CAAC;SACH;IACH,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,OAAe;QACzB,IAAI,IAAI,CAAC,SAAS,EAAE;YAClB,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;SACrC;QACD,IAAI,IAAI,CAAC,YAAY,EAAE;YACrB,OAAO,CAAC,KAAK,CACX,GAAG,qBAAS,CAAC,QAAQ,WAAW,qBAAS,CAAC,KAAK,IAAI,OAAO,EAAE,CAC7D,CAAC;SACH;IACH,CAAC;;AAzEH,wBA0EC"}
|
package/dist/src/Queue.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { ILogger, LogLevel } from './Types';
|
|
2
|
-
export declare class Queue {
|
|
3
|
-
private logQueue;
|
|
4
|
-
private isRunning;
|
|
5
|
-
private readonly logDir;
|
|
6
|
-
constructor(options: ILogger);
|
|
7
|
-
private checkLogDir;
|
|
8
|
-
add(message: string, level: LogLevel): void;
|
|
9
|
-
runQueue(): Promise<void>;
|
|
10
|
-
private writeToFile;
|
|
11
|
-
}
|
package/dist/src/Queue.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Queue = void 0;
|
|
4
|
-
const fs = require("fs/promises");
|
|
5
|
-
const path = require("path");
|
|
6
|
-
const Logger_1 = require("./Logger");
|
|
7
|
-
class Queue {
|
|
8
|
-
logQueue;
|
|
9
|
-
isRunning;
|
|
10
|
-
logDir;
|
|
11
|
-
constructor(options) {
|
|
12
|
-
this.logQueue = [];
|
|
13
|
-
this.isRunning = false;
|
|
14
|
-
this.logDir = options?.directory ?? 'Logs';
|
|
15
|
-
this.checkLogDir();
|
|
16
|
-
}
|
|
17
|
-
async checkLogDir() {
|
|
18
|
-
try {
|
|
19
|
-
await fs.mkdir(this.logDir);
|
|
20
|
-
await Logger_1.Logger.get().debug(`Logs folder generated. Logging to: ${this.logDir}`);
|
|
21
|
-
}
|
|
22
|
-
catch (error) {
|
|
23
|
-
await Logger_1.Logger.get().debug(`Logs folder already exists. Logging to: ${this.logDir}`);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
add(message, level) {
|
|
27
|
-
this.logQueue.push({ message, level });
|
|
28
|
-
if (!this.isRunning) {
|
|
29
|
-
this.isRunning = true;
|
|
30
|
-
this.runQueue();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
async runQueue() {
|
|
34
|
-
const { message, level } = this.logQueue.shift();
|
|
35
|
-
await this.writeToFile(message, level);
|
|
36
|
-
if (this.logQueue.length) {
|
|
37
|
-
return this.runQueue();
|
|
38
|
-
}
|
|
39
|
-
this.isRunning = false;
|
|
40
|
-
}
|
|
41
|
-
async writeToFile(message, level) {
|
|
42
|
-
const DAY = new Date().getDate();
|
|
43
|
-
const MONTH = new Date().getMonth() + 1;
|
|
44
|
-
const YEAR = new Date().getFullYear();
|
|
45
|
-
const HOURS = new Date().getHours();
|
|
46
|
-
const MINUTES = new Date().getMinutes();
|
|
47
|
-
const FULL_DATE = [DAY, MONTH, YEAR].join('-');
|
|
48
|
-
const FULL_TIME = [HOURS, MINUTES].join(':');
|
|
49
|
-
let logFile;
|
|
50
|
-
try {
|
|
51
|
-
logFile = await fs.readFile(path.join(this.logDir, `${FULL_DATE}-${level}.log`));
|
|
52
|
-
}
|
|
53
|
-
catch (e) {
|
|
54
|
-
const data = 'Log Time: ' + FULL_TIME + '\n' + message + '\n\n';
|
|
55
|
-
await fs.writeFile(path.join(this.logDir, `${FULL_DATE}-${level}.log`), data);
|
|
56
|
-
}
|
|
57
|
-
if (logFile) {
|
|
58
|
-
const data = logFile + 'Log Time: ' + FULL_TIME + '\n' + message + '\n\n';
|
|
59
|
-
await fs.writeFile(path.join(this.logDir, `${FULL_DATE}-${level}.log`), data);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
exports.Queue = Queue;
|
|
64
|
-
//# sourceMappingURL=Queue.js.map
|
package/dist/src/Queue.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Queue.js","sourceRoot":"./","sources":["src/Queue.ts"],"names":[],"mappings":";;;AAAA,kCAAkC;AAClC,6BAA6B;AAE7B,qCAAgC;AAEhC,MAAa,KAAK;IACR,QAAQ,CAAc;IACtB,SAAS,CAAU;IACV,MAAM,CAAS;IAEhC,YAAY,OAAgB;QAC1B,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;QACnB,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;QACvB,IAAI,CAAC,MAAM,GAAG,OAAO,EAAE,SAAS,IAAI,MAAM,CAAC;QAC3C,IAAI,CAAC,WAAW,EAAE,CAAC;IACrB,CAAC;IAGO,KAAK,CAAC,WAAW;QACvB,IAAI;YACF,MAAM,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAC5B,MAAM,eAAM,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,sCAAsC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;SAC/E;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,eAAM,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,2CAA2C,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;SACpF;IACH,CAAC;IAGD,GAAG,CAAC,OAAe,EAAE,KAAe;QAClC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;QACvC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;YACtB,IAAI,CAAC,QAAQ,EAAE,CAAC;SACjB;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E,MAAM,EAAE,CAAC,SAAS,CAChB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,SAAS,IAAI,KAAK,MAAM,CAAC,EACnD,IAAI,CACL,CAAC;SACH;QAED,IAAI,OAAO,EAAE;YACX,MAAM,IAAI,GAAG,OAAO,GAAG,YAAY,GAAG,SAAS,GAAG,IAAI,GAAG,OAAO,GAAG,MAAM,CAAC;YAC1E,MAAM,EAAE,CAAC,SAAS,CAChB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,SAAS,IAAI,KAAK,MAAM,CAAC,EACnD,IAAI,CACL,CAAC;SACH;IACH,CAAC;CACF;AAxED,sBAwEC"}
|
package/dist/src/Types.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export type Primitives = string | number | object;
|
|
2
|
-
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
3
|
-
export type LogObject = {
|
|
4
|
-
message: string;
|
|
5
|
-
level: LogLevel;
|
|
6
|
-
};
|
|
7
|
-
export interface ILogger {
|
|
8
|
-
directory?: string;
|
|
9
|
-
logToConsole?: boolean;
|
|
10
|
-
logToFile?: boolean;
|
|
11
|
-
level?: LogLevel;
|
|
12
|
-
}
|
package/dist/src/Types.js
DELETED
package/dist/src/Types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Types.js","sourceRoot":"./","sources":["src/Types.ts"],"names":[],"mappings":""}
|
package/src/Constants.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs/promises';
|
|
2
|
-
import { Constants } from './Constants';
|
|
3
|
-
import { ILogger, LogLevel } from './Types';
|
|
4
|
-
import { Queue } from './Queue';
|
|
5
|
-
|
|
6
|
-
export class Logger {
|
|
7
|
-
private readonly logDir: string;
|
|
8
|
-
private readonly logToConsole: boolean;
|
|
9
|
-
private readonly logToFile: boolean;
|
|
10
|
-
private readonly level: LogLevel;
|
|
11
|
-
private static logger: Logger = null;
|
|
12
|
-
|
|
13
|
-
private readonly logQueue: Queue;
|
|
14
|
-
|
|
15
|
-
constructor(options?: ILogger) {
|
|
16
|
-
console.log('Initiating a logger...');
|
|
17
|
-
if (Logger.logger) {
|
|
18
|
-
console.warn('The logger was already initiated.');
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
this.logToConsole = options?.logToConsole ?? true;
|
|
23
|
-
this.logToFile = options?.logToFile ?? false;
|
|
24
|
-
this.level = options?.level ?? 'info';
|
|
25
|
-
|
|
26
|
-
Logger.logger = this;
|
|
27
|
-
|
|
28
|
-
if (this.logToFile) {
|
|
29
|
-
this.logQueue = new Queue(options);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
console.log('Logger initiated.');
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
static get(): Logger {
|
|
36
|
-
return Logger.logger;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
async debug(message: string) {
|
|
40
|
-
if (this.logToFile) {
|
|
41
|
-
this.logQueue.add(message, 'debug');
|
|
42
|
-
}
|
|
43
|
-
if (this.logToConsole && this.level === 'debug') {
|
|
44
|
-
console.debug(
|
|
45
|
-
`${Constants.TEXT_CYAN}[DEBUG]:${Constants.RESET} ${message}`,
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async info(message: string) {
|
|
51
|
-
if (this.logToFile) {
|
|
52
|
-
this.logQueue.add(message, 'info');
|
|
53
|
-
}
|
|
54
|
-
if (this.logToConsole) {
|
|
55
|
-
console.log(`${Constants.TEXT_BLUE}[INFO]:${Constants.RESET} ${message}`);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async warn(message: string) {
|
|
60
|
-
if (this.logToFile) {
|
|
61
|
-
this.logQueue.add(message, 'warn');
|
|
62
|
-
}
|
|
63
|
-
if (this.logToConsole) {
|
|
64
|
-
console.warn(
|
|
65
|
-
`${Constants.TEXT_YELLOW}[WARN]:${Constants.RESET} ${message}`,
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async error(message: string) {
|
|
71
|
-
if (this.logToFile) {
|
|
72
|
-
this.logQueue.add(message, 'error');
|
|
73
|
-
}
|
|
74
|
-
if (this.logToConsole) {
|
|
75
|
-
console.error(
|
|
76
|
-
`${Constants.TEXT_RED}[ERROR]:${Constants.RESET} ${message}`,
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
package/src/Queue.ts
DELETED
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import * as fs from 'fs/promises';
|
|
2
|
-
import * as path from 'path';
|
|
3
|
-
import { ILogger, LogLevel, LogObject } from './Types';
|
|
4
|
-
import {Logger} from "./Logger";
|
|
5
|
-
|
|
6
|
-
export class Queue {
|
|
7
|
-
private logQueue: LogObject[];
|
|
8
|
-
private isRunning: boolean;
|
|
9
|
-
private readonly logDir: string;
|
|
10
|
-
|
|
11
|
-
constructor(options: ILogger) {
|
|
12
|
-
this.logQueue = [];
|
|
13
|
-
this.isRunning = false;
|
|
14
|
-
this.logDir = options?.directory ?? 'Logs';
|
|
15
|
-
this.checkLogDir();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// TODO: update this maybe
|
|
19
|
-
private async checkLogDir() {
|
|
20
|
-
try {
|
|
21
|
-
await fs.mkdir(this.logDir);
|
|
22
|
-
await Logger.get().debug(`Logs folder generated. Logging to: ${this.logDir}`);
|
|
23
|
-
} catch (error) {
|
|
24
|
-
await Logger.get().debug(`Logs folder already exists. Logging to: ${this.logDir}`);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
add(message: string, level: LogLevel) {
|
|
30
|
-
this.logQueue.push({ message, level });
|
|
31
|
-
if (!this.isRunning) {
|
|
32
|
-
this.isRunning = true;
|
|
33
|
-
this.runQueue();
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
async runQueue(): Promise<void> {
|
|
38
|
-
const { message, level } = this.logQueue.shift();
|
|
39
|
-
await this.writeToFile(message, level);
|
|
40
|
-
if (this.logQueue.length) {
|
|
41
|
-
return this.runQueue();
|
|
42
|
-
}
|
|
43
|
-
this.isRunning = false;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// TODO: rewrite better...
|
|
47
|
-
private async writeToFile(message: string, level: LogLevel) {
|
|
48
|
-
const DAY: number = new Date().getDate();
|
|
49
|
-
const MONTH: number = new Date().getMonth() + 1; // +1 because it goes 0-11
|
|
50
|
-
const YEAR: number = new Date().getFullYear();
|
|
51
|
-
const HOURS: number = new Date().getHours();
|
|
52
|
-
const MINUTES: number = new Date().getMinutes();
|
|
53
|
-
|
|
54
|
-
const FULL_DATE: string = [DAY, MONTH, YEAR].join('-');
|
|
55
|
-
const FULL_TIME: string = [HOURS, MINUTES].join(':');
|
|
56
|
-
|
|
57
|
-
let logFile: string | Buffer;
|
|
58
|
-
try {
|
|
59
|
-
logFile = await fs.readFile(
|
|
60
|
-
path.join(this.logDir, `${FULL_DATE}-${level}.log`),
|
|
61
|
-
);
|
|
62
|
-
} catch (e) {
|
|
63
|
-
const data = 'Log Time: ' + FULL_TIME + '\n' + message + '\n\n';
|
|
64
|
-
await fs.writeFile(
|
|
65
|
-
path.join(this.logDir, `${FULL_DATE}-${level}.log`),
|
|
66
|
-
data,
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (logFile) {
|
|
71
|
-
const data = logFile + 'Log Time: ' + FULL_TIME + '\n' + message + '\n\n';
|
|
72
|
-
await fs.writeFile(
|
|
73
|
-
path.join(this.logDir, `${FULL_DATE}-${level}.log`),
|
|
74
|
-
data,
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
package/src/Types.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export type Primitives = string | number | object;
|
|
2
|
-
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
3
|
-
|
|
4
|
-
export type LogObject = {
|
|
5
|
-
message: string;
|
|
6
|
-
level: LogLevel;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export interface ILogger {
|
|
10
|
-
directory?: string;
|
|
11
|
-
logToConsole?: boolean;
|
|
12
|
-
logToFile?: boolean;
|
|
13
|
-
level?: LogLevel;
|
|
14
|
-
}
|