1 | # Browser API
|
2 |
|
3 | Pino is compatible with [`browserify`](http://npm.im/browserify) for browser side usage:
|
4 |
|
5 | This can be useful with isomorphic/universal JavaScript code.
|
6 |
|
7 | By 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 |
|
12 | Pino can be passed a `browser` object in the options object,
|
13 | which can have the following properties:
|
14 |
|
15 | ### `asObject` (Boolean)
|
16 |
|
17 | ```js
|
18 | const pino = require('pino')({browser: {asObject: true}})
|
19 | ```
|
20 |
|
21 | The `asObject` option will create a pino-like log object instead of
|
22 | passing all arguments to a console method, for instance:
|
23 |
|
24 | ```js
|
25 | pino.info('hi') // creates and logs {msg: 'hi', level: 30, time: <ts>}
|
26 | ```
|
27 |
|
28 | When `write` is set, `asObject` will always be `true`.
|
29 |
|
30 | ### `write` (Function | Object)
|
31 |
|
32 | Instead of passing log messages to `console.log` they can be passed to
|
33 | a supplied function.
|
34 |
|
35 | If `write` is set to a single function, all logging objects are passed
|
36 | to this function.
|
37 |
|
38 | ```js
|
39 | const pino = require('pino')({
|
40 | browser: {
|
41 | write: (o) => {
|
42 | // do something with o
|
43 | }
|
44 | }
|
45 | })
|
46 | ```
|
47 |
|
48 | If `write` is an object, it can have methods that correspond to the
|
49 | levels. When a message is logged at a given level, the corresponding
|
50 | method is called. If a method isn't present, the logging falls back
|
51 | to using the `console`.
|
52 |
|
53 |
|
54 | ```js
|
55 | const 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 |
|
71 | The serializers provided to `pino` are ignored by default in the browser, including
|
72 | the standard serializers provided with Pino. Since the default destination for log
|
73 | messages is the console, values such as `Error` objects are enhanced for inspection,
|
74 | which they otherwise wouldn't be if the Error serializer was enabled.
|
75 |
|
76 | We can turn all serializers on,
|
77 |
|
78 | ```js
|
79 | const pino = require('pino')({
|
80 | browser: {
|
81 | serialize: true
|
82 | }
|
83 | })
|
84 | ```
|
85 |
|
86 | Or we can selectively enable them via an array:
|
87 |
|
88 | ```js
|
89 | const 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
|
100 | pino.info({custom: 'a', another: 'b'})
|
101 | ```
|
102 |
|
103 | When `serialize` is `true` the standard error serializer is also enabled (see https://github.com/pinojs/pino/blob/master/docs/api.md#stdSerializers).
|
104 | This is a global serializer which will apply to any `Error` objects passed to the logger methods.
|
105 |
|
106 | If `serialize` is an array the standard error serializer is also automatically enabled, it can
|
107 | be explicitly disabled by including a string in the serialize array: `!stdSerializers.err`, like so:
|
108 |
|
109 | ```js
|
110 | const 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 |
|
121 | The `serialize` array also applies to any child logger serializers (see https://github.com/pinojs/pino/blob/master/docs/api.md#discussion-2
|
122 | for how to set child-bound serializers).
|
123 |
|
124 | Unlike server pino the serializers apply to every object passed to the logger method,
|
125 | if the `asObject` option is `true`, this results in the serializers applying to the
|
126 | first object (as in server pino).
|
127 |
|
128 | For more info on serializers see https://github.com/pinojs/pino/blob/master/docs/api.md#parameters.
|
129 |
|
130 | ### `transmit` (Object)
|
131 |
|
132 | An object with `send` and `level` properties.
|
133 |
|
134 | The `transmit.level` property specifies the minimum level (inclusive) of when the `send` function
|
135 | should 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 |
|
138 | The `transmit` object must have a `send` function which will be called after
|
139 | writing the log message. The `send` function is passed the level of the log
|
140 | message and a `logEvent` object.
|
141 |
|
142 | The `logEvent` object is a data structure representing a log message, it represents
|
143 | the arguments passed to a logger statement, the level
|
144 | at which they were logged and the hierarchy of child bindings.
|
145 |
|
146 | The `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 |
|
157 | The `ts` property is a unix epoch timestamp in milliseconds, the time is taken from the moment the
|
158 | logger method is called.
|
159 |
|
160 | The `messages` array is all arguments passed to logger method, (for instance `logger.info('a', 'b', 'c')`
|
161 | would result in `messages` array `['a', 'b', 'c']`).
|
162 |
|
163 | The `bindings` array represents each child logger (if any), and the relevant bindings.
|
164 | For instance given `logger.child({a: 1}).child({b: 2}).info({c: 3})`, the bindings array
|
165 | would hold `[{a: 1}, {b: 2}]` and the `messages` array would be `[{c: 3}]`. The `bindings`
|
166 | are ordered according to their position in the child logger hierarchy, with the lowest index
|
167 | being the top of the hierarchy.
|
168 |
|
169 | By default serializers are not applied to log output in the browser, but they will *always* be
|
170 | applied to `messages` and `bindings` in the `logEvent` object. This allows us to ensure a consistent
|
171 | format for all values between server and client.
|
172 |
|
173 | The `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
|
175 | labels differ from server side.
|
176 |
|
177 | The point of the `send` function is to remotely record log messages:
|
178 |
|
179 | ```js
|
180 | const 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 | ```
|