UNPKG

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