-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini.ts
129 lines (120 loc) · 3.45 KB
/
mini.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Copyright © 2022 Dpm Land. All Rights Reserved.
*/
import { colors, ensureDirSync, format, join } from './deps.ts';
/**
* @description The constructor for the dlog module
* @param {string} app_name - The application name for the logger output
* @param {boolean} logToFile - Write a log file DEFAULT: true
* @param {string} logFolder - The folder name to create and log
*/
export default class Dlog {
private _app = '';
private _logToFile: boolean;
private _logFolder: string;
private _date: string;
constructor(
app_name: string,
logToFile: boolean = false,
logFolder: string = 'logs',
) {
this._app = app_name;
this._logToFile = logToFile;
this._logFolder = logFolder;
this._date = format(new Date(), 'yyyy|MM|dd');
}
/**
* @description Log with the info level
* @param {string} message - The message to log
*/
info(message: string): void {
const noColorMsg = `[${
format(new Date(), 'yyyy/MM/dd h:mm:ss a')
}] [${this._app}] [INFO] ${message}`;
console.log(
`${colors.italic(colors.green(`${this._app.toUpperCase()} ->`))} ${
colors.blue('[ INFO ]:')
} ${message}`,
);
if (this._logToFile) {
this.writeToFile(noColorMsg);
}
}
/**
* @description Log with the debug level
* @param {string} message - The message to log
*/
debug(message: string): void {
const noColorMsg = `[${
format(new Date(), 'yyyy/MM/dd h:mm:ss a')
}] [${this._app}] [DEBUG] ${message}`;
console.log(
`${colors.italic(colors.green(`${this._app.toUpperCase()} ->`))} ${
colors.magenta('[ DEBUG ]:')
} ${message}`,
);
if (this._logToFile) {
this.writeToFile(noColorMsg);
}
}
/**
* @description Log with the warn level
* @param {string} message - The message to log
*/
warn(message: string): void {
const noColorMsg = `[${
format(new Date(), 'yyyy/MM/dd h:mm:ss a')
}] [${this._app}] [WARN] ${message}`;
console.log(
`${colors.italic(colors.green(`${this._app.toUpperCase()} ->`))} ${
colors.brightYellow('[ WARN ]:')
} ${message}`,
);
if (this._logToFile) {
this.writeToFile(noColorMsg);
}
}
/**
* @description Log with the error level
* @param {string} message - The message to log
*/
error(message: string): void {
const noColorMsg = `[${
format(new Date(), 'yyyy/MM/dd h:mm:ss a')
}] [${this._app}] [ERROR] ${message}`;
console.log(
`${colors.italic(colors.green(`${this._app.toUpperCase()} ->`))} ${
colors.brightRed('[ ✗ ERROR ]:')
} ${message}`,
);
if (this._logToFile) {
this.writeToFile(noColorMsg);
}
}
/**
* @description Log with the done level
* @param {string} message - The message to log
*/
done(message: string): void {
const noColorMsg = `[${
format(new Date(), 'yyyy/MM/dd h:mm:ss a')
}] [${this._app}] [DONE] ${message}`;
console.log(
`${colors.italic(colors.green(`${this._app.toUpperCase()} ->`))} ${
colors.brightGreen('[ DONE ]:')
} ${message}`,
);
if (this._logToFile) {
this.writeToFile(noColorMsg);
}
}
private writeToFile(noColorMsg: string): void {
ensureDirSync(this._logFolder);
const appName = this._app.replace(' ', '-');
const path = join(this._logFolder, `${appName}.log`);
Deno.writeTextFileSync(path, noColorMsg + '\n', {
append: true,
create: true,
});
}
}