/
hapi-pino-with-pino-pretty.js
78 lines (73 loc) 路 2.04 KB
/
hapi-pino-with-pino-pretty.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'use strict'
// see docs https://github.com/pinojs/pino/blob/master/docs/pretty.md *prod
// see docs https://github.com/pinojs/pino-pretty *dev
const Hapi = require('@hapi/hapi')
// manage logs
const Pino = require('..')
// const sonic = require('sonic-boom')
// const Path = require('path')
// const { join } = require('path')
const getResponse = [
{ string: 'string1', number: 1, boolean: true },
{ string: 'string2', number: 2, boolean: false }
]
async function start () {
// Create a server with a host and port
const server = Hapi.server({
host: 'localhost',
port: 3000,
debug: false // disable Hapi debug console logging
})
// Add the route
server.route({
method: 'GET',
path: '/items',
handler: async function (request, h) {
// test sonicBoob library works
// const sonic = new SonicBoom({
// dest: './pino-logs/node_trace.1.log',
// append: true,
// mkdir: true
// });
return h.response(getResponse)
}
})
// const tmpDir = Path.join(__dirname, '.tmp_' + Date.now())
// const destination = join(tmpDir, 'output')
await server.register({
plugin: Pino,
options: {
logPayload: true,
mergeHapiLogData: true,
ignorePaths: ['/alive.txt', '/private'],
ignoreFunc: (options, request) => request.path.startsWith('/private'),
transport: {
target: 'pino-pretty',
options: {
colorize: true,
minimumLevel: 'info',
levelFirst: true,
messageFormat: true,
timestampKey: 'time',
translateTime: true,
singleLine: true,
mkdir: true,
append: true // the next line is breaking the app
// destination: new sonic({
// dest: destination || 1,
// append: true,
// mkdir: true,
// sync: true
// })
}
}
}
})
await server.start()
server.log(['info'], `server running: ${server.info.uri}/items`)
return server
}
start().catch((err) => {
console.log(err)
process.exit(1)
})