example.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var log4js = require('../lib/log4js');
  2. //log the cheese logger messages to a file, and the console ones as well.
  3. log4js.configure({
  4. appenders: [
  5. {
  6. type: "file",
  7. filename: "cheese.log",
  8. category: [ 'cheese','console' ]
  9. },
  10. {
  11. type: "console"
  12. }
  13. ],
  14. replaceConsole: true
  15. });
  16. //to add an appender programmatically, and without clearing other appenders
  17. //loadAppender is only necessary if you haven't already configured an appender of this type
  18. log4js.loadAppender('file');
  19. log4js.addAppender(log4js.appenders.file('pants.log'), 'pants');
  20. //a custom logger outside of the log4js/lib/appenders directory can be accessed like so
  21. //log4js.loadAppender('what/you/would/put/in/require');
  22. //log4js.addAppender(log4js.appenders['what/you/would/put/in/require'](args));
  23. //or through configure as:
  24. //log4js.configure({
  25. // appenders: [ { type: 'what/you/would/put/in/require', otherArgs: 'blah' } ]
  26. //});
  27. var logger = log4js.getLogger('cheese');
  28. //only errors and above get logged.
  29. //you can also set this log level in the config object
  30. //via the levels field.
  31. logger.setLevel('ERROR');
  32. //console logging methods have been replaced with log4js ones.
  33. //so this will get coloured output on console, and appear in cheese.log
  34. console.error("AAArgh! Something went wrong", { some: "otherObject", useful_for: "debug purposes" });
  35. //these will not appear (logging level beneath error)
  36. logger.trace('Entering cheese testing');
  37. logger.debug('Got cheese.');
  38. logger.info('Cheese is Gouda.');
  39. logger.warn('Cheese is quite smelly.');
  40. //these end up on the console and in cheese.log
  41. logger.error('Cheese %s is too ripe!', "gouda");
  42. logger.fatal('Cheese was breeding ground for listeria.');
  43. //these don't end up in cheese.log, but will appear on the console
  44. var anotherLogger = log4js.getLogger('another');
  45. anotherLogger.debug("Just checking");
  46. //one for pants.log
  47. //will also go to console, since that's configured for all categories
  48. var pantsLog = log4js.getLogger('pants');
  49. pantsLog.debug("Something for pants");