Tracer
A powerful and customizable logging library for node.js
Install / Use
/learn @baryon/TracerREADME
tracer for node.js
A powerful and customizable logging library for node.js.
===========
Features
- print log messages with timestamp, file name, method name, line number, path or call stack
- be customized output format with micro-template and timestamp format
- support user-defined logging levels
- add easily any transport
- support filter functions, so print statements in full color and font (color console)
Install
npm install tracer --save
Usage
Add to your code:
Simple Console
var logger = require('tracer').console()
Color Console
var logger = require('tracer').colorConsole()
Set Output Level
var logger = require('tracer').colorConsole({ level: 'warn' })
Simple Example
Simple Console
var logger = require('tracer').console();
logger.log('hello');
logger.trace('hello', 'world');
logger.debug('hello %s', 'world', 123);
logger.info('hello %s %d', 'world', 123, {foo:'bar'});
logger.warn('hello %s %d %j', 'world', 123, {foo:'bar'});
logger.error('hello %s %d %j', 'world', 123, {foo:'bar'}, [1, 2, 3, 4], Object);
$ node example/console.js
2012-03-02T13:35:22.83Z <log> console.js:3 (Object.<anonymous>) hello
2012-03-02T13:35:22.85Z <trace> console.js:4 (Object.<anonymous>) hello world
2012-03-02T13:35:22.85Z <debug> console.js:5 (Object.<anonymous>) hello world 123
2012-03-02T13:35:22.85Z <info> console.js:6 (Object.<anonymous>) hello world 123 { foo: 'bar' }
2012-03-02T13:35:22.85Z <warn> console.js:7 (Object.<anonymous>) hello world 123 {"foo":"bar"}
2012-03-02T13:35:22.85Z <error> console.js:8 (Object.<anonymous>) hello world 123 {"foo":"bar"} [ 1, 2, 3, 4 ] function Object() { [native code] }
Color Console
var logger = require('tracer').colorConsole()
logger.log('hello')
logger.trace('hello', 'world')
logger.debug('hello %s', 'world', 123)
logger.info('hello %s %d', 'world', 123, { foo: 'bar' })
logger.warn('hello %s %d %j', 'world', 123, { foo: 'bar' })
logger.error(
'hello %s %d %j',
'world',
123,
{ foo: 'bar' },
[1, 2, 3, 4],
Object
)
Daily Log
var logger = require('tracer').dailyfile({
root: '.',
maxLogFiles: 10,
allLogsFileName: 'myAppName'
})
logger.log('hello')
logger.trace('hello', 'world')
logger.debug('hello %s', 'world', 123)
logger.info('hello %s %d', 'world', 123, { foo: 'bar' })
logger.warn('hello %s %d %j', 'world', 123, { foo: 'bar' })
logger.error(
'hello %s %d %j',
'world',
123,
{ foo: 'bar' },
[1, 2, 3, 4],
Object
)
dailylog will output all types log to diff files every day like log4j and if we provide allLogsFileName then all logs will be move to that file too.
Advanced Example
some helper package is need, so install -dev for running examples
npm install -dev tracer
Take a look at the examples directory for different examples.
Set logging level
the level option support index (number) or method name.
var logger = require('tracer').console({ level: 'warn' })
equal
var logger = require('tracer').console({ level: 4 })
var logger = require('tracer').console({ level: 'warn' })
logger.log('hello')
logger.trace('hello', 'world')
logger.debug('hello %s', 'world', 123)
logger.info('hello %s %d', 'world', 123, { foo: 'bar' })
logger.warn('hello %s %d %j', 'world', 123, { foo: 'bar' })
logger.error(
'hello %s %d %j',
'world',
123,
{ foo: 'bar' },
[1, 2, 3, 4],
Object
)
//$ node example/level.js
//2012-03-02T13:41:33.29Z <warn> level.js:6 (Object.<anonymous>) hello world 123 {"foo":"bar"}
//2012-03-02T13:41:33.30Z <error> level.js:7 (Object.<anonymous>) hello world 123 {"foo":"bar"} [ 1, 2, 3, 4 ] function Object() { [native code] }
//log,trace, debug and info level was not ouputed
Customize output format
format tag:
- timestamp: current time
- title: method name, default is 'log', 'trace', 'debug', 'info', 'warn', 'error','fatal'
- level: method level, default is 'log':0, 'trace':1, 'debug':2, 'info':3, 'warn':4, 'error':5, 'fatal':6
- message: printf message, support %s string, %d number, %j JSON and auto inspect
- file: file name
- line: line number
- pos: position
- path: file's path
- folder: file's parent folder
- method: method name of caller
- stack: call stack message
we use tinytim micro-template system to output log. see details tinytim. and, we use Date Format to format datetime.
var logger = require('tracer').console({
format: '{{timestamp}} <{{title}}> {{message}} (in {{file}}:{{line}})',
dateformat: 'HH:MM:ss.L'
})
Or, you can set special format for output method
var logger = require('tracer').colorConsole({
format: [
'{{timestamp}} <{{title}}> {{message}} (in {{file}}:{{line}})', //default format
{
error:
'{{timestamp}} <{{title}}> {{message}} (in {{file}}:{{line}})\nCall Stack:\n{{stack}}' // error format
}
],
dateformat: 'HH:MM:ss.L',
preprocess: function(data) {
data.title = data.title.toUpperCase()
}
})
the preprocess method is a choice for changing tag.
Customize output methods
var colors = require('colors')
var logger = require('tracer').colorConsole({
level: 'log1',
methods: ['log0', 'log1', 'log2', 'log3', 'log4', 'log5'],
filters: [colors.underline, colors.yellow]
})
logger.log0('hello')
logger.log1('hello', 'world')
logger.log2('hello %s', 'world', 123)
logger.log4('hello %s %d', 'world', 123)
logger.log5('hello %s %d', 'world', 123)
Customize filters
each filtes function was called. the function is synchronous and must be like
function f1(str) {
return str.toUpperCase()
}
About Colors.js
var colors = require('colors')
var logger = require('tracer').colorConsole({
filters: [
f1,
colors.underline,
colors.blue, //default filter
//the last item can be custom filter. here is "warn" and "error" filter
{
warn: colors.yellow,
error: [f1, colors.red, colors.bold]
}
]
})
the filter support key-function pair, example: color_console.js
{
filters : {
//log : colors.black,
trace : colors.magenta,
debug : colors.blue,
info : colors.green,
warn : colors.yellow,
error : [ colors.red, colors.bold ]
}
}
and the filters is an array, the last item can be custom filter. see example:filter.js
Log File Transport
var fs = require('fs')
var logger = require('tracer').console({
transport: function(data) {
console.log(data.output)
fs.appendFile('./file.log', data.rawoutput + '\n', err => {
if (err) throw err
})
}
})
logger.log('hello')
logger.trace('hello', 'world')
logger.debug('hello %s', 'world', 123)
logger.info('hello %s %d', 'world', 123, { foo: 'bar' })
logger.warn('hello %s %d %j', 'world', 123, { foo: 'bar' })
logger.error(
'hello %s %d %j',
'world',
123,
{ foo: 'bar' },
[1, 2, 3, 4],
Object
)
Stream Transport
var fs = require('fs')
var logger = require('tracer').console({
transport: function(data) {
console.log(data.output)
var stream = fs
.createWriteStream('./stream.log', {
flags: 'a',
encoding: 'utf8',
mode: 0666
})
.write(data.rawoutput + '\n')
}
})
logger.log('hello')
logger.trace('hello', 'world')
logger.debug('hello %s', 'world', 123)
logger.info('hello %s %d', 'world', 123, { foo: 'bar' })
logger.warn('hello %s %d %j', 'world', 123, { foo: 'bar' })
logger.error(
'hello %s %d %j',
'world',
123,
{ foo: 'bar' },
[1, 2, 3, 4],
Object
)
MongoDB Transport
var mongo = require('mongoskin')
var db = mongo.db('127.0.0.1:27017/test?auto_reconnect')
var log_conf = {
transport: function(data) {
console.log(data.output)
var loginfo = db.collection('loginfo')
loginfo.insert(data, function(err, log) {
if (err) {
console.error(err)
}
})
}
}
var logger = require('tracer').console(log_conf)
logger.log('hello')
logger.trace('hello', 'world')
logger.debug('hello %s', 'world', 123)
logger.info('hello %s %d', 'world', 123, { foo: 'bar' })
logger.warn('hello %s %d %j', 'world', 123, { foo: 'bar' })
logger.error(
'hello %s %d %j',
'world',
123,
{ foo: 'bar' },
[1, 2, 3, 4],
Object
)
console.log('\n\n\npress ctrl-c to exit')
Defining Multiple Transport
var fs = require('fs');
var logger = require('tracer').console({
transport: [
function (data) {
fs.appendFile('./file.log', data.rawoutput + '\n', (err) => {
if (err) throw err;
});
},
function(data) {
console.log(data.output);
}
]
});
logger.log('hello');
logger.trace('hello', 'world');
logger.debug('hello %s', 'world', 123);
logger.info('hello %s %d', 'world', 123, {foo: 'bar'});
logger.warn('hello %s %d %j', 'world', 123, {foo: 'bar'});
logger.error('hello %s %d %j', 'world', 123, {foo: 'bar'}, [1, 2, 3, 4], Object);
Define your logging helper
the work is like color_console.js
var colors = require('colors')
module.exports = function(conf) {
return require('./console')(
{
filters: {
//log : colors.black,
trace: colors.magenta,
debug: colors.blue,
info: colors.green,
warn: colors.yellow,
error: [colors.red, colors.bold]
}
},
conf
)
}
Customize output Object's properties
Related Skills
node-connect
347.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
