API Docs for: 0.8.0
Show:

File: include/utils/logging.js

  1. /*
  2. Copyright (C) 2016 PencilBlue, LLC
  3.  
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. 'use strict';
  18.  
  19. //dependencies
  20. var path = require('path');
  21. var cluster = require('cluster');
  22. var winston = require('winston');
  23. var util = require('../util.js');
  24.  
  25. module.exports = function LogFactory(config){
  26.  
  27. //verify that we have a valid logging configuration provided
  28. if (!util.isObject(config.logging)) {
  29. config.logging = {};
  30. }
  31. if (!util.isString(config.logging.level)) {
  32. config.logging.level = "info";
  33. }
  34. if (!util.isArray(config.logging.transports)) {
  35.  
  36. //initialize transports with console by default
  37. config.logging.transports = [
  38. new (winston.transports.Console)({ level: config.logging.level, timestamp: true, label: cluster.worker ? cluster.worker.id : 'M'}),
  39. ];
  40.  
  41. //when a log file path is provided log to a file
  42. if (util.isString(config.logging.file)) {
  43.  
  44. //ensure the directory structure exists
  45. util.mkdirsSync(config.logging.file, true, util.cb);
  46.  
  47. //add the transport
  48. var fileTransport = new (winston.transports.File)({ filename: config.logging.file, level: config.logging.level, timestamp: true });
  49. config.logging.transports.push(fileTransport);
  50. }
  51. }
  52.  
  53. //configure winston
  54. var logger = new (winston.Logger)({
  55. transports: config.logging.transports,
  56. level: config.logging.level,
  57. padLevels: false
  58. });
  59.  
  60. /**
  61. * Determines if the root log level is set to debug or silly
  62. * @method isDebug
  63. * @return {Boolean}
  64. */
  65. logger.isDebug = function(){
  66. return logger.levels[logger.level] >= logger.levels.debug;
  67. };
  68.  
  69. /**
  70. * Determines if the root log level is set to silly
  71. * @method isSilly
  72. * @return {Boolean}
  73. */
  74. logger.isSilly = function(){
  75. return logger.levels[logger.level] >= logger.levels.silly;
  76. };
  77.  
  78. //return the conifgured logger instance
  79. logger.info('SystemStartup: Log Level is: '+config.logging.level);
  80. return logger;
  81. };
  82.