1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var app_root_path_1 = tslib_1.__importDefault(require("app-root-path"));
|
5 | var PlatformTools_1 = require("../platform/PlatformTools");
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var FileLogger = (function () {
|
11 |
|
12 |
|
13 |
|
14 | function FileLogger(options, fileLoggerOptions) {
|
15 | this.options = options;
|
16 | this.fileLoggerOptions = fileLoggerOptions;
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 | |
22 |
|
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 |
|
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 |
|
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 |
|
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 |
|
59 |
|
60 | FileLogger.prototype.logMigration = function (message, queryRunner) {
|
61 | this.write(message);
|
62 | };
|
63 | |
64 |
|
65 |
|
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 |
|
85 |
|
86 | |
87 |
|
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");
|
98 | };
|
99 | |
100 |
|
101 |
|
102 |
|
103 | FileLogger.prototype.stringifyParams = function (parameters) {
|
104 | try {
|
105 | return JSON.stringify(parameters);
|
106 | }
|
107 | catch (error) {
|
108 | return parameters;
|
109 | }
|
110 | };
|
111 | return FileLogger;
|
112 | }());
|
113 | exports.FileLogger = FileLogger;
|
114 |
|
115 |
|