Transform

Extends stream.Transform

./source/transform.js

Transform. This is a helper for our transforms to be able to process the written log data more easily. All the need to do is extend this class and add their own format method.

Examples

class Pretty extends require('caterpillar').Transform {
format (entry) {
return require('util').inspect(entry, {colors: true})
}
}
require('caterpillar').create()
.pipe(Pretty.create())
.pipe(process.stdout)
.log('note', 'cool times', 5)

Static members

.create(args)

Alternative way of creating an instance of the class without having to use the new keyword. Useful when creating the class directly from require statements.

create(args)

./source/transform.js

Alternative way of creating an instance of the class without having to use the new keyword. Useful when creating the class directly from require statements.

Parameters

  • ... Any args :

Returns

Transform

Instance members

#constructor(args)

Construct our class and pass the arguments over to setConfig

constructor(args)

./source/transform.js

Construct our class and pass the arguments over to setConfig

Parameters

  • ... args :
#format(message)

Format the written data into whatever we want. Here is where our transformers work with the written data to enhance it.

format(message)

./source/transform.js

Format the written data into whatever we want. Here is where our transformers work with the written data to enhance it.

Parameters

Returns

Any
#getConfig

Get the current configuration object for this instance.

getConfig

./source/transform.js

Get the current configuration object for this instance.

Returns

Object
#getInitialConfig

Get the initial configuration option. Use this to add default/initial configuration to your class.

getInitialConfig

./source/transform.js

Get the initial configuration option. Use this to add default/initial configuration to your class.

Returns

Object
#pipe(child)

Pipe this data to some other writable stream. If the child stream also has a setConfig method, we will ensure the childs configuration is kept consistent with parents.

pipe(child)

./source/transform.js

Pipe this data to some other writable stream. If the child stream also has a setConfig method, we will ensure the childs configuration is kept consistent with parents.

Parameters

Returns

stream.Writable :

the result of the pipe operation

#setConfig(configs)

Apply the specified configurations to this instance's configuration via deep merging.

setConfig(configs)

./source/transform.js

Apply the specified configurations to this instance's configuration via deep merging.

Parameters

Returns

this

Examples

setConfig({a: 1}, {b: 2})
getConfig()  // {a: 1, b: 2}

Logger

Extends stream.PassThrough

./source/logger.js

Logger. This is what we write to. It extends from PassThrough and not transform. If you are piping / writing directly to the logger, make sure it corresponds to the correct entry format (as described in log).

Examples

Creation

// Via class
const Logger = require('caterpillar').Logger
const logger = new Logger()
// Via create helper
const logger = Logger.create()
// Via create alias
const logger = require('caterpillar').create()

Static members

.create(args)

Alternative way of creating an instance of the class without having to use the new keyword. Useful when creating the class directly from require statements.

create(args)

./source/logger.js

Alternative way of creating an instance of the class without having to use the new keyword. Useful when creating the class directly from require statements.

Parameters

  • ... Any args :

Returns

Logger

Instance members

#constructor(args)

Construct our class and pass the arguments over to setConfig

constructor(args)

./source/logger.js

Construct our class and pass the arguments over to setConfig

Parameters

  • ... args :
#getConfig

Get the current configuration object for this instance.

getConfig

./source/logger.js

Get the current configuration object for this instance.

Returns

Object
#getLevelInfo(level)

Receive either the level name or number and return the combination.

getLevelInfo(level)

./source/logger.js

Receive either the level name or number and return the combination.

Parameters

Returns

Object

Throws

  • Error :

    will throw an error if returned empty handed

Examples

Input

logger.getLevelInfo('note')

Result

{
"levelNumber": 5,
"levelName": "notice"
}
#getLevelName(number)

Receive a level number and return the level name

getLevelName(number)

./source/logger.js

Receive a level number and return the level name

Parameters

Returns

string

Throws

  • Error :

    will throw an error if returned empty handed

#getLevelNumber(name)

Receive a level name and return the level number

getLevelNumber(name)

./source/logger.js

Receive a level name and return the level number

Parameters

Returns

number

Throws

  • Error :

    will throw an error if no result was found

#getLineInfo

The current line info of whatever called this.

getLineInfo

./source/logger.js

The current line info of whatever called this.

Returns

Object

Throws

  • Error :

    will throw an error if returned empty handed

Examples

Input

logger.getLineInfo()

Result

{
"line": "60",
"method": "Object.<anonymous>",
"file": "/Users/balupton/some-project/calling-file.js"
}
#log(args)

Log the arguments into the logger stream as formatted data with debugging information.

log(args)

./source/logger.js

Log the arguments into the logger stream as formatted data with debugging information.

Parameters

  • ... Any args :

Returns

Object

Examples

Inputs

logger.log('note', 'this is working swell')
logger.log('this', 'worked', 'swell')

Results

{
"args": ["this is working swell"],
"date": "2013-04-25T10:18:25.722Z",
"levelNumber": 5,
"levelName": "notice",
"line": "59",
"method": "Object.<anonymous>",
"file": "/Users/balupton/some-project/calling-file.js"
}
{
"args": ["this", "worked", "well"],
"date": "2013-04-25T10:18:26.539Z",
"levelNumber": 6,
"levelName": "info",
"line": "60",
"method": "Object.<anonymous>",
"file": "/Users/balupton/some-project/calling-file.js"
}
#pipe(child)

Pipe this data to some other writable stream. If the child stream also has a setConfig method, we will ensure the childs configuration is kept consistent with parents.

pipe(child)

./source/logger.js

Pipe this data to some other writable stream. If the child stream also has a setConfig method, we will ensure the childs configuration is kept consistent with parents.

Parameters

Returns

stream.Writable :

the result of the pipe operation

#setConfig(configs)

Apply the specified configurations to this instance's configuration via deep merging.

setConfig(configs)

./source/logger.js

Apply the specified configurations to this instance's configuration via deep merging.

Parameters

Returns

this

Examples

setConfig({a: 1}, {b: 2})
getConfig()  // {a: 1, b: 2}