-
Notifications
You must be signed in to change notification settings - Fork 45
4 · API : Console2
##Console2(opt)
require('../scribe')(); //loads Scribe (with basic loggers)
//create a local (for the module) console
var console = process.console;
console.log("Hello");
//you can use printf-like format
console.log("A string %s and a number %d", "hello", "123");
//Time
console.time().log("Print the full time");
console.date().log("Just print the date");
//Tags
console.tag("My Tag").log("Add a tag");
console.tag("Tag1", 'Tag2', 123).log("Or multiple tag");
console.tag({msg : 'my-tag', colors : ['red', 'inverse']}).log("Use colors.js colors");
//File and line number
console.file().log("Print the file and the line of the call");
//Object
console.log({just : 'an object'});
//Combos !
console.log(
"Print many things",
{ key1 : "val 1", key2 : { a: "b", c : []}, key3 : [1234]},
['an array'],
"A String"
);
console.tag("Combo!").time().file().log("A combo");
//Chaining loggers
console.info('Some info').tag('A tag').log('Some logs').warning('Some warning');
Params : (all optional)
-
opt.colors
: Array|String. Default colors output for all loggers. Default ['cyan']. -
opt.tagsColors
: Array|String. Default colors output for tags. Default undefined. -
opt.timeColors
: Array|String. Default colors output for time. Default undefined. -
opt.datecolors
: Array|String. Default colors output for date. Default undefined. -
opt.fileColors
: Array|String. Default colors output for filename. Default undefined. -
opt.lineColors
: Array|String. Default colors output for line number. Default undefined. -
opt.alwaysTags
: Boolean. Always print tags (even without tag() ). Default false. -
opt.alwaysLocation
: Boolean. Always print location (even without file() ). Default false. -
opt.alwaysTime
: Boolean. Always print time (even without time() ). Default false. -
opt.alwaysDate
: Boolean. Always print date (even without date() ). Default false. -
opt.logInConsole
: Boolean. Should all loggers print to console by default ? Default true. -
opt.contextMediumSize
: Int. Medium size of the context part of a log message. Used when calculating indent. Default to 45. -
opt.spaceSize
: Int. Space between context part and log part. Default to 4. -
opt.defaultTags
: Array of Strings or Objects. Default tags to print with each log.
Colors must be colors.js compatible.
Example :
var myConfigConsole = scribe.console({
console : { //pass here Console2 options
colors : ['rainbow', 'inverse'],
tagsColors : ['yellow', 'underline']
}
);
See :
###Console2.time() Tell the logger to log the time (ISO format).
###Console2.date()
Tell the logger to log the date (using toDateString()
).
###Console2.tag(*args), Console2.t(*args)
Params:
An infinite number of tag
where tag
is :
- a string
- or an object :
-
msg
: String. The tag -
colors
: Array|String. Colors.js colors
-
Examples :
console.tag('My tag').log('Something');
console.tag("A tag", {msg : "A another tag", colors : ['red', 'inverse']}).log('Something else').
###Console2.file(), Console2.f() Tell the logger to log the filename and the line of the log.
###Console2.addLogger(name, colors, opt)
Params :
-
name
: String. Name of the logger. Next you call the logger withconsole.mylogger()
-
colors
: Array|String. Optional. Colors.js colors. -
opt
: Optional. Options for this logger only. See Console2 options.-
tagsColors
: Array|String. Default colors output for tags. Default undefined. -
timeColors
: Array|String. Default colors output for time. Default undefined. -
datecolors
: Array|String. Default colors output for date. Default undefined. -
fileColors
: Array|String. Default colors output for filename. Default undefined. -
lineColors
: Array|String. Default colors output for line number. Default undefined. -
alwaysTags
: Boolean. Always print tags (even without tag() ). Default false. -
alwaysLocation
: Boolean. Always print location (even without file() ). Default false. -
alwaysTime
: Boolean. Always print time (even without time() ). Default false. -
alwaysDate
: Boolean. Always print date (even without date() ). Default false. -
logInConsole
: Boolean. Should all loggers print to console by default ? Default true. -
defaultTags
: Array of Strings or Objects. Default tags to print with each log.
-
Example :
console.addLogger('fun', ['rainbow', 'inverse'], {
timeColors : ['gray', 'underline']
})
console.fun('something');
See Console2.buildArgs() to know what you can pass to a logger
###Console2.buildArgs(log)
Do not use this unless you want to change how args you pass to a logger are printed. See code.
Params :
-
log
: A log object constructed by the logger. See code.-
args
. Logger'sarguments
- ...
-
Return : String. The args part of the message.
- If all args ar string of number, will use
util.format.apply()
as the original nodejs console does. - If object or array are present, will print the args line by line and object/arg on multilines.
###Console2.buildContext(log, opt)
Do not use this unless you want to change how context (tags/location/time/date/...) you pass to a logger are printed. See code.
Params :
-
log
: A log object constructed by the logger. See code. -
opt
: wether to print tags/location/time/date or not
Return :
-
result
: String. The context part of the log. -
lenght
: Int. The human-readable length of the result. Ie. without colors console caracters.
###Console2.[your logger](*args)
Will print *args
to the console, with context if there is.
See Console2.buildArgs()
.
Params : anything, printf format, etc.
Return : the Console2 instance.
Example :
console.addLogger('demo');
console.tag('Simple').demo('A ', 'message');
console.tag('Printf format').demo("%s:%s", 10, "23");
console.tag('Multiple args and big context').time().file().demo(
"A message", //string
123, //number
[1, 2], //array
{ foo : 'bar' } //object
);
console.log("Demo").time().warning("Test").info("some info");
###Console2 events
Each Console2 instance emits events :
-
newLogger
when a new logger is created/** * name : name of the logger * opt : Console2 options */ console.on('newLogger', function (name, opt) { //welcome, new logger ! });
-
new
when a logger logs something/** * log : the log object. var log = { type : 'loggerName', show : { tags : Boolean, location : Boolean, time : Boolean, date : Boolean }, context : { tags : [ tags ], file : Boolean, time : Date.now(), location : { filename : 'main.js', line : 12 } }, args : arguments, //loggers arguments opt : opt, //loggers options for addLogger() contextString : '[..] ...' , argsString : 'My message', message : '[..] ... My message' }; * * loggerName : the logger name */ console.on('new', function (log, loggerName) { //Oh! `loggerName` has logged something });
-
[loggerName]
when [loggerName] logs something. Fired whithnew
/** * log : same as above */ console.on('log', function (log) { //someone has logged something with logger "log" });
If loggerName is
'error'
, Scribe replace error with'errorEvent'
as NodeJSeventEmitter
will raise an Error if there is no listeners.
Copyright © 2014-2015 Scribe.js