又造了一个小轮子, 方便调试用, log4js的扩展~
发布于 9 年前 作者 gynmi 5028 次浏览 最后一次编辑是 8 年前 来自 分享

此模块是借鉴log4js-node-extend模块, 再添加一些自主想法, 具体可以看文档 传送门: log4jx

Installation

npm install log4jx

Usage

var log4js = require("log4js");
var log4jx = require("log4jx");

/// 只需要添加4~5行代码就可以拥有追踪输出代码的功能, 还犹豫什么, 把星星点亮吧
log4jx(log4js, {
  path  : __dirname,
  format: "[@date] [@level][@pid][@port][@memory][@worker] @category -@data (@file:[@method]:@line:@column)"
});
/// set port number
process.port = 3000;

log4js.configure({
  appenders     : [
    {
      type: 'console'
    }
  ],
  replaceConsole: true
});

function getLogger(name, level) {
  var dateFileLog = log4js.getLogger(name);
  level = level || log4js.levels.INFO;
  dateFileLog.setLevel(level);
  return dateFileLog;
}

var logger = getLogger("category", "DEBUG");

function getApple() {
  console.log("test console");
  logger.info("test info");
  logger.debug("test debug");
  logger.debug("test debug", {name:"tom"},{age:100});
  logger.warn("test warn");
  logger.error("test error");
  
  //->[2015-06-06 14:01:31.288] [I][36386][3000][20M][ M ] console - test console  (full_logger.js:[getApple]:48:10)
  //->[2015-06-06 14:01:31.291] [I][36386][3000][20M][ M ] category - test info  (full_logger.js:[getApple]:49:10)
  //->[2015-06-06 14:01:31.292] [D][36386][3000][20M][ M ] category - test debug  (full_logger.js:[getApple]:50:10)
  //->[2015-06-06 14:01:31.292] [D][36386][3000][20M][ M ] category - test debug object { name: 'tom' } { age: 100 }  (full_logger.js:[getApple]:51:10)
  //->[2015-06-06 14:01:31.294] [W][36386][3000][20M][ M ] category - test warn  (full_logger.js:[getApple]:52:10)
  //->[2015-06-06 14:01:31.294] [E][36386][3000][20M][ M ] category - test error  (full_logger.js:[getApple]:53:10)
}

getApple();

OR

var log4jx = require("log4jx");

/// set output format
var log4js = log4jx({
  path  : __dirname,
  format: "[@date] [@level][@pid][@port][@memory][@worker] @category -@data (@file:[@method]:@line:@column)"
});

/// set port number if you need `@port` tag
process.port = 3000;

function getApple() {
  console.log("test console");

  //->[2015-06-06 14:01:31.288] [I][36386][3000][20M][ M ] console - test console  (logger.js:[getApple]:48:10)
}

getApple();

回到顶部