UNPKG

4.69 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var PlatformTools_1 = require("../platform/PlatformTools");
4/**
5 * Performs logging of the events in TypeORM.
6 * This version of logger logs everything into ormlogs.log file.
7 */
8var FileLogger = /** @class */ (function () {
9 // -------------------------------------------------------------------------
10 // Constructor
11 // -------------------------------------------------------------------------
12 function FileLogger(options) {
13 this.options = options;
14 }
15 // -------------------------------------------------------------------------
16 // Public Methods
17 // -------------------------------------------------------------------------
18 /**
19 * Logs query and parameters used in it.
20 */
21 FileLogger.prototype.logQuery = function (query, parameters, queryRunner) {
22 if (this.options === "all" || this.options === true || (this.options instanceof Array && this.options.indexOf("query") !== -1)) {
23 var sql = query + (parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : "");
24 this.write("[QUERY]: " + sql);
25 }
26 };
27 /**
28 * Logs query that is failed.
29 */
30 FileLogger.prototype.logQueryError = function (error, query, parameters, queryRunner) {
31 if (this.options === "all" || this.options === true || (this.options instanceof Array && this.options.indexOf("error") !== -1)) {
32 var sql = query + (parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : "");
33 this.write([
34 "[FAILED QUERY]: " + sql,
35 "[QUERY ERROR]: " + error
36 ]);
37 }
38 };
39 /**
40 * Logs query that is slow.
41 */
42 FileLogger.prototype.logQuerySlow = function (time, query, parameters, queryRunner) {
43 var sql = query + (parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : "");
44 this.write("[SLOW QUERY: " + time + " ms]: " + sql);
45 };
46 /**
47 * Logs events from the schema build process.
48 */
49 FileLogger.prototype.logSchemaBuild = function (message, queryRunner) {
50 if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("schema") !== -1)) {
51 this.write(message);
52 }
53 };
54 /**
55 * Logs events from the migrations run process.
56 */
57 FileLogger.prototype.logMigration = function (message, queryRunner) {
58 this.write(message);
59 };
60 /**
61 * Perform logging using given logger, or by default to the console.
62 * Log has its own level and message.
63 */
64 FileLogger.prototype.log = function (level, message, queryRunner) {
65 switch (level) {
66 case "log":
67 if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("log") !== -1))
68 this.write("[LOG]: " + message);
69 break;
70 case "info":
71 if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("info") !== -1))
72 this.write("[INFO]: " + message);
73 break;
74 case "warn":
75 if (this.options === "all" || (this.options instanceof Array && this.options.indexOf("warn") !== -1))
76 this.write("[WARN]: " + message);
77 break;
78 }
79 };
80 // -------------------------------------------------------------------------
81 // Protected Methods
82 // -------------------------------------------------------------------------
83 /**
84 * Writes given strings into the log file.
85 */
86 FileLogger.prototype.write = function (strings) {
87 strings = strings instanceof Array ? strings : [strings];
88 var basePath = PlatformTools_1.PlatformTools.load("app-root-path").path;
89 strings = strings.map(function (str) { return "[" + new Date().toISOString() + "]" + str; });
90 PlatformTools_1.PlatformTools.appendFileSync(basePath + "/ormlogs.log", strings.join("\r\n") + "\r\n"); // todo: use async or implement promises?
91 };
92 /**
93 * Converts parameters to a string.
94 * Sometimes parameters can have circular objects and therefor we are handle this case too.
95 */
96 FileLogger.prototype.stringifyParams = function (parameters) {
97 try {
98 return JSON.stringify(parameters);
99 }
100 catch (error) { // most probably circular objects in parameters
101 return parameters;
102 }
103 };
104 return FileLogger;
105}());
106exports.FileLogger = FileLogger;
107
108//# sourceMappingURL=FileLogger.js.map