UNPKG

4.21 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * Performs logging of the events in TypeORM.
5 * This version of logger uses console to log events and does not use syntax highlighting.
6 */
7var SimpleConsoleLogger = /** @class */ (function () {
8 // -------------------------------------------------------------------------
9 // Constructor
10 // -------------------------------------------------------------------------
11 function SimpleConsoleLogger(options) {
12 this.options = options;
13 }
14 // -------------------------------------------------------------------------
15 // Public Methods
16 // -------------------------------------------------------------------------
17 /**
18 * Logs query and parameters used in it.
19 */
20 SimpleConsoleLogger.prototype.logQuery = function (query, parameters, queryRunner) {
21 if (this.options === "all" || this.options === true || (Array.isArray(this.options) && this.options.indexOf("query") !== -1)) {
22 var sql = query + (parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : "");
23 console.log("query" + ": " + sql);
24 }
25 };
26 /**
27 * Logs query that is failed.
28 */
29 SimpleConsoleLogger.prototype.logQueryError = function (error, query, parameters, queryRunner) {
30 if (this.options === "all" || this.options === true || (Array.isArray(this.options) && this.options.indexOf("error") !== -1)) {
31 var sql = query + (parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : "");
32 console.log("query failed: " + sql);
33 console.log("error:", error);
34 }
35 };
36 /**
37 * Logs query that is slow.
38 */
39 SimpleConsoleLogger.prototype.logQuerySlow = function (time, query, parameters, queryRunner) {
40 var sql = query + (parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : "");
41 console.log("query is slow: " + sql);
42 console.log("execution time: " + time);
43 };
44 /**
45 * Logs events from the schema build process.
46 */
47 SimpleConsoleLogger.prototype.logSchemaBuild = function (message, queryRunner) {
48 if (this.options === "all" || (Array.isArray(this.options) && this.options.indexOf("schema") !== -1)) {
49 console.log(message);
50 }
51 };
52 /**
53 * Logs events from the migrations run process.
54 */
55 SimpleConsoleLogger.prototype.logMigration = function (message, queryRunner) {
56 console.log(message);
57 };
58 /**
59 * Perform logging using given logger, or by default to the console.
60 * Log has its own level and message.
61 */
62 SimpleConsoleLogger.prototype.log = function (level, message, queryRunner) {
63 switch (level) {
64 case "log":
65 if (this.options === "all" || (Array.isArray(this.options) && this.options.indexOf("log") !== -1))
66 console.log(message);
67 break;
68 case "info":
69 if (this.options === "all" || (Array.isArray(this.options) && this.options.indexOf("info") !== -1))
70 console.info(message);
71 break;
72 case "warn":
73 if (this.options === "all" || (Array.isArray(this.options) && this.options.indexOf("warn") !== -1))
74 console.warn(message);
75 break;
76 }
77 };
78 // -------------------------------------------------------------------------
79 // Protected Methods
80 // -------------------------------------------------------------------------
81 /**
82 * Converts parameters to a string.
83 * Sometimes parameters can have circular objects and therefor we are handle this case too.
84 */
85 SimpleConsoleLogger.prototype.stringifyParams = function (parameters) {
86 try {
87 return JSON.stringify(parameters);
88 }
89 catch (error) { // most probably circular objects in parameters
90 return parameters;
91 }
92 };
93 return SimpleConsoleLogger;
94}());
95exports.SimpleConsoleLogger = SimpleConsoleLogger;
96
97//# sourceMappingURL=SimpleConsoleLogger.js.map