How to use pino

Comprehensive pino code examples:

How to use pino.levels:

67
68
69
70
71
72
73
74
75
76
77
// The limitation of this wrapping is that structured data fields are *not*
// passed on to the custom logger. I.e. this is a fallback mechanism.
class SafePinoDestWrapper {
  constructor (customLogger) {
    this.customLogger = customLogger
    this.logFnNameFromLastLevel = pino.levels.labels
    this[Symbol.for('pino.metadata')] = true
  }


  write (s) {

How to use pino.child:

144
145
146
147
148
149
150
151
152
153
    destination: 1,
    all: true,
    translateTime: true,
  }
},
logger: pino.child({name: loggerConfig.name}),
genReqId: function (req, res) {
  const existingID = req.id ?? req.headers["x-request-id"];
  if (existingID) return existingID;
  let id = randomUUID();

How to use pino.warn:

59
60
61
62
63
64
65
66
67
68
if (dir in require.cache) {
  delete require.cache[dir];
  if (fs.existsSync(dir))
    logger.info(`re - require plugin '${filename}'`);
  else {
    logger.warn(`deleted plugin '${filename}'`);
    return delete global.plugins[filename];
  }
} else logger.info(`requiring new plugin '${filename}'`);
let err = syntaxerror(fs.readFileSync(dir), filename);

How to use pino.default:

2
3
4
5
6
7
8
9
10
11
12
13
const pino = require("pino");


const webhookLogger = process.env.ERROR_LOGS ? new WebhookClient({ url: process.env.ERROR_LOGS }) : undefined;


const today = new Date();
const pinoLogger = pino.default(
  {
    level: "debug",
  },
  pino.multistream([

How to use pino.transport:

20
21
22
23
24
25
26
27
28
29
    level: config.file.level,
    options: { destination: filePath, mkdir: true }
  });
}

const transport = pino.transport({
  targets,
  options: {
    colorize: config.colorize,
  },

How to use pino.symbols:

0
1
2
3
4
5
6
7
8
9
10
11
'use strict';


const Hoek = require('@hapi/hoek');
const pino = require('pino');
const { stdSerializers } = pino;
const { serializersSym } = pino.symbols;
const nullLogger = require('abstract-logging');


const levels = ['trace', 'debug', 'info', 'warn', 'error'];
const levelTags = {

How to use pino.debug:

258
259
260
261
262
263
264
265
266
267
lookup(serviceName, {family: 4, all: true}, (err, addresses) => {
  if (err) {
    logger.error({err}, `Error looking up ${serviceName}`);
    return;
  }
  logger.debug({addresses, rtpServers}, `dns lookup for ${serviceName} returned`);
  const addrs = addresses.map((a) => a.address);
  if (!equalsIgnoreOrder(addrs, rtpServers)) {
    rtpServers.length = 0;
    Array.prototype.push.apply(rtpServers, addrs);

How to use pino.multistream:

6
7
8
9
10
11
12
13
14
15
const today = new Date();
const pinoLogger = pino.default(
  {
    level: "debug",
  },
  pino.multistream([
    {
      level: "info",
      stream: pino.transport({
        target: "pino-pretty",

How to use pino.pino:

139
140
141
142
143
144
145
146
147
148
'use strict'

const { connect } = require('@platformatic/sql-mapper')
const { pino } = require('pino')
const pretty = require('pino-pretty')
const logger = pino(pretty())

async function main() {
  const pgConnectionString = 'postgres://postgres:postgres@127.0.0.1/postgres'
  const mapper = await connect({

How to use pino.stdTimeFunctions:

177
178
179
180
181
182
183
184
185
186
				 */
				res.headersSent = true;
				return pino.stdSerializers.res(res);
			},
		},
		timestamp: () => pino.stdTimeFunctions.isoTime(),
	},
	ignoreTrailingSlash: true,
},
cors: {

How to use pino.destination:

35
36
37
38
39
40
41
42
43
    destination = config.log.destination || undefined
    pinoConfig.level = config.log.level
    pinoConfig.prettyPrint = !destination ? prettyPrint : false
  }

  return pino(pinoConfig, pino.destination(destination))
}

module.exports = createLogger

How to use pino.error:

31
32
33
34
35
36
37
38
39
  })
}

app.listen(3000, (err) => {
  if (err) {
    logger.error('The app could not start')
  }
  logger.info('The app is listening on 3000')
})

How to use pino.stdSerializers:

23
24
25
26
27
28
29
30
31
    channel: serializers.channel,
    role: serializers.channel,
    user: serializers.user,
    message: serializers.message,
    feed: serializers.feed,
    error: pino.stdSerializers.err
  },
  enabled: process.env.NODE_ENV !== 'test'
}

How to use pino.info:

5
6
7
8
9
10
11
12
13
14
const pino = require('pino')({
  extreme: true
})

function handle (req, res) {
  pino.info(req)
  res.end('hello world')
}

server.listen(3000)