UNPKG

7.34 kBJavaScriptView Raw
1"use strict";
2var __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})();
15var __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};
26Object.defineProperty(exports, "__esModule", { value: true });
27exports.enable = exports.winston2 = exports.winston3 = void 0;
28// Copyright (c) Microsoft Corporation. All rights reserved.
29// Licensed under the MIT license. See LICENSE file in the project root for details.
30var diagnostic_channel_1 = require("diagnostic-channel");
31// register a "filter" with each logger that publishes the data about to be logged
32var 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 // whenever someone logs, ensure our filter comes last
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};
63var 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 = /** @class */ (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 // tslint:disable-next-line:prefer-const - try to obtain level from Symbol(level) afterwards
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; // Symbol(level) is uncolorized, so prefer getting it from here
88 message = info instanceof Error ? info : message; // Winston places Errors at info, strings at info.message
89 var levelKind = mapLevelToKind(this.winston, level);
90 meta = meta || {}; // Winston _somtimes_ puts metadata inside meta, so start from here
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 // Patch this function
102 function patchedConfigure() {
103 // Grab highest sev logging level in case of custom logging levels
104 var levels = arguments[0].levels || originalWinston.config.npm.levels;
105 var lastLevel;
106 for (var level in levels) {
107 if (levels.hasOwnProperty(level)) {
108 lastLevel = lastLevel === undefined || levels[level] > levels[lastLevel] ? level : lastLevel;
109 }
110 }
111 this.add(new AppInsightsTransport(originalWinston, { level: lastLevel }));
112 }
113 var origCreate = originalWinston.createLogger;
114 originalWinston.createLogger = function patchedCreate() {
115 // Grab highest sev logging level in case of custom logging levels
116 var levels = arguments[0].levels || originalWinston.config.npm.levels;
117 var lastLevel;
118 for (var level in levels) {
119 if (levels.hasOwnProperty(level)) {
120 lastLevel = lastLevel === undefined || levels[level] > levels[lastLevel] ? level : lastLevel;
121 }
122 }
123 // Add custom app insights transport to the end
124 // Remark: Configure is not available until after createLogger()
125 // and the Logger prototype is not exported in winston 3.x, so
126 // patch both createLogger and configure. Could also call configure
127 // again after createLogger, but that would cause configure to be called
128 // twice per create.
129 var result = origCreate.apply(this, arguments);
130 result.add(new AppInsightsTransport(originalWinston, { level: lastLevel }));
131 var origConfigure = result.configure;
132 result.configure = function () {
133 origConfigure.apply(this, arguments);
134 patchedConfigure.apply(this, arguments);
135 };
136 return result;
137 };
138 var origRootConfigure = originalWinston.configure;
139 originalWinston.configure = function () {
140 origRootConfigure.apply(this, arguments);
141 patchedConfigure.apply(this, arguments);
142 };
143 originalWinston.add(new AppInsightsTransport(originalWinston));
144 return originalWinston;
145};
146exports.winston3 = {
147 versionSpecifier: "3.x",
148 patch: winston3PatchFunction,
149};
150exports.winston2 = {
151 versionSpecifier: "2.x",
152 patch: winston2PatchFunction,
153};
154function enable() {
155 diagnostic_channel_1.channel.registerMonkeyPatch("winston", exports.winston2);
156 diagnostic_channel_1.channel.registerMonkeyPatch("winston", exports.winston3);
157}
158exports.enable = enable;
159//# sourceMappingURL=winston.pub.js.map
\No newline at end of file