1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | };
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | var __rest = (this && this.__rest) || function (s, e) {
|
16 | var t = {};
|
17 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
18 | t[p] = s[p];
|
19 | if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
20 | for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
21 | if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
22 | t[p[i]] = s[p[i]];
|
23 | }
|
24 | return t;
|
25 | };
|
26 | Object.defineProperty(exports, "__esModule", { value: true });
|
27 | exports.enable = exports.winston2 = exports.winston3 = void 0;
|
28 |
|
29 |
|
30 | var diagnostic_channel_1 = require("diagnostic-channel");
|
31 |
|
32 | var winston2PatchFunction = function (originalWinston) {
|
33 | var originalLog = originalWinston.Logger.prototype.log;
|
34 | var curLevels;
|
35 | var loggingFilter = function (level, message, meta) {
|
36 | var levelKind;
|
37 | if (curLevels === originalWinston.config.npm.levels) {
|
38 | levelKind = "npm";
|
39 | }
|
40 | else if (curLevels === originalWinston.config.syslog.levels) {
|
41 | levelKind = "syslog";
|
42 | }
|
43 | else {
|
44 | levelKind = "unknown";
|
45 | }
|
46 | diagnostic_channel_1.channel.publish("winston", { level: level, message: message, meta: meta, levelKind: levelKind });
|
47 | return message;
|
48 | };
|
49 |
|
50 | originalWinston.Logger.prototype.log = function log() {
|
51 | curLevels = this.levels;
|
52 | if (!this.filters || this.filters.length === 0) {
|
53 | this.filters = [loggingFilter];
|
54 | }
|
55 | else if (this.filters[this.filters.length - 1] !== loggingFilter) {
|
56 | this.filters = this.filters.filter(function (f) { return f !== loggingFilter; });
|
57 | this.filters.push(loggingFilter);
|
58 | }
|
59 | return originalLog.apply(this, arguments);
|
60 | };
|
61 | return originalWinston;
|
62 | };
|
63 | var winston3PatchFunction = function (originalWinston) {
|
64 | var mapLevelToKind = function (winston, level) {
|
65 | var levelKind;
|
66 | if (winston.config.npm.levels[level] != null) {
|
67 | levelKind = "npm";
|
68 | }
|
69 | else if (winston.config.syslog.levels[level] != null) {
|
70 | levelKind = "syslog";
|
71 | }
|
72 | else {
|
73 | levelKind = "unknown";
|
74 | }
|
75 | return levelKind;
|
76 | };
|
77 | var AppInsightsTransport = (function (_super) {
|
78 | __extends(AppInsightsTransport, _super);
|
79 | function AppInsightsTransport(winston, opts) {
|
80 | var _this = _super.call(this, opts) || this;
|
81 | _this.winston = winston;
|
82 | return _this;
|
83 | }
|
84 | AppInsightsTransport.prototype.log = function (info, callback) {
|
85 |
|
86 | var message = info.message, level = info.level, meta = info.meta, splat = __rest(info, ["message", "level", "meta"]);
|
87 | level = typeof Symbol["for"] === "function" ? info[Symbol["for"]("level")] : level;
|
88 | message = info instanceof Error ? info : message;
|
89 | var levelKind = mapLevelToKind(this.winston, level);
|
90 | meta = meta || {};
|
91 | for (var key in splat) {
|
92 | if (splat.hasOwnProperty(key)) {
|
93 | meta[key] = splat[key];
|
94 | }
|
95 | }
|
96 | diagnostic_channel_1.channel.publish("winston", { message: message, level: level, levelKind: levelKind, meta: meta });
|
97 | callback();
|
98 | };
|
99 | return AppInsightsTransport;
|
100 | }(originalWinston.Transport));
|
101 |
|
102 | function getLogLevel(opts) {
|
103 | var _a;
|
104 |
|
105 | if (opts === null || opts === void 0 ? void 0 : opts.level) {
|
106 | return opts.level;
|
107 | }
|
108 |
|
109 | var levels = (_a = opts === null || opts === void 0 ? void 0 : opts.levels) !== null && _a !== void 0 ? _a : originalWinston.config.npm.levels;
|
110 | var lastLevel;
|
111 | for (var level in levels) {
|
112 | if (levels.hasOwnProperty(level)) {
|
113 | lastLevel = lastLevel === undefined || levels[level] > levels[lastLevel] ? level : lastLevel;
|
114 | }
|
115 | }
|
116 | return lastLevel;
|
117 | }
|
118 |
|
119 | function patchedConfigure(opts) {
|
120 | this.add(new AppInsightsTransport(originalWinston, { level: getLogLevel(opts) }));
|
121 | }
|
122 | var origCreate = originalWinston.createLogger;
|
123 | originalWinston.createLogger = function patchedCreate(opts) {
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | var result = origCreate.call(this, opts);
|
131 | result.add(new AppInsightsTransport(originalWinston, { level: getLogLevel(opts) }));
|
132 | var origConfigure = result.configure;
|
133 | result.configure = function () {
|
134 | origConfigure.apply(this, arguments);
|
135 | patchedConfigure.apply(this, arguments);
|
136 | };
|
137 | return result;
|
138 | };
|
139 | var origRootConfigure = originalWinston.configure;
|
140 | originalWinston.configure = function () {
|
141 | origRootConfigure.apply(this, arguments);
|
142 | patchedConfigure.apply(this, arguments);
|
143 | };
|
144 | originalWinston.add(new AppInsightsTransport(originalWinston));
|
145 | return originalWinston;
|
146 | };
|
147 | exports.winston3 = {
|
148 | versionSpecifier: "3.x",
|
149 | patch: winston3PatchFunction
|
150 | };
|
151 | exports.winston2 = {
|
152 | versionSpecifier: "2.x",
|
153 | patch: winston2PatchFunction
|
154 | };
|
155 | function enable() {
|
156 | diagnostic_channel_1.channel.registerMonkeyPatch("winston", exports.winston2);
|
157 | diagnostic_channel_1.channel.registerMonkeyPatch("winston", exports.winston3);
|
158 | }
|
159 | exports.enable = enable;
|
160 |
|
\ | No newline at end of file |