target.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. var __read = (this && this.__read) || function (o, n) {
  3. var m = typeof Symbol === "function" && o[Symbol.iterator];
  4. if (!m) return o;
  5. var i = m.call(o), r, ar = [], e;
  6. try {
  7. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  8. }
  9. catch (error) { e = { error: error }; }
  10. finally {
  11. try {
  12. if (r && !r.done && (m = i["return"])) m.call(i);
  13. }
  14. finally { if (e) throw e.error; }
  15. }
  16. return ar;
  17. };
  18. var __spread = (this && this.__spread) || function () {
  19. for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
  20. return ar;
  21. };
  22. Object.defineProperty(exports, "__esModule", { value: true });
  23. var fs_1 = require("fs");
  24. var path_1 = require("path");
  25. var cache_getters_1 = require("../utils/cache-getters");
  26. var level_1 = require("./level");
  27. var message_1 = require("./message");
  28. var logTargetWithLevelRegex = new RegExp("^\\s*(.+):([0-9]+|" + level_1.LogLevelNames.join('|') + ")\\s*$", 'i');
  29. var parseLogTargets = function (targetString) {
  30. var items = (targetString || '').split(/([^\\]),/g).reduce(function (list, item, index) {
  31. if (index % 2 === 1) {
  32. list[list.length - 1] += item;
  33. }
  34. else {
  35. list.push(item);
  36. }
  37. return list;
  38. }, []);
  39. return items.reduce(function (targets, str) {
  40. var format;
  41. str = str.replace(/^(.+)%([a-z_][a-z0-9_]*)$/, function (_, before, key) {
  42. format = message_1.LogFormatters[key];
  43. return before;
  44. });
  45. var pieces = str.match(logTargetWithLevelRegex);
  46. var file;
  47. var level;
  48. if (pieces) {
  49. file = pieces[1].trim();
  50. level = pieces[2].trim();
  51. }
  52. else {
  53. file = str.trim();
  54. }
  55. var append = file.endsWith('+');
  56. if (append) {
  57. file = file.slice(0, -1).trim();
  58. }
  59. file = file.replace(/\\,/g, ',');
  60. if (!file) {
  61. return targets;
  62. }
  63. var isStandardFd = /^(stdout|stderr)$/i.test(file);
  64. if (format == null) {
  65. format = isStandardFd ? message_1.LogFormatters.simple : message_1.LogFormatters.json;
  66. }
  67. var target = cache_getters_1.cacheGetters({
  68. format: format,
  69. get minLevel() {
  70. return level_1.parseLogLevel(level) || -Infinity;
  71. },
  72. get stream() {
  73. if (isStandardFd) {
  74. return process[file.toLowerCase()];
  75. }
  76. else {
  77. return fs_1.createWriteStream(path_1.resolve(process.cwd(), file), {
  78. flags: append ? 'a' : 'w',
  79. autoClose: true,
  80. encoding: 'utf8',
  81. });
  82. }
  83. },
  84. }, 'minLevel', 'stream');
  85. return __spread(targets, [target]);
  86. }, []);
  87. };
  88. exports.parseLogTargets = parseLogTargets;
  89. var DEFAULT_LOG_TARGET = "stderr:" + level_1.LogLevels.warn;
  90. exports.DEFAULT_LOG_TARGET = DEFAULT_LOG_TARGET;