-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
AdvancedConsoleLogger.ts
107 lines (93 loc) · 4.14 KB
/
AdvancedConsoleLogger.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
import {LoggerOptions} from "./LoggerOptions";
import {PlatformTools} from "../platform/PlatformTools";
import {QueryRunner} from "../query-runner/QueryRunner";
import {Logger} from "./Logger";
/**
* Performs logging of the events in TypeORM.
* This version of logger uses console to log events and use syntax highlighting.
*/
export class AdvancedConsoleLogger implements Logger {
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(private options?: LoggerOptions) {
}
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Logs query and parameters used in it.
*/
logQuery(query: string, parameters?: any[], queryRunner?: QueryRunner) {
if (this.options === "all" || this.options === true || (this.options instanceof Array && this.options.indexOf("query") !== -1)) {
const sql = query + (parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : "");
PlatformTools.logInfo("query:", PlatformTools.highlightSql(sql));
}
}
/**
* Logs query that is failed.
*/
logQueryError(error: string, query: string, parameters?: any[], queryRunner?: QueryRunner) {
if (this.options === "all" || this.options === true || (this.options instanceof Array && this.options.indexOf("error") !== -1)) {
const sql = query + (parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : "");
PlatformTools.logError(`query failed:`, PlatformTools.highlightSql(sql));
PlatformTools.logError(`error:`, error);
}
}
/**
* Logs query that is slow.
*/
logQuerySlow(time: number, query: string, parameters?: any[], queryRunner?: QueryRunner) {
const sql = query + (parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : "");
PlatformTools.logWarn(`query is slow:`, PlatformTools.highlightSql(sql));
PlatformTools.logWarn(`execution time:`, time);
}
/**
* Logs events from the schema build process.
*/
logSchemaBuild(message: string, queryRunner?: QueryRunner) {
if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("schema") !== -1)) {
PlatformTools.log(message);
}
}
/**
* Logs events from the migration run process.
*/
logMigration(message: string, queryRunner?: QueryRunner) {
PlatformTools.log(message);
}
/**
* Perform logging using given logger, or by default to the console.
* Log has its own level and message.
*/
log(level: "log"|"info"|"warn", message: any, queryRunner?: QueryRunner) {
switch (level) {
case "log":
if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("log") !== -1))
console.log(message);
break;
case "info":
if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("info") !== -1))
console.info(message);
break;
case "warn":
if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("warn") !== -1))
console.warn(PlatformTools.warn(message));
break;
}
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
/**
* Converts parameters to a string.
* Sometimes parameters can have circular objects and therefor we are handle this case too.
*/
protected stringifyParams(parameters: any[]) {
try {
return JSON.stringify(parameters);
} catch (error) { // most probably circular objects in parameters
return parameters;
}
}
}