logger.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. "use strict";
  2. var messages = require("./connect-utils");
  3. var utils = require("./utils");
  4. var _ = require("./lodash.custom");
  5. var template = "[{blue:%s}] ";
  6. var logger = require("eazy-logger").Logger({
  7. useLevelPrefixes: false
  8. });
  9. module.exports.logger = logger;
  10. /**
  11. * @param name
  12. * @returns {*}
  13. */
  14. module.exports.getLogger = function (name) {
  15. return logger.clone(function (config) {
  16. config.prefix = config.prefix + template.replace("%s", name);
  17. return config;
  18. });
  19. };
  20. /**
  21. * Logging Callbacks
  22. */
  23. module.exports.callbacks = {
  24. /**
  25. * Log when file-watching has started
  26. * @param {BrowserSync} bs
  27. * @param data
  28. */
  29. "file:watching": function (bs, data) {
  30. if (Object.keys(data).length) {
  31. logger.info("Watching files...");
  32. }
  33. },
  34. /**
  35. * Log when a file changes
  36. * @param {BrowserSync} bs
  37. * @param data
  38. */
  39. "file:reload": function (bs, data) {
  40. if (canLogFileChange(bs, data)) {
  41. if (data.path[0] === "*") {
  42. return logger.info("{cyan:Reloading files that match: {magenta:%s", data.path);
  43. }
  44. logger.info("{cyan:File event [" + data.event + "] : {magenta:%s", data.path);
  45. }
  46. },
  47. /**
  48. *
  49. */
  50. "service:exit": function () {
  51. logger.debug("Exiting...");
  52. },
  53. /**
  54. *
  55. */
  56. "browser:reload": function (bs, data = {}) {
  57. if (canLogFileChange(bs)) {
  58. if (data.files && data.files.length > 1) {
  59. return logger.info(`{cyan:Reloading Browsers... (buffered ${data.files.length} events)`);
  60. }
  61. logger.info("{cyan:Reloading Browsers...");
  62. }
  63. },
  64. /**
  65. *
  66. */
  67. "browser:error": function () {
  68. logger.error("Couldn't open browser (if you are using BrowserSync in a headless environment, you might want to set the {cyan:open} option to {cyan:false})");
  69. },
  70. /**
  71. * @param {BrowserSync} bs
  72. * @param data
  73. */
  74. "stream:changed": function (bs, data) {
  75. if (canLogFileChange(bs)) {
  76. var changed = data.changed;
  77. logger.info("{cyan:%s %s changed} ({magenta:%s})", changed.length, changed.length > 1 ? "files" : "file", changed.join(", "));
  78. }
  79. },
  80. /**
  81. * Client connected logging
  82. * @param {BrowserSync} bs
  83. * @param data
  84. */
  85. "client:connected": function (bs, data) {
  86. var uaString = utils.getUaString(data.ua);
  87. var msg = "{cyan:Browser Connected: {magenta:%s, version: %s}";
  88. var method = "info";
  89. if (!bs.options.get("logConnections")) {
  90. method = "debug";
  91. }
  92. logger.log(method, msg, uaString.name, uaString.version);
  93. },
  94. /**
  95. * Main logging when the service is running
  96. * @param {BrowserSync} bs
  97. * @param data
  98. */
  99. "service:running": function (bs, data) {
  100. const type = data.type;
  101. if (bs.options.get("json")) {
  102. return console.log(JSON.stringify({
  103. "service:running": {
  104. options: bs.options.toJS()
  105. }
  106. }));
  107. }
  108. if (type === "server") {
  109. var baseDir = bs.options.getIn(["server", "baseDir"]);
  110. logUrls(bs.options.get("urls").toJS());
  111. if (baseDir) {
  112. if (utils.isList(baseDir)) {
  113. baseDir.forEach(serveFiles);
  114. }
  115. else {
  116. serveFiles(baseDir);
  117. }
  118. }
  119. }
  120. if (type === "proxy") {
  121. logger.info("Proxying: {cyan:%s}", bs.options.getIn(["proxy", "target"]));
  122. logUrls(bs.options.get("urls").toJS());
  123. }
  124. if (type === "snippet") {
  125. if (bs.options.get("logSnippet")) {
  126. logger.info("{bold:Copy the following snippet into your website, " +
  127. "just before the closing {cyan:</body>} tag");
  128. logger.unprefixed("info", messages.scriptTags(bs.options));
  129. }
  130. logUrls(bs.options
  131. .get("urls")
  132. .filter(function (value, key) {
  133. return key.slice(0, 2) === "ui";
  134. })
  135. .toJS());
  136. }
  137. function serveFiles(base) {
  138. logger.info("Serving files from: {magenta:%s}", base);
  139. }
  140. }
  141. };
  142. /**
  143. * Plugin interface for BrowserSync
  144. * @param {EventEmitter} emitter
  145. * @param {BrowserSync} bs
  146. * @returns {Object}
  147. */
  148. module.exports.plugin = function (emitter, bs) {
  149. var logPrefix = bs.options.get("logPrefix");
  150. var logLevel = bs.options.get("logLevel");
  151. // Should set logger level here!
  152. logger.setLevel(logLevel);
  153. if (logPrefix) {
  154. if (_.isFunction(logPrefix)) {
  155. logger.setPrefix(logPrefix);
  156. }
  157. else {
  158. logger.setPrefix(template.replace("%s", logPrefix));
  159. }
  160. }
  161. _.each(exports.callbacks, function (func, event) {
  162. emitter.on(event, func.bind(this, bs));
  163. });
  164. return logger;
  165. };
  166. /**
  167. *
  168. * @param urls
  169. */
  170. function logUrls(urls) {
  171. var keys = Object.keys(urls);
  172. var longestName = 0;
  173. var longesturl = 0;
  174. var offset = 2;
  175. if (!keys.length) {
  176. return;
  177. }
  178. var names = keys.map(function (key) {
  179. if (key.length > longestName) {
  180. longestName = key.length;
  181. }
  182. if (urls[key].length > longesturl) {
  183. longesturl = urls[key].length;
  184. }
  185. return key;
  186. });
  187. var underline = getChars(longestName + offset + longesturl + 1, "-");
  188. var underlined = false;
  189. logger.info("{bold:Access URLs:");
  190. logger.unprefixed("info", "{grey: %s", underline);
  191. keys.forEach(function (key, i) {
  192. var keyname = getKeyName(key);
  193. logger.unprefixed("info", " %s: {magenta:%s}", getPadding(key.length, longestName + offset) + keyname, urls[key]);
  194. if (!underlined && names[i + 1] && names[i + 1].indexOf("ui") > -1) {
  195. underlined = true;
  196. logger.unprefixed("info", "{grey: %s}", underline);
  197. }
  198. });
  199. logger.unprefixed("info", "{grey: %s}", underline);
  200. }
  201. /**
  202. * @param {Number} len
  203. * @param {Number} max
  204. * @returns {string}
  205. */
  206. function getPadding(len, max) {
  207. return new Array(max - (len + 1)).join(" ");
  208. }
  209. /**
  210. * @param {Number} len
  211. * @param {String} char
  212. * @returns {string}
  213. */
  214. function getChars(len, char) {
  215. return new Array(len).join(char);
  216. }
  217. /**
  218. * Transform url-key names into something more presentable
  219. * @param key
  220. * @returns {string}
  221. */
  222. function getKeyName(key) {
  223. if (key.indexOf("ui") > -1) {
  224. if (key === "ui") {
  225. return "UI";
  226. }
  227. if (key === "ui-external") {
  228. return "UI External";
  229. }
  230. }
  231. return key.substr(0, 1).toUpperCase() + key.substring(1);
  232. }
  233. /**
  234. * Determine if file changes should be logged
  235. * @param bs
  236. * @param data
  237. * @returns {boolean}
  238. */
  239. function canLogFileChange(bs, data) {
  240. if (data && data.log === false) {
  241. return false;
  242. }
  243. return bs.options.get("logFileChanges");
  244. }
  245. //# sourceMappingURL=logger.js.map