-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.ts
91 lines (65 loc) · 2.79 KB
/
log.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
import * as fs from 'fs';
import * as path from 'path';
import * as events from 'events';
export class Logger extends events.EventEmitter{
fileOutput: any;
public dirChaine = '';
public getClassName() {
var funcNameRegex = /function (.{1,})\(/;
var results = (funcNameRegex).exec((<any> this).constructor.toString());
return (results && results.length > 1) ? results[1] : "";
}
public getLogFileName(){
return `${this.getClassName()}`;
}
public getLogTitle(){
return `${this.getClassName()} : ${new Date().toString()} : `
}
private getFileLogPath(){
let date = new Date();
let dateStr = `${date.getFullYear()}_${this.zeroPad((date.getMonth()+1).toString(), 2)}_${this.zeroPad(date.getDate().toString(), 2)}`
let dateYearMonth = `${date.getFullYear()}_${this.zeroPad((date.getMonth()+1).toString(), 2)}`;
let directory = path.join(__dirname, 'logs', dateYearMonth, dateStr, this.dirChaine);
if(!fs.existsSync(directory)){
let mkdirp = require('mkdirp');
mkdirp.sync(directory);
}
return path.join(directory, `${this.getLogFileName()}.log`)
}
public log(...messages){
if(this.fileOutput == null){
let fileLogPath = this.getFileLogPath();
this.fileOutput = fs.createWriteStream(fileLogPath);
}
let fileConsole = new (require('console').Console)(this.fileOutput);
fileConsole.log(this.getLogTitle(), ...messages);
console.log(this.getLogTitle(), ...messages);
}
public error(... messages){
if(this.fileOutput == null){
let fileLogPath = this.getFileLogPath();
this.fileOutput = fs.createWriteStream(fileLogPath);
}
let fileConsole = new (require('console').Console)(this.fileOutput);
messages.unshift("[ERROR]");
fileConsole.error(this.getLogTitle(), ...messages);
console.error(this.getLogTitle(), ...messages);
}
public warm(... messages){
if(this.fileOutput == null){
let fileLogPath = this.getFileLogPath();
this.fileOutput = fs.createWriteStream(fileLogPath);
}
let fileConsole = new (require('console').Console)(this.fileOutput);
messages.unshift("[WARNING]");
fileConsole.warn(this.getLogTitle(), ...messages);
console.warn(this.getLogTitle(), ...messages);
}
public zeroPad(str: string, places: number) {
let zero = Math.max(places - str.length, 0);
for(let i =0; i< zero; i++){
str = "0" + str;
}
return str;
}
}