|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const os = require('os') |
| 4 | +const { join } = require('path') |
| 5 | +const { readFile } = require('fs').promises |
| 6 | +const writeStream = require('flush-write-stream') |
| 7 | +const { watchFileCreated, file } = require('../helper') |
| 8 | +const { test } = require('tap') |
| 9 | +const pino = require('../../') |
| 10 | + |
| 11 | +const { pid } = process |
| 12 | +const hostname = os.hostname() |
| 13 | + |
| 14 | +function serializeError (error) { |
| 15 | + return { |
| 16 | + type: error.name, |
| 17 | + message: error.message, |
| 18 | + stack: error.stack |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +function parseLogs (buffer) { |
| 23 | + return JSON.parse(`[${buffer.toString().replace(/}{/g, '},{')}]`) |
| 24 | +} |
| 25 | + |
| 26 | +test('transport uses pino config', async ({ same, teardown, plan }) => { |
| 27 | + plan(1) |
| 28 | + const destination = file() |
| 29 | + const transport = pino.transport({ |
| 30 | + pipeline: [{ |
| 31 | + target: join(__dirname, '..', 'fixtures', 'transport-uses-pino-config.js') |
| 32 | + }, { |
| 33 | + target: 'pino/file', |
| 34 | + options: { destination } |
| 35 | + }] |
| 36 | + }) |
| 37 | + teardown(transport.end.bind(transport)) |
| 38 | + const instance = pino({ |
| 39 | + messageKey: 'customMessageKey', |
| 40 | + errorKey: 'customErrorKey', |
| 41 | + customLevels: { custom: 35 } |
| 42 | + }, transport) |
| 43 | + |
| 44 | + const error = new Error('bar') |
| 45 | + instance.custom('foo') |
| 46 | + instance.error(error) |
| 47 | + await watchFileCreated(destination) |
| 48 | + const result = parseLogs(await readFile(destination)) |
| 49 | + |
| 50 | + same(result, [{ |
| 51 | + severityText: 'custom', |
| 52 | + body: 'foo', |
| 53 | + attributes: { |
| 54 | + pid, |
| 55 | + hostname |
| 56 | + } |
| 57 | + }, { |
| 58 | + severityText: 'error', |
| 59 | + body: 'bar', |
| 60 | + attributes: { |
| 61 | + pid, |
| 62 | + hostname |
| 63 | + }, |
| 64 | + error: serializeError(error) |
| 65 | + }]) |
| 66 | +}) |
| 67 | + |
| 68 | +test('transport uses pino config without customizations', async ({ same, teardown, plan }) => { |
| 69 | + plan(1) |
| 70 | + const destination = file() |
| 71 | + const transport = pino.transport({ |
| 72 | + pipeline: [{ |
| 73 | + target: join(__dirname, '..', 'fixtures', 'transport-uses-pino-config.js') |
| 74 | + }, { |
| 75 | + target: 'pino/file', |
| 76 | + options: { destination } |
| 77 | + }] |
| 78 | + }) |
| 79 | + teardown(transport.end.bind(transport)) |
| 80 | + const instance = pino(transport) |
| 81 | + |
| 82 | + const error = new Error('qux') |
| 83 | + instance.info('baz') |
| 84 | + instance.error(error) |
| 85 | + await watchFileCreated(destination) |
| 86 | + const result = parseLogs(await readFile(destination)) |
| 87 | + |
| 88 | + same(result, [{ |
| 89 | + severityText: 'info', |
| 90 | + body: 'baz', |
| 91 | + attributes: { |
| 92 | + pid, |
| 93 | + hostname |
| 94 | + } |
| 95 | + }, { |
| 96 | + severityText: 'error', |
| 97 | + body: 'qux', |
| 98 | + attributes: { |
| 99 | + pid, |
| 100 | + hostname |
| 101 | + }, |
| 102 | + error: serializeError(error) |
| 103 | + }]) |
| 104 | +}) |
| 105 | + |
| 106 | +test('transport uses pino config with multistream', async ({ same, teardown, plan }) => { |
| 107 | + plan(2) |
| 108 | + const destination = file() |
| 109 | + const messages = [] |
| 110 | + const stream = writeStream(function (data, enc, cb) { |
| 111 | + const message = JSON.parse(data) |
| 112 | + delete message.time |
| 113 | + messages.push(message) |
| 114 | + cb() |
| 115 | + }) |
| 116 | + const transport = pino.transport({ |
| 117 | + pipeline: [{ |
| 118 | + target: join(__dirname, '..', 'fixtures', 'transport-uses-pino-config.js') |
| 119 | + }, { |
| 120 | + target: 'pino/file', |
| 121 | + options: { destination } |
| 122 | + }] |
| 123 | + }) |
| 124 | + teardown(transport.end.bind(transport)) |
| 125 | + const instance = pino({ |
| 126 | + messageKey: 'customMessageKey', |
| 127 | + errorKey: 'customErrorKey', |
| 128 | + customLevels: { custom: 35 } |
| 129 | + }, pino.multistream([transport, { stream }])) |
| 130 | + |
| 131 | + const error = new Error('buzz') |
| 132 | + const serializedError = serializeError(error) |
| 133 | + instance.custom('fizz') |
| 134 | + instance.error(error) |
| 135 | + await watchFileCreated(destination) |
| 136 | + const result = parseLogs(await readFile(destination)) |
| 137 | + |
| 138 | + same(result, [{ |
| 139 | + severityText: 'custom', |
| 140 | + body: 'fizz', |
| 141 | + attributes: { |
| 142 | + pid, |
| 143 | + hostname |
| 144 | + } |
| 145 | + }, { |
| 146 | + severityText: 'error', |
| 147 | + body: 'buzz', |
| 148 | + attributes: { |
| 149 | + pid, |
| 150 | + hostname |
| 151 | + }, |
| 152 | + error: serializedError |
| 153 | + }]) |
| 154 | + |
| 155 | + same(messages, [{ |
| 156 | + level: 35, |
| 157 | + pid, |
| 158 | + hostname, |
| 159 | + customMessageKey: 'fizz' |
| 160 | + }, { |
| 161 | + level: 50, |
| 162 | + pid, |
| 163 | + hostname, |
| 164 | + customErrorKey: serializedError, |
| 165 | + customMessageKey: 'buzz' |
| 166 | + }]) |
| 167 | +}) |
0 commit comments