UNPKG

6.34 kBMarkdownView Raw
1# Browser API
2
3Pino is compatible with [`browserify`](http://npm.im/browserify) for browser side usage:
4
5This can be useful with isomorphic/universal JavaScript code.
6
7By default, in the browser,
8`pino` uses corresponding [Log4j](https://en.wikipedia.org/wiki/Log4j) `console` methods (`console.error`, `console.warn`, `console.info`, `console.debug`, `console.trace`) and uses `console.error` for any `fatal` level logs.
9
10## Options
11
12Pino can be passed a `browser` object in the options object,
13which can have the following properties:
14
15### `asObject` (Boolean)
16
17```js
18const pino = require('pino')({browser: {asObject: true}})
19```
20
21The `asObject` option will create a pino-like log object instead of
22passing all arguments to a console method, for instance:
23
24```js
25pino.info('hi') // creates and logs {msg: 'hi', level: 30, time: <ts>}
26```
27
28When `write` is set, `asObject` will always be `true`.
29
30### `write` (Function | Object)
31
32Instead of passing log messages to `console.log` they can be passed to
33a supplied function.
34
35If `write` is set to a single function, all logging objects are passed
36to this function.
37
38```js
39const pino = require('pino')({
40 browser: {
41 write: (o) => {
42 // do something with o
43 }
44 }
45})
46```
47
48If `write` is an object, it can have methods that correspond to the
49levels. When a message is logged at a given level, the corresponding
50method is called. If a method isn't present, the logging falls back
51to using the `console`.
52
53
54```js
55const pino = require('pino')({
56 browser: {
57 write: {
58 info: function (o) {
59 //process info log object
60 },
61 error: function (o) {
62 //process error log object
63 }
64 }
65 }
66})
67```
68
69### `serialize`: (Boolean | Array)
70
71The serializers provided to `pino` are ignored by default in the browser, including
72the standard serializers provided with Pino. Since the default destination for log
73messages is the console, values such as `Error` objects are enhanced for inspection,
74which they otherwise wouldn't be if the Error serializer was enabled.
75
76We can turn all serializers on,
77
78```js
79const pino = require('pino')({
80 browser: {
81 serialize: true
82 }
83})
84```
85
86Or we can selectively enable them via an array:
87
88```js
89const pino = require('pino')({
90 serializers: {
91 custom: myCustomSerializer,
92 another: anotherSerializer
93 },
94 browser: {
95 serialize: ['custom']
96 }
97})
98// following will apply myCustomSerializer to the custom property,
99// but will not apply anotherSerializer to another key
100pino.info({custom: 'a', another: 'b'})
101```
102
103When `serialize` is `true` the standard error serializer is also enabled (see https://github.com/pinojs/pino/blob/master/docs/api.md#stdSerializers).
104This is a global serializer which will apply to any `Error` objects passed to the logger methods.
105
106If `serialize` is an array the standard error serializer is also automatically enabled, it can
107be explicitly disabled by including a string in the serialize array: `!stdSerializers.err`, like so:
108
109```js
110const pino = require('pino')({
111 serializers: {
112 custom: myCustomSerializer,
113 another: anotherSerializer
114 },
115 browser: {
116 serialize: ['!stdSerializers.err', 'custom'] //will not serialize Errors, will serialize `custom` keys
117 }
118})
119```
120
121The `serialize` array also applies to any child logger serializers (see https://github.com/pinojs/pino/blob/master/docs/api.md#discussion-2
122for how to set child-bound serializers).
123
124Unlike server pino the serializers apply to every object passed to the logger method,
125if the `asObject` option is `true`, this results in the serializers applying to the
126first object (as in server pino).
127
128For more info on serializers see https://github.com/pinojs/pino/blob/master/docs/api.md#parameters.
129
130### `transmit` (Object)
131
132An object with `send` and `level` properties.
133
134The `transmit.level` property specifies the minimum level (inclusive) of when the `send` function
135should be called, if not supplied the `send` function be called based on the main logging `level`
136(set via `options.level`, defaulting to `info`).
137
138The `transmit` object must have a `send` function which will be called after
139writing the log message. The `send` function is passed the level of the log
140message and a `logEvent` object.
141
142The `logEvent` object is a data structure representing a log message, it represents
143the arguments passed to a logger statement, the level
144at which they were logged and the hierarchy of child bindings.
145
146The `logEvent` format is structured like so:
147
148```js
149{
150 ts = Number,
151 messages = Array,
152 bindings = Array,
153 level: { label = String, value = Number}
154}
155```
156
157The `ts` property is a unix epoch timestamp in milliseconds, the time is taken from the moment the
158logger method is called.
159
160The `messages` array is all arguments passed to logger method, (for instance `logger.info('a', 'b', 'c')`
161would result in `messages` array `['a', 'b', 'c']`).
162
163The `bindings` array represents each child logger (if any), and the relevant bindings.
164For instance given `logger.child({a: 1}).child({b: 2}).info({c: 3})`, the bindings array
165would hold `[{a: 1}, {b: 2}]` and the `messages` array would be `[{c: 3}]`. The `bindings`
166are ordered according to their position in the child logger hierarchy, with the lowest index
167being the top of the hierarchy.
168
169By default serializers are not applied to log output in the browser, but they will *always* be
170applied to `messages` and `bindings` in the `logEvent` object. This allows us to ensure a consistent
171format for all values between server and client.
172
173The `level` holds the label (for instance `info`), and the corresponding numerical value
174(for instance `30`). This could be important in cases where client side level values and
175labels differ from server side.
176
177The point of the `send` function is to remotely record log messages:
178
179```js
180const pino = require('pino')({
181 browser: {
182 transmit: {
183 level: 'warn',
184 send: function (level, logEvent) {
185 if (level === 'warn') {
186 // maybe send the logEvent to a separate endpoint
187 // or maybe analyse the messages further before sending
188 }
189 // we could also use the `logEvent.level.value` property to determine
190 // numerical value
191 if (logEvent.level.value >= 50) { // covers error and fatal
192
193 // send the logEvent somewhere
194 }
195 }
196 }
197 }
198})
199```