test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import * as log4js from './log4js';
  2. log4js.configure('./filename');
  3. const logger1 = log4js.getLogger();
  4. logger1.level = 'debug';
  5. logger1.debug("Some debug messages");
  6. logger1.fatal({
  7. whatever: 'foo'
  8. })
  9. const logger3 = log4js.getLogger('cheese');
  10. logger3.trace('Entering cheese testing');
  11. logger3.debug('Got cheese.');
  12. logger3.info('Cheese is Gouda.');
  13. logger3.warn('Cheese is quite smelly.');
  14. logger3.error('Cheese is too ripe!');
  15. logger3.fatal('Cheese was breeding ground for listeria.');
  16. log4js.configure({
  17. appenders: { cheese: { type: 'console', filename: 'cheese.log' } },
  18. categories: { default: { appenders: ['cheese'], level: 'error' } }
  19. });
  20. log4js.configure({
  21. appenders: {
  22. out: { type: 'file', filename: 'pm2logs.log' }
  23. },
  24. categories: {
  25. default: { appenders: ['out'], level: 'info' }
  26. },
  27. pm2: true,
  28. pm2InstanceVar: 'INSTANCE_ID'
  29. });
  30. log4js.addLayout('json', config => function (logEvent) {
  31. return JSON.stringify(logEvent) + config.separator;
  32. });
  33. log4js.configure({
  34. appenders: {
  35. out: { type: 'stdout', layout: { type: 'json', separator: ',' } }
  36. },
  37. categories: {
  38. default: { appenders: ['out'], level: 'info' }
  39. }
  40. });
  41. log4js.configure({
  42. appenders: {
  43. file: { type: 'dateFile', filename: 'thing.log', pattern: '.mm' }
  44. },
  45. categories: {
  46. default: { appenders: ['file'], level: 'debug' }
  47. }
  48. });
  49. const logger4 = log4js.getLogger('thing');
  50. logger4.log('logging a thing');
  51. const logger5 = log4js.getLogger('json-test');
  52. logger5.info('this is just a test');
  53. logger5.error('of a custom appender');
  54. logger5.warn('that outputs json');
  55. log4js.shutdown();
  56. log4js.configure({
  57. appenders: {
  58. cheeseLogs: { type: 'file', filename: 'cheese.log' },
  59. console: { type: 'console' }
  60. },
  61. categories: {
  62. cheese: { appenders: ['cheeseLogs'], level: 'error' },
  63. another: { appenders: ['console'], level: 'trace' },
  64. default: { appenders: ['console', 'cheeseLogs'], level: 'trace' }
  65. }
  66. });
  67. const logger6 = log4js.getLogger('cheese');
  68. // only errors and above get logged.
  69. const otherLogger = log4js.getLogger();
  70. // this will get coloured output on console, and appear in cheese.log
  71. otherLogger.error('AAArgh! Something went wrong', { some: 'otherObject', useful_for: 'debug purposes' });
  72. otherLogger.log('This should appear as info output');
  73. // these will not appear (logging level beneath error)
  74. logger6.trace('Entering cheese testing');
  75. logger6.debug('Got cheese.');
  76. logger6.info('Cheese is Gouda.');
  77. logger6.log('Something funny about cheese.');
  78. logger6.warn('Cheese is quite smelly.');
  79. // these end up only in cheese.log
  80. logger6.error('Cheese %s is too ripe!', 'gouda');
  81. logger6.fatal('Cheese was breeding ground for listeria.');
  82. // these don't end up in cheese.log, but will appear on the console
  83. const anotherLogger = log4js.getLogger('another');
  84. anotherLogger.debug('Just checking');
  85. // will also go to console and cheese.log, since that's configured for all categories
  86. const pantsLog = log4js.getLogger('pants');
  87. pantsLog.debug('Something for pants');
  88. import { configure, getLogger } from './log4js';
  89. configure('./filename');
  90. const logger2 = getLogger();
  91. logger2.level = 'debug';
  92. logger2.debug("Some debug messages");
  93. configure({
  94. appenders: { cheese: { type: 'file', filename: 'cheese.log' } },
  95. categories: { default: { appenders: ['cheese'], level: 'error' } }
  96. });
  97. log4js.configure('./filename').getLogger();
  98. const logger7 = log4js.getLogger();
  99. logger7.level = 'debug';
  100. logger7.debug("Some debug messages");
  101. const levels: log4js.Levels = log4js.levels;
  102. const level: log4js.Level = levels.getLevel('info');
  103. log4js.connectLogger(logger1, {
  104. format: ':x, :y',
  105. level: 'info',
  106. context: true
  107. });
  108. log4js.connectLogger(logger2, {
  109. format: (req, _res, format) => format(`:remote-addr - ${req.id} - ":method :url HTTP/:http-version" :status :content-length ":referrer" ":user-agent"`)
  110. });
  111. //support for passing in an appender module
  112. log4js.configure({
  113. appenders: { thing: { type: { configure: () => {} }}},
  114. categories: { default: { appenders: ['thing'], level: 'debug'}}
  115. });