logging-manager.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. const bunyan = require("bunyan");
  2. const request = require("request");
  3. Logger = module.exports = {
  4. initialize(name) {
  5. this.isProduction =
  6. (process.env["NODE_ENV"] || "").toLowerCase() === "production";
  7. this.defaultLevel =
  8. process.env["LOG_LEVEL"] || (this.isProduction ? "warn" : "debug");
  9. this.loggerName = name;
  10. this.ringBuffer = new bunyan.RingBuffer({
  11. limit: process.env["LOG_RING_BUFFER_SIZE"] || 30
  12. });
  13. this.logger = bunyan.createLogger({
  14. name,
  15. serializers: bunyan.stdSerializers,
  16. streams: [
  17. {
  18. level: this.defaultLevel,
  19. stream: process.stdout
  20. },
  21. {
  22. level: "trace",
  23. type: "raw",
  24. stream: this.ringBuffer
  25. }
  26. ]
  27. });
  28. if (this.isProduction) {
  29. // clear interval if already set
  30. if (this.checkInterval) {
  31. clearInterval(this.checkInterval);
  32. }
  33. // check for log level override on startup
  34. this.checkLogLevel();
  35. // re-check log level every minute
  36. const checkLogLevel = () => this.checkLogLevel();
  37. this.checkInterval = setInterval(checkLogLevel, 1000 * 60);
  38. }
  39. return this;
  40. },
  41. checkLogLevel() {
  42. const options = {
  43. headers: {
  44. "Metadata-Flavor": "Google"
  45. },
  46. uri: `http://metadata.google.internal/computeMetadata/v1/project/attributes/${
  47. this.loggerName
  48. }-setLogLevelEndTime`
  49. };
  50. request(options, (err, response, body) => {
  51. if (parseInt(body) > Date.now()) {
  52. this.logger.level("trace");
  53. } else {
  54. this.logger.level(this.defaultLevel);
  55. }
  56. });
  57. },
  58. initializeErrorReporting(sentry_dsn, options) {
  59. const raven = require("raven");
  60. this.raven = new raven.Client(sentry_dsn, options);
  61. this.lastErrorTimeStamp = 0; // for rate limiting on sentry reporting
  62. this.lastErrorCount = 0;
  63. },
  64. captureException(attributes, message, level) {
  65. // handle case of logger.error "message"
  66. let key, value;
  67. if (typeof attributes === "string") {
  68. attributes = { err: new Error(attributes) };
  69. }
  70. // extract any error object
  71. let error = attributes.err || attributes.error;
  72. // avoid reporting errors twice
  73. for (key in attributes) {
  74. value = attributes[key];
  75. if (value instanceof Error && value.reportedToSentry) {
  76. return;
  77. }
  78. }
  79. // include our log message in the error report
  80. if (error == null) {
  81. if (typeof message === "string") {
  82. error = { message };
  83. }
  84. } else if (message != null) {
  85. attributes.description = message;
  86. }
  87. // report the error
  88. if (error != null) {
  89. // capture attributes and use *_id objects as tags
  90. const tags = {};
  91. const extra = {};
  92. for (key in attributes) {
  93. value = attributes[key];
  94. if (key.match(/_id/) && typeof value === "string") {
  95. tags[key] = value;
  96. }
  97. extra[key] = value;
  98. }
  99. // capture req object if available
  100. const { req } = attributes;
  101. if (req != null) {
  102. extra.req = {
  103. method: req.method,
  104. url: req.originalUrl,
  105. query: req.query,
  106. headers: req.headers,
  107. ip: req.ip
  108. };
  109. }
  110. // recreate error objects that have been converted to a normal object
  111. if (!(error instanceof Error) && typeof error === "object") {
  112. const newError = new Error(error.message);
  113. for (key of Object.keys(error || {})) {
  114. value = error[key];
  115. newError[key] = value;
  116. }
  117. error = newError;
  118. }
  119. // filter paths from the message to avoid duplicate errors in sentry
  120. // (e.g. errors from `fs` methods which have a path attribute)
  121. try {
  122. if (error.path) {
  123. error.message = error.message.replace(` '${error.path}'`, "");
  124. }
  125. } catch (error1) {}
  126. // send the error to sentry
  127. try {
  128. this.raven.captureException(error, { tags, extra, level });
  129. // put a flag on the errors to avoid reporting them multiple times
  130. return (() => {
  131. const result = [];
  132. for (key in attributes) {
  133. value = attributes[key];
  134. if (value instanceof Error) {
  135. result.push((value.reportedToSentry = true));
  136. } else {
  137. result.push(undefined);
  138. }
  139. }
  140. return result;
  141. })();
  142. } catch (error2) {
  143. return;
  144. }
  145. }
  146. },
  147. debug() {
  148. return this.logger.debug.apply(this.logger, arguments);
  149. },
  150. info() {
  151. return this.logger.info.apply(this.logger, arguments);
  152. },
  153. log() {
  154. return this.logger.info.apply(this.logger, arguments);
  155. },
  156. error(attributes, message, ...args) {
  157. if (this.isProduction) {
  158. attributes.logBuffer = this.ringBuffer.records;
  159. }
  160. this.logger.error(attributes, message, ...Array.from(args));
  161. if (this.raven != null) {
  162. const MAX_ERRORS = 5; // maximum number of errors in 1 minute
  163. const now = new Date();
  164. // have we recently reported an error?
  165. const recentSentryReport = now - this.lastErrorTimeStamp < 60 * 1000;
  166. // if so, increment the error count
  167. if (recentSentryReport) {
  168. this.lastErrorCount++;
  169. } else {
  170. this.lastErrorCount = 0;
  171. this.lastErrorTimeStamp = now;
  172. }
  173. // only report 5 errors every minute to avoid overload
  174. if (this.lastErrorCount < MAX_ERRORS) {
  175. // add a note if the rate limit has been hit
  176. const note =
  177. this.lastErrorCount + 1 === MAX_ERRORS ? "(rate limited)" : "";
  178. // report the exception
  179. return this.captureException(attributes, message, `error${note}`);
  180. }
  181. }
  182. },
  183. err() {
  184. return this.error.apply(this, arguments);
  185. },
  186. warn() {
  187. return this.logger.warn.apply(this.logger, arguments);
  188. },
  189. fatal(attributes, message, callback) {
  190. if (callback == null) {
  191. callback = function() {};
  192. }
  193. this.logger.fatal(attributes, message);
  194. if (this.raven != null) {
  195. var cb = function(e) {
  196. // call the callback once after 'logged' or 'error' event
  197. callback();
  198. return (cb = function() {});
  199. };
  200. this.captureException(attributes, message, "fatal");
  201. this.raven.once("logged", cb);
  202. return this.raven.once("error", cb);
  203. } else {
  204. return callback();
  205. }
  206. }
  207. };
  208. Logger.initialize("default-sharelatex");