-
Notifications
You must be signed in to change notification settings - Fork 13
/
log.ts
81 lines (65 loc) · 2.89 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
// Copyright 2020-2020 The Mandarine.TS Framework authors. All rights reserved. MIT license.
import { bold, green, magenta, red, yellow } from "https://deno.land/std@0.84.0/fmt/colors.ts";
export interface LogOptions {
logDuringTesting: string | undefined;
}
type MsgType = "debug" | "info" | "warn" | "error";
export class Log {
private className: string | null = null;
public logOptions: LogOptions;
constructor(source: any | string) {
this.logOptions = {
logDuringTesting: Deno.env.get("LOG_DURING_TESTING")
};
if(typeof source === 'string') {
this.className = source === "-" ? "-" : <string> source + ".class";
return;
}
try {
this.className = source.prototype.constructor.name + ".class";
}catch(error) {
this.className = "unknown";
}
}
public debug(msg: string, ...supportingDetails: any[]): void {
if(Deno.env.get("DEBUG")) {
this.emitLogMessage("debug", msg, supportingDetails);
}
}
public info(msg: string, ...supportingDetails: any[]): void {
this.emitLogMessage("info", msg, supportingDetails);
}
public warn(msg: string, ...supportingDetails: any[]): void {
this.emitLogMessage("warn", msg, supportingDetails);
}
public error(msg: string, ...supportingDetails: any[]): void {
this.emitLogMessage("error", msg, supportingDetails);
}
public compiler(msg: string, msgType: MsgType, ...supportingDetails: any[]): void {
if(this.logOptions && this.logOptions.logDuringTesting === "false") return;
if(supportingDetails.length > 0) this[msgType](msg, supportingDetails);
else this[msgType](msg);
}
private emitLogMessage(msgType: MsgType, msg: string, supportingDetails: any[]) {
let finalMessage: string | null = null;
switch(msgType) {
case "debug":
finalMessage = `${magenta(bold(`[${msgType.toUpperCase()} | ${new Date().toLocaleString()}]`))} [${Deno.pid}] [${this.className}] ${msg}`;
break;
case "info":
finalMessage = `${green(bold(`[${msgType.toUpperCase()} | ${new Date().toLocaleString()}]`))} [${Deno.pid}] [${this.className}] ${msg}`;
break;
case "warn":
finalMessage = `${yellow(bold(`[${msgType.toUpperCase()} | ${new Date().toLocaleString()}]`))} [${Deno.pid}] [${this.className}] ${msg}`;
break;
case "error":
finalMessage = `${red(bold(`[${msgType.toUpperCase()} | ${new Date().toLocaleString()}]`))} [${Deno.pid}] [${this.className}] ${msg}`;
break;
}
if(supportingDetails.length > 0) console[msgType](finalMessage, supportingDetails);
else console[msgType](finalMessage);
}
public static getLogger(source: any) {
return new Log(source);
}
}