2013-10-22 02:29:57 -05:00
|
|
|
var Logger = require('bunyan')
|
|
|
|
, Stream = require('stream')
|
|
|
|
, utils = require('./utils')
|
2013-10-11 00:32:59 -05:00
|
|
|
|
|
|
|
function getlvl(x) {
|
|
|
|
if (x < 15) {
|
2013-10-26 07:18:36 -05:00
|
|
|
return 'trace'
|
2013-10-11 00:32:59 -05:00
|
|
|
} else if (x < 25) {
|
2013-10-26 07:18:36 -05:00
|
|
|
return 'debug'
|
2013-10-11 00:32:59 -05:00
|
|
|
} else if (x < 35) {
|
2013-10-26 07:18:36 -05:00
|
|
|
return 'info'
|
2013-10-11 00:32:59 -05:00
|
|
|
} else if (x == 35) {
|
2013-10-26 07:18:36 -05:00
|
|
|
return 'http'
|
2013-10-11 00:32:59 -05:00
|
|
|
} else if (x < 45) {
|
2013-10-26 07:18:36 -05:00
|
|
|
return 'warn'
|
2013-10-11 00:32:59 -05:00
|
|
|
} else if (x < 55) {
|
2013-10-26 07:18:36 -05:00
|
|
|
return 'error'
|
2013-10-11 00:32:59 -05:00
|
|
|
} else {
|
2013-10-26 07:18:36 -05:00
|
|
|
return 'fatal'
|
2013-10-11 00:32:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.setup = function(logs) {
|
2013-10-26 07:18:36 -05:00
|
|
|
var streams = []
|
|
|
|
if (!logs) logs = [{ type: 'stdout', format: 'pretty', level: 'http' }]
|
2013-10-11 00:32:59 -05:00
|
|
|
|
|
|
|
logs.forEach(function(target) {
|
2013-10-26 07:18:36 -05:00
|
|
|
var stream = new Stream()
|
|
|
|
stream.writable = true
|
2013-10-11 00:32:59 -05:00
|
|
|
|
|
|
|
if (target.type === 'stdout' || target.type === 'stderr') {
|
|
|
|
// destination stream
|
2013-10-26 07:18:36 -05:00
|
|
|
var dest = target.type === 'stdout' ? process.stdout : process.stderr
|
2013-10-11 00:32:59 -05:00
|
|
|
|
|
|
|
if (target.format === 'pretty') {
|
|
|
|
// making fake stream for prettypritting
|
|
|
|
stream.write = function(obj) {
|
2013-10-26 07:18:36 -05:00
|
|
|
dest.write(print(obj.level, obj.msg, obj, dest.isTTY) + "\n")
|
2013-10-11 00:32:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stream.write = function(obj) {
|
2013-10-26 07:18:36 -05:00
|
|
|
dest.write(JSON.stringify(obj, Logger.safeCycles()) + "\n")
|
2013-10-11 00:32:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (target.type === 'file') {
|
2013-10-26 07:18:36 -05:00
|
|
|
var dest = require('fs').createWriteStream(target.path, {flags: 'a', encoding: 'utf8'})
|
2013-10-11 00:32:59 -05:00
|
|
|
dest.on('error', function (err) {
|
2013-10-26 07:18:36 -05:00
|
|
|
Logger.emit('error', err)
|
|
|
|
})
|
2013-10-11 00:32:59 -05:00
|
|
|
stream.write = function(obj) {
|
2013-10-26 07:18:36 -05:00
|
|
|
dest.write(JSON.stringify(obj, Logger.safeCycles()) + "\n")
|
2013-10-11 00:32:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
2013-10-26 07:18:36 -05:00
|
|
|
throw new Error('wrong target type for a log')
|
2013-10-11 00:32:59 -05:00
|
|
|
}
|
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
if (target.level === 'http') target.level = 35
|
2013-10-11 00:32:59 -05:00
|
|
|
streams.push({
|
|
|
|
type: "raw",
|
|
|
|
level: target.level || 35,
|
|
|
|
stream: stream,
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
|
|
|
})
|
2013-10-11 00:32:59 -05:00
|
|
|
|
|
|
|
var logger = new Logger({
|
|
|
|
name: 'sinopia',
|
|
|
|
streams: streams,
|
|
|
|
serializers: {
|
|
|
|
err: Logger.stdSerializers.err,
|
|
|
|
req: Logger.stdSerializers.req,
|
|
|
|
res: Logger.stdSerializers.res,
|
|
|
|
},
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
2013-10-11 00:32:59 -05:00
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
module.exports.logger = logger
|
2013-10-11 00:32:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// adopted from socket.io
|
|
|
|
// this part was converted to coffee-script and back again over the years,
|
|
|
|
// so it might look weird
|
|
|
|
|
|
|
|
// level to color
|
|
|
|
var levels = {
|
|
|
|
fatal: 31,
|
|
|
|
error: 31,
|
|
|
|
warn: 33,
|
|
|
|
http: 35,
|
|
|
|
info: 36,
|
|
|
|
debug: 90,
|
|
|
|
trace: 90,
|
2013-10-26 07:18:36 -05:00
|
|
|
}
|
2013-10-11 00:32:59 -05:00
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
var max = 0
|
2013-10-11 00:32:59 -05:00
|
|
|
for (var l in levels) {
|
2013-10-26 07:18:36 -05:00
|
|
|
max = Math.max(max, l.length)
|
2013-10-11 00:32:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function pad(str) {
|
2013-10-26 07:18:36 -05:00
|
|
|
if (str.length < max) return str + new Array(max - str.length + 1).join(' ')
|
|
|
|
return str
|
|
|
|
}
|
2013-10-11 00:32:59 -05:00
|
|
|
|
2013-10-12 02:57:59 -05:00
|
|
|
var subsystems = [{
|
2013-12-07 17:54:26 -05:00
|
|
|
in: '\033[32m<--\033[39m',
|
|
|
|
out: '\033[33m-->\033[39m',
|
|
|
|
fs: '\033[90m-=-\033[39m',
|
|
|
|
default: '\033[34m---\033[39m',
|
2013-10-12 02:57:59 -05:00
|
|
|
}, {
|
|
|
|
in: '<--',
|
|
|
|
out: '-->',
|
2013-10-18 15:45:36 -05:00
|
|
|
fs: '-=-',
|
2013-10-12 02:57:59 -05:00
|
|
|
default: '---',
|
2013-10-18 15:45:36 -05:00
|
|
|
}]
|
2013-10-12 02:57:59 -05:00
|
|
|
|
|
|
|
function print(type, msg, obj, colors) {
|
2013-10-26 07:18:36 -05:00
|
|
|
if (typeof type === 'number') type = getlvl(type)
|
2013-10-11 00:32:59 -05:00
|
|
|
var finalmsg = msg.replace(/@{(!?[$A-Za-z_][$0-9A-Za-z\._]*)}/g, function(_, name) {
|
2013-10-26 07:18:36 -05:00
|
|
|
var str = obj, is_error
|
2013-10-11 00:32:59 -05:00
|
|
|
if (name[0] === '!') {
|
2013-10-26 07:18:36 -05:00
|
|
|
name = name.substr(1)
|
|
|
|
is_error = true
|
2013-10-11 00:32:59 -05:00
|
|
|
}
|
|
|
|
|
2013-10-26 07:18:36 -05:00
|
|
|
var _ref = name.split('.')
|
2013-10-11 00:32:59 -05:00
|
|
|
for (var _i = 0; _i < _ref.length; _i++) {
|
2013-10-26 07:18:36 -05:00
|
|
|
var id = _ref[_i]
|
2013-10-22 02:29:57 -05:00
|
|
|
if (utils.is_object(str) || Array.isArray(str)) {
|
2013-10-26 07:18:36 -05:00
|
|
|
str = str[id]
|
2013-10-11 00:32:59 -05:00
|
|
|
} else {
|
2013-10-26 07:18:36 -05:00
|
|
|
str = undefined
|
2013-10-11 00:32:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof(str) === 'string') {
|
2013-10-12 02:57:59 -05:00
|
|
|
if (!colors) {
|
2013-10-26 07:18:36 -05:00
|
|
|
return str
|
2013-10-12 02:57:59 -05:00
|
|
|
} else if (is_error) {
|
2013-12-07 17:54:26 -05:00
|
|
|
return '\033[31m' + str + '\033[39m'
|
2013-10-11 00:32:59 -05:00
|
|
|
} else {
|
2013-12-07 17:54:26 -05:00
|
|
|
return '\033[32m' + str + '\033[39m'
|
2013-10-11 00:32:59 -05:00
|
|
|
}
|
|
|
|
} else {
|
2013-10-26 07:18:36 -05:00
|
|
|
return require('util').inspect(str, void 0, void 0, colors)
|
2013-10-11 00:32:59 -05:00
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
})
|
|
|
|
var sub = subsystems[+!colors][obj.sub] || subsystems[+!colors].default
|
2013-10-12 02:57:59 -05:00
|
|
|
// ^^--- black magic... kidding, just "colors ? 0 : 1"
|
2013-10-11 00:32:59 -05:00
|
|
|
|
2013-10-12 02:57:59 -05:00
|
|
|
if (colors) {
|
2013-12-07 17:54:26 -05:00
|
|
|
return " \033[" + levels[type] + "m" + (pad(type)) + "\033[39m " + sub + " " + finalmsg
|
2013-10-12 02:57:59 -05:00
|
|
|
} else {
|
2013-10-26 07:18:36 -05:00
|
|
|
return " " + (pad(type)) + " " + sub + " " + finalmsg
|
2013-10-12 02:57:59 -05:00
|
|
|
}
|
2013-10-26 07:18:36 -05:00
|
|
|
}
|
2013-10-11 00:32:59 -05:00
|
|
|
|