Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a basic formatters option to the browser pino #1898

Merged
merged 2 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ function pino (opts) {
transmit,
serialize,
asObject: opts.browser.asObject,
formatters: opts.browser.formatters,
levels,
timestamp: getTimeFunction(opts)
}
Expand Down Expand Up @@ -299,8 +300,9 @@ function createWrap (self, opts, rootLogger, level) {
if (opts.serialize && !opts.asObject) {
applySerializers(args, this._serialize, this.serializers, this._stdErrSerialize)
}
if (opts.asObject) write.call(proto, asObject(this, level, args, ts))
else write.apply(proto, args)
if (opts.asObject || opts.formatters) {
write.call(proto, asObject(this, level, args, ts, opts.formatters))
} else write.apply(proto, args)

if (opts.transmit) {
const transmitLevel = opts.transmit.level || self._level
Expand All @@ -321,15 +323,23 @@ function createWrap (self, opts, rootLogger, level) {
})(self[baseLogFunctionSymbol][level])
}

function asObject (logger, level, args, ts) {
function asObject (logger, level, args, ts, formatters = {}) {
const {
level: levelFormatter
} = formatters
if (logger._serialize) applySerializers(args, logger._serialize, logger.serializers, logger._stdErrSerialize)
const argsCloned = args.slice()
let msg = argsCloned[0]
const o = {}
if (ts) {
o.time = ts
}
o.level = logger.levels.values[level]
if (levelFormatter) {
const formattedLevel = levelFormatter(level, logger.levels.values[level])
Object.assign(o, formattedLevel)
} else {
o.level = logger.levels.values[level]
}
let lvl = (logger._childLevel | 0) + 1
if (lvl < 1) lvl = 1
// deliberate, catching objects, arrays
Expand Down
19 changes: 19 additions & 0 deletions docs/browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ pino.info('hi') // creates and logs {msg: 'hi', level: 30, time: <ts>}

When `write` is set, `asObject` will always be `true`.

### `formatters` (Object)

An object containing functions for formatting the shape of the log lines. When provided, it enables the logger to produce a pino-like log object with customized formatting. Currently, it supports formatting for the `level` object only.

##### `level`

Changes the shape of the log level. The default shape is `{ level: number }`.
The function takes two arguments, the label of the level (e.g. `'info'`)
and the numeric value (e.g. `30`).

```js
const formatters = {
level (label, number) {
return { level: number }
}
}
```


### `write` (Function | Object)

Instead of passing log messages to `console.log` they can be passed to
Expand Down
23 changes: 23 additions & 0 deletions test/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,29 @@ test('opts.browser.asObject logs pino-like object to console', ({ end, ok, is })
end()
})

test('opts.browser.formatters logs pino-like object to console', ({ end, ok, is }) => {
const info = console.info
console.info = function (o) {
is(o.level, 30)
is(o.label, 'info')
is(o.msg, 'test')
ok(o.time)
console.info = info
}
const instance = require('../browser')({
browser: {
formatters: {
level (label, number) {
return { label, level: number }
}
}
}
})

instance.info('test')
end()
})

test('opts.browser.write func log single string', ({ end, ok, is }) => {
const instance = pino({
browser: {
Expand Down
Loading