Github https://github.com/log4js-node/log4js-node
NPM https://www.npmjs.com/package/log4js

  1. npm install log4js

日志类型

  1. const log4js = require("log4js");
  2. log4js.configure({
  3. appenders: { cheese: { type: "file", filename: "cheese.log" } },
  4. categories: { default: { appenders: ["cheese"], level: "error" } }
  5. });
  6. const logger = log4js.getLogger("cheese");
  7. logger.trace("Entering cheese testing");
  8. logger.debug("Got cheese.");
  9. logger.info("Cheese is Comté.");
  10. logger.warn("Cheese is quite smelly.");
  11. logger.error("Cheese is too ripe!");
  12. logger.fatal("Cheese was breeding ground for listeria.");

日志分级

从上到下,级别逐渐递增
image.png

  1. const logger = log4js.getLogger("cheese");
  2. logger.trace("Entering cheese testing");
  3. logger.debug("Got cheese.");
  4. logger.info("Cheese is Comté.");
  5. logger.warn("Cheese is quite smelly.");
  6. logger.error("Cheese is too ripe!");
  7. logger.fatal("Cheese was breeding ground for listeria.");

参考 https://zhuanlan.zhihu.com/p/22110802

appenders 追加器

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

  1. const log4js = require('log4js');
  2. log4js.configure({
  3. appenders: {
  4. out: { type: 'stdout' },
  5. app: { type: 'file', filename: 'application.log' }
  6. },
  7. categories: {
  8. default: { appenders: [ 'out', 'app' ], level: 'debug' }
  9. }
  10. });

log4 https://zhuanlan.zhihu.com/p/22110802

categories 日志类型