1 | import * as moment from "moment";
|
2 | import * as colors from "colors/safe";
|
3 |
|
4 | export enum Level {
|
5 | error = 0,
|
6 | warn = 1,
|
7 | info = 2,
|
8 | verbose = 3,
|
9 | debug = 4,
|
10 | silly = 5
|
11 | }
|
12 |
|
13 | export default class Logger {
|
14 | private static self: Logger;
|
15 |
|
16 | public static get shared() {
|
17 | if (!Logger.self) {
|
18 | Logger.self = new Logger();
|
19 | }
|
20 | return Logger.self;
|
21 | }
|
22 |
|
23 | private constructor() {}
|
24 |
|
25 | private level: Level = Level.debug;
|
26 |
|
27 | private get head(): string {
|
28 | let now = moment();
|
29 | let d = now.format("YYYY-MM-DD HH:mm:ss");
|
30 | return "[" + d + "] ";
|
31 | }
|
32 |
|
33 | public log(msg: any, ...optionalParameters: any[]) {
|
34 | this.info(msg, optionalParameters);
|
35 | }
|
36 |
|
37 | public error(msg: any, ...optionalParameters: any[]) {
|
38 | if (this.level < Level.error) {
|
39 | return;
|
40 | }
|
41 | if (optionalParameters.length > 0) {
|
42 | console.error(colors.red(this.head + msg), optionalParameters);
|
43 | } else {
|
44 | console.error(colors.red(this.head + msg));
|
45 | }
|
46 | }
|
47 |
|
48 | public warn(msg: any, ...optionalParameters: any[]) {
|
49 | if (this.level < Level.warn) {
|
50 | return;
|
51 | }
|
52 | if (optionalParameters.length > 0) {
|
53 | console.warn(colors.yellow(this.head + msg), optionalParameters);
|
54 | } else {
|
55 | console.warn(colors.yellow(this.head + msg));
|
56 | }
|
57 | }
|
58 |
|
59 | public info(msg: any, ...optionalParameters: any[]) {
|
60 | if (this.level < Level.info) {
|
61 | return;
|
62 | }
|
63 | if (optionalParameters.length > 0) {
|
64 | console.info(this.head + msg, optionalParameters);
|
65 | } else {
|
66 | console.info(this.head + msg);
|
67 | }
|
68 | }
|
69 |
|
70 | public verbose(msg: any, ...optionalParameters: any[]) {
|
71 | if (this.level < Level.verbose) {
|
72 | return;
|
73 | }
|
74 | if (optionalParameters.length > 0) {
|
75 | console.log(this.head + msg, optionalParameters);
|
76 | } else {
|
77 | console.log(this.head + msg);
|
78 | }
|
79 | }
|
80 |
|
81 | public debug(msg: any, ...optionalParameters: any[]) {
|
82 | if (this.level < Level.debug) {
|
83 | return;
|
84 | }
|
85 | if (optionalParameters.length > 0) {
|
86 | console.log(this.head + msg, optionalParameters);
|
87 | } else {
|
88 | console.log(this.head + msg);
|
89 | }
|
90 | }
|
91 |
|
92 | public silly(msg: any, ...optionalParameters: any[]) {
|
93 | if (this.level < Level.silly) {
|
94 | return;
|
95 | }
|
96 | if (optionalParameters.length > 0) {
|
97 | console.log(this.head + msg, optionalParameters);
|
98 | } else {
|
99 | console.log(this.head + msg);
|
100 | }
|
101 | }
|
102 | }
|
103 |
|
104 | let logger = Logger.shared;
|
105 |
|
106 | export { logger };
|