Skip to content

Commit

Permalink
Merge pull request #44 from mcollina/integration-points
Browse files Browse the repository at this point in the history
on('level-change') and minor tweaks
  • Loading branch information
David Mark Clements committed May 9, 2016
2 parents cf714b3 + 56c3c74 commit 543668b
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 18 deletions.
48 changes: 38 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,16 @@ Into this:
* <a href="#debug"><code>logger.<b>debug()</b></code></a>
* <a href="#trace"><code>logger.<b>trace()</b></code></a>
* <a href="#levelVal"><code>logger.<b>levelVal</b></code></a>
* <a href="#levelValues"><code>pino.levels.<b>values</b></code></a>
* <a href="#levelLabels"><code>pino.levels.<b>labels</b></code></a>
* <a href="#level-change"><code>logger.on(<b>'level-change'</b>, fn)</code></a>
* <a href="#levelValues"><code>logger.levels.<b>values</b></code> & <code>pino.levels.<b>values</b></code></a>
* <a href="#levelLabels"><code>logger.levels.<b>labels</b></code> & <code>pino.levels.<b>labels</b></code></a>
* <a href="#log_version"><code>pino.<b>LOG_VERSION</b></code> & <code>logger.<b>LOG_VERSION</b></code></a>
* <a href="#reqSerializer"><code>pino.stdSerializers.<b>req</b></code></a>
* <a href="#resSerializer"><code>pino.stdSerializers.<b>res</b></code></a>
* <a href="#errSerializer"><code>pino.stdSerializers.<b>err</b></code></a>
* <a href="#pretty"><code>pino.<b>pretty()</b></code></a>


<a name="constructor"></a>
### pino([stream], [opts])

Expand Down Expand Up @@ -339,8 +342,27 @@ If more args follows `msg`, these will be used to format `msg` using

Returns the integer value for the logger instance's logging level.

<a name="level-change"></a>
### logger.on('level-change', fn)

Registers a listener function that is triggered when the level is changed.

The listener is passed four arguments: `levelLabel`, `levelValue`, `previousLevelLabel`, `previousLevelValue`.

**Note:** When browserified, this functionality will only be available if the `events` module has been required else where (e.g. if you're using streams in the browser). This allows for a trade-off between bundle size and functionality.

```js
var listener = function (lvl, val, prevLvl, prevVal) {
console.log(lvl, val, prevLvl, prevVal)
}
logger.on('level-change', listener)
logger.level = 'trace' // trigger console message
logger.removeListener('level-change', listener)
logger.level = 'info' // no message, since listener was removed
```

<a name="levelValues"></a>
### pino.levels.values
### logger.levels.values & pino.levels.values

Returns the mappings of level names to their respective internal number
representation. For example:
Expand All @@ -350,7 +372,7 @@ pino.levels.values.error === 50 // true
```

<a name="levelLabels"></a>
### pino.levels.labels
### logger.levels.labels & pino.levels.labels

Returns the mappings of level internal level numbers to their string
representations. For example:
Expand All @@ -359,6 +381,12 @@ representations. For example:
pino.levels.labels[50] === 'error' // true
```

<a name="log_version"></a>
### logger.LOG_VERSION & pino.LOG_VERSION

Read only. Holds the current log format version (as output in the `v` property of each log record).


<a name="reqSerializer"></a>
### pino.stdSerializers.req

Expand Down Expand Up @@ -528,18 +556,18 @@ server.register(require('hapi-pino'), (err) => {

See the [hapi-pino readme](http://npm.im/hapi-pino) for more info.

<a name="restifiy"></a>
## How to use Pino with Restifiy
<a name="restify"></a>
## How to use Pino with Restify

We've got you covered:

```sh
npm install --save restifiy-pino-logger
npm install --save restify-pino-logger
```

```js
var server = require('restifiy').createServer({name: 'server'})
var pino = require('restifiy-pino-logger')()
var server = require('restify').createServer({name: 'server'})
var pino = require('restify-pino-logger')()

server.use(pino)

Expand All @@ -551,7 +579,7 @@ server.get('/', function (req, res) {
server.listen(3000)
```

See the [restifiy-pino-logger readme](http://npm.im/restifiy-pino-logger) for more info.
See the [restify-pino-logger readme](http://npm.im/restify-pino-logger) for more info.

<a name="koa"></a>
## How to use Pino with koa
Expand Down
40 changes: 32 additions & 8 deletions pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ function pino (opts, stream) {
}

Object.defineProperty(pino, 'levels', {
get: function getLevels () {
return {
values: levels,
labels: nums
}
}
value: {
values: copy({}, levels),
labels: copy({}, nums)
},
enumerable: true
})

function Pino (level, stream, serializers, stringify, end, name, hostname, slowtime, chindings, cache, formatOpts) {
Expand All @@ -93,19 +92,30 @@ function Pino (level, stream, serializers, stringify, end, name, hostname, slowt
this._setLevel(level)
}

if (require('eve' + 'nts')) {
Pino.prototype = new (require('eve' + 'nts').EventEmitter)()
}

Pino.prototype.fatal = genLog(levels.fatal)
Pino.prototype.error = genLog(levels.error)
Pino.prototype.warn = genLog(levels.warn)
Pino.prototype.info = genLog(levels.info)
Pino.prototype.debug = genLog(levels.debug)
Pino.prototype.trace = genLog(levels.trace)

Object.defineProperty(Pino.prototype, 'levels', {value: pino.levels})

Object.defineProperty(Pino.prototype, 'levelVal', {
get: function getLevelVal () {
return this._levelVal
},
set: function setLevelVal (num) {
if (typeof num === 'string') { return this._setLevel(num) }

if (this.emit) {
this.emit('level-change', nums[num], num, nums[this._levelVal], this._levelVal)
}

this._levelVal = num

for (var key in levels) {
Expand Down Expand Up @@ -136,6 +146,12 @@ Object.defineProperty(Pino.prototype, 'level', {
set: Pino.prototype._setLevel
})

Object.defineProperty(
Pino.prototype,
'LOG_VERSION',
{value: LOG_VERSION}
)

Pino.prototype.asJson = function asJson (obj, msg, num) {
if (!msg && obj instanceof Error) {
msg = obj.message
Expand Down Expand Up @@ -278,6 +294,11 @@ function genLog (z) {
}
}

function copy (a, b) {
for (var k in b) { a[k] = b[k] }
return a
}

function onExit (fn) {
var oneFn = once(fn)
process.on('beforeExit', handle('beforeExit'))
Expand All @@ -301,11 +322,14 @@ function onExit (fn) {
}

module.exports = pino

module.exports.stdSerializers = {
req: asReqValue,
res: asResValue,
err: asErrValue
}

module.exports.pretty = require('./pretty')
Object.defineProperty(
module.exports,
'LOG_VERSION',
{value: LOG_VERSION, enumerable: true}
)
34 changes: 34 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,37 @@ test('pino transform', function (t) {

instance.info('hello world')
})

test('level-change event', function (t) {
var instance = pino()
var handle = function (lvl, val, prevLvl, prevVal) {
t.is(lvl, 'trace')
t.is(val, 10)
t.is(prevLvl, 'info')
t.is(prevVal, 30)
}
instance.on('level-change', handle)
instance.level = 'trace'
instance.removeListener('level-change', handle)
instance.level = 'info'

var count = 0

var l1 = function () { count += 1 }
var l2 = function () { count += 1 }
var l3 = function () { count += 1 }
instance.on('level-change', l1)
instance.on('level-change', l2)
instance.on('level-change', l3)

instance.level = 'trace'
instance.removeListener('level-change', l3)
instance.level = 'fatal'
instance.removeListener('level-change', l1)
instance.level = 'debug'
instance.removeListener('level-change', l2)
instance.level = 'info'

t.is(count, 6)
t.end()
})

0 comments on commit 543668b

Please sign in to comment.