Skip to content

Commit

Permalink
added lastTime meta parameter (pinojs#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina authored Apr 4, 2018
1 parent fc4c83b commit 62070d4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,8 @@ should be set:
* the last logging level as `stream.lastLevel`
* the last logging message as `stream.lastMsg`
* the last logging object as `stream.lastObj`
* the last time as `stream.lastTime`, which will be the partial string returned
by the time function.
* the last logger instance as `stream.lastLogger` (to support child
loggers)

Expand Down
8 changes: 5 additions & 3 deletions pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ Object.defineProperty(
{value: LOG_VERSION}
)

function asJson (obj, msg, num) {
function asJson (obj, msg, num, time) {
// to catch both null and undefined
var hasObj = obj !== undefined && obj !== null
var objError = hasObj && obj instanceof Error
msg = !msg && objError ? obj.message : msg || undefined
var data = this._lscache[num] + this.time()
var data = this._lscache[num] + time
if (msg !== undefined) {
// JSON.stringify is safe here
data += this.messageKeyString + JSON.stringify('' + msg)
Expand Down Expand Up @@ -208,13 +208,15 @@ Object.defineProperty(pinoPrototype, 'child', {
})

function pinoWrite (obj, msg, num) {
var s = this.asJson(obj, msg, num)
var t = this.time()
var s = this.asJson(obj, msg, num, t)
var stream = this.stream
if (this.cache === null) {
if (stream[needsMetadata]) {
stream.lastLevel = num
stream.lastMsg = msg
stream.lastObj = obj
stream.lastTime = t.slice(8)
stream.lastLogger = this // for child loggers
}
stream.write(flatstr(s))
Expand Down
4 changes: 3 additions & 1 deletion test/metadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var pid = process.pid
var hostname = os.hostname()

test('metadata works', function (t) {
t.plan(6)
t.plan(7)
var dest = sink(function (chunk, enc, cb) {
t.ok(new Date(chunk.time) <= new Date(), 'time is greater than Date.now()')
delete chunk.time
Expand All @@ -22,12 +22,14 @@ test('metadata works', function (t) {
v: 1
})
})
var now = Date.now()
var instance = pino({}, {
[Symbol.for('needsMetadata')]: true,
write: function (chunk) {
t.equal(instance, this.lastLogger)
t.equal(30, this.lastLevel)
t.equal('a msg', this.lastMsg)
t.ok(Number(this.lastTime) >= now)
t.deepEqual({ hello: 'world' }, this.lastObj)
dest.write(chunk)
}
Expand Down

0 comments on commit 62070d4

Please sign in to comment.