链接



简介
https://juejin.im/post/57b962af7db2a200542a0fb3

官方文档
https://log4js-node.github.io/log4js-node/terms.html

github
https://github.com/log4js-node/log4js-node

栗子

官方栗子
https://github.com/log4js-node/log4js-example

  1. {
  2. "appenders": {
  3. "access": {
  4. "type": "dateFile",
  5. "filename": "log/access.log",
  6. "pattern": "-yyyy-MM-dd",
  7. "category": "http"
  8. },
  9. "app": {
  10. "type": "file",
  11. "filename": "log/app.log",
  12. "maxLogSize": 10485760,
  13. "numBackups": 3
  14. },
  15. "errorFile": {
  16. "type": "file",
  17. "filename": "log/errors.log"
  18. },
  19. "errors": {
  20. "type": "logLevelFilter",
  21. "level": "ERROR",
  22. "appender": "errorFile"
  23. }
  24. },
  25. "categories": {
  26. "default": {
  27. "appenders": ["app", "errors"],
  28. "level": "DEBUG"
  29. },
  30. "http": {
  31. "appenders": ["access"],
  32. "level": "DEBUG"
  33. }
  34. }
  35. }
  1. /**
  2. * Initialise log4js first, so we don't miss any log messages
  3. */
  4. var log4js = require('log4js');
  5. log4js.configure('./config/log4js.json');
  6. var log = log4js.getLogger("startup");
  7. var log = log4js.getLogger("app");



项目中使用的栗子
https://www.cnblogs.com/duhuo/p/5176154.html
注意, 这个栗子是旧版本的log4js

  1. {
  2. "appenders": [{
  3. "category": "console",
  4. "type": "console"
  5. },
  6. {
  7. "category": "log_info",
  8. "type": "file",
  9. "filename": "./logs/log_info/info.log",
  10. "maxLogSize": 104857500,
  11. "backups": 100
  12. },
  13. {
  14. "category": "log_stat",
  15. "type": "datefile",
  16. "filename": "./logs/log_stat/stat"
  17. },
  18. {
  19. "category": "log_trace",
  20. "type": "datefile",
  21. "filename": "./logs/log_trace/trace"
  22. },
  23. {
  24. "category": "log_error",
  25. "type": "datefile",
  26. "filename": "./logs/log_error/error"
  27. },
  28. {
  29. "category": "log_todo",
  30. "type": "datefile",
  31. "filename": "./logs/log_todo/todo"
  32. }
  33. ],
  34. "replaceConsole": true,
  35. "levels": {
  36. "log_info": "ALL",
  37. "log_stat": "ALL",
  38. "log_trace": "ALL",
  39. "log_error": "ALL",
  40. "log_todo": "ALL"
  41. }
  42. }

概念

总览


log4js - 图1
https://www.jianshu.com/p/9604d08db899

level

log4js - 图2

category

log4js.getLogger(‘somecategory’)

得到的结果:
[2016-08-21 01:16:00.212] [DEBUG] somecategory - Time: 2016-08-20T17:16:00.212Z

Appender


文档
https://log4js-node.github.io/log4js-node/appenders.html



使用邮件

https://github.com/log4js-node/smtp

Annex