-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
custom_timestamp.ts
53 lines (43 loc) · 1.28 KB
/
custom_timestamp.ts
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
/**
* This example shows how to use a custom timestamp function
*
* Can be useful to use nanoseconds instead of milliseconds
* if you need more precision
*
* But note that Pino will be slower, see
* https://github.com/pinojs/pino/blob/master/docs/api.md#timestamp-boolean--function
*/
import 'dotenv/config'
import { pino } from 'pino'
import type { LokiOptions } from '../src/index'
const loadNs = process.hrtime()
const loadMs = new Date().getTime()
function nanoseconds() {
const diffNs = process.hrtime(loadNs)
return BigInt(loadMs) * BigInt(1e6) + BigInt(diffNs[0] * 1e9 + diffNs[1])
}
const transport = pino.transport<LokiOptions>({
// 👇 Replace this with "pino-loki"
target: '../dist/index.mjs',
options: {
// These labels will be added to every log
labels: { application: 'MY-APP' },
batching: false,
// Credentials for our Loki instance
host: process.env.LOKI_HOST!,
basicAuth: {
username: process.env.LOKI_USERNAME!,
password: process.env.LOKI_PASSWORD!,
},
},
})
const logger = pino(
{
// 👇 Will replace default pino timestamp with our custom one
timestamp: () => `,"time":${nanoseconds()}`,
},
transport,
)
logger.info('custom timestamp 1!')
logger.info('custom timestamp 2!')
logger.info('custom timestamp 3!')