logger.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const _ = require('./underscore');
  2. import log4js from 'log4js';
  3. /**
  4. * Setup logging system.
  5. * @param {Object} [options]
  6. * @param {String} [options.logLevel] Minimum logging threshold (default: info).
  7. * @param {String} [options.logFolder] Log folder to write logs to.
  8. */
  9. exports.setup = function(options) {
  10. const logFolder = options.logFolder;
  11. const level = options.logLevel || 'info';
  12. const config = {
  13. appenders: {
  14. out: { type: 'console' },
  15. all: {
  16. type: 'file',
  17. filename: `${logFolder}/all.log`
  18. },
  19. main: {
  20. type: 'file',
  21. filename: `${logFolder}/category/main.log`
  22. },
  23. EthereumNode: {
  24. type: 'file',
  25. filename: `${logFolder}/category/ethereum_node.log`
  26. },
  27. swarm: {
  28. type: 'file',
  29. filename: `${logFolder}/category/swarm.log`
  30. }
  31. },
  32. categories: {
  33. default: { appenders: ['out', 'all', 'main'], level },
  34. EthereumNode: { appenders: ['out', 'all', 'EthereumNode'], level },
  35. swarm: { appenders: ['out', 'all', 'swarm'], level }
  36. }
  37. };
  38. log4js.configure(config);
  39. };
  40. exports.create = category => {
  41. const logger = log4js.getLogger(category);
  42. // Allow for easy creation of sub-categories.
  43. logger.create = subCategory => {
  44. return exports.create(`${category}/${subCategory}`);
  45. };
  46. return logger;
  47. };