Last active
December 16, 2015 14:49
-
-
Save QETHAN/5450955 to your computer and use it in GitHub Desktop.
node.js log工具 来自CNode社区
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
/* | |
Foreground Colours | |
30 Black | |
31 Red | |
32 Green | |
33 Yellow | |
34 Blue | |
35 Magenta | |
36 Cyan | |
37 White | |
38 缺省前景色 设置下划线 | |
39 缺省前景色 关闭下划线 | |
Background Colours | |
40 Black | |
41 Red | |
42 Green | |
43 Yellow | |
44 Blue | |
45 Magenta | |
46 Cyan | |
47 White | |
文本终端的颜色可以使用“ANSI非常规字符序列”来生成。举例: | |
echo -e “\033[44;37;5m ME \033[0m COOL” | |
以上命令设置作用如下:背景色为蓝色,前景色为白色,字体闪烁,输出字符“ME”,然后重新设置屏幕到缺省设置,输出字符 “COOL”。“e”是命令 echo 的一个可选项,它用于激活特殊字符的解析器。“\033”引导非常规字符序列。“m”意味着设置属性然后结束非常规字符序列,这个例子里真正有效的字符是 “44;37;5” 和“0”。修改“44;37;5”可以生成不同颜色的组合,数值和编码的前后顺序没有关系。 | |
*/ | |
var cwd = process.cwd() + '/', | |
INFO = 0; | |
DEBUG = 1; | |
WARNING = 2; | |
ERROR = 3; | |
TRACE = 4; | |
INIT = 6; | |
type = ['INFO', 'DEBUG', 'WARNING', 'ERROR', 'TRACE', '', 'LOG_INIT']; | |
colors = [38, 34, 35, 31, 32, 36, 33]; | |
bufferSize = 20000; | |
writeSize = 16384; | |
exports.INFO = INFO; | |
exports.DEBUG = DEBUG; | |
exports.WARNING = WARNING; | |
exports.ERROR = ERROR; | |
exports.TRACE = TRACE; | |
function getPos() { | |
try { | |
throw new Error(); | |
} catch(e) { | |
console.log('------>'+e.stack); | |
var pos = e.stack.split('\n')[4].split('(')[1].split(')')[0].split(':'); | |
return pos[0].replace(cwd, '') + ':' + pos[1]; | |
} | |
} | |
function pad2(num) { | |
return num > 9 ? num : '0' + num; | |
} | |
function getTime() { | |
var t = new Date(); | |
return [t.getFullYear(), '-', pad2(t.getMonth() + 1) , '-', pad2(t.getDate()), ' ', | |
pad2(t.getHours()), ':', pad2(t.getMinutes()), ':', pad2(t.getSeconds())].join(''); | |
} | |
function formatLog(log, color) { | |
var tag = head = foot = ''; | |
if (color) { | |
head = '\x1B['; | |
foot = '\x1B[0m'; | |
tag = colors[5]+'m'; | |
color = colors[log.type]+'m'; | |
} | |
return [log.time, ' [', head, color, type[log.type], foot, '] [', head, tag, log.pos, foot, '] ', log.msg].join(''); | |
} | |
exports.create = function(level, file) { | |
if (!level) { | |
level = INFO; | |
} | |
if (file) { | |
var buffer = new Buffer(bufferSize); | |
var pos = 0; | |
var fd = fs.openSync(file, 'a'); | |
process.on('exit', function(){ | |
fs.writeSync(fd, buffer, 0, pos, null); | |
}) | |
} | |
function log(type, msg) { | |
if (type < level){ | |
return; | |
} | |
var log = {type:type, msg:msg, time:getTime(), pos:getPos()}; | |
console.log(formatLog(log, true)); | |
if (file) { | |
if (pos >= writeSize) { | |
fs.writeSync(fd, buffer, 0, pos, null); | |
pos = 0; | |
} | |
pos += buffer.write(formatLog(log) + "\r\n", pos); | |
} | |
} | |
console.log(formatLog({type:INIT, pos:file, time:getTime(), msg: 'log init with level ' + type[level]}, true)); | |
return { | |
info : function(msg) {log(INFO, msg);}, | |
debug : function(msg) {log(DEBUG, msg);}, | |
warning : function(msg) {log(WARNING, msg);}, | |
error : function(msg) {log(ERROR, msg);}, | |
trace : function(msg) {log(TRACE, msg);}, | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var log = require('./log'), | |
logWithoutFile = log.create(); | |
logWithFile = log.create(log.WARNING, 'my.log'), | |
logWithoutFile.info('info msg'); | |
logWithoutFile.debug('debug msg'); | |
logWithoutFile.warning('warning msg'); | |
logWithoutFile.error('error msg'); | |
logWithoutFile.trace('trace msg'); | |
logWithFile.info('info msg'); | |
logWithFile.debug('debug msg'); | |
logWithFile.warning('warning msg'); | |
logWithFile.error('error msg'); | |
// logWithFile.trace('trace msg'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment