Aktueller Stand

This commit is contained in:
2026-01-22 19:05:45 +01:00
parent 85dee61a4d
commit e280e4eadb
1967 changed files with 397327 additions and 74093 deletions

View File

@@ -2,58 +2,56 @@
const stream = require('node:stream')
const t = require('tap')
const t = require('node:test')
const split = require('split2')
const pino = require('pino')
const Fastify = require('../../fastify')
const { on } = stream
t.test('logger options', (t) => {
t.setTimeout(60000)
t.test('logger options', { timeout: 60000 }, async (t) => {
t.plan(16)
t.plan(12)
t.test('logger can be silenced', (t) => {
await t.test('logger can be silenced', (t) => {
t.plan(17)
const fastify = Fastify({
logger: false
})
t.teardown(fastify.close.bind(fastify))
t.ok(fastify.log)
t.equal(typeof fastify.log, 'object')
t.equal(typeof fastify.log.fatal, 'function')
t.equal(typeof fastify.log.error, 'function')
t.equal(typeof fastify.log.warn, 'function')
t.equal(typeof fastify.log.info, 'function')
t.equal(typeof fastify.log.debug, 'function')
t.equal(typeof fastify.log.trace, 'function')
t.equal(typeof fastify.log.child, 'function')
t.after(() => fastify.close())
t.assert.ok(fastify.log)
t.assert.deepEqual(typeof fastify.log, 'object')
t.assert.deepEqual(typeof fastify.log.fatal, 'function')
t.assert.deepEqual(typeof fastify.log.error, 'function')
t.assert.deepEqual(typeof fastify.log.warn, 'function')
t.assert.deepEqual(typeof fastify.log.info, 'function')
t.assert.deepEqual(typeof fastify.log.debug, 'function')
t.assert.deepEqual(typeof fastify.log.trace, 'function')
t.assert.deepEqual(typeof fastify.log.child, 'function')
const childLog = fastify.log.child()
t.equal(typeof childLog, 'object')
t.equal(typeof childLog.fatal, 'function')
t.equal(typeof childLog.error, 'function')
t.equal(typeof childLog.warn, 'function')
t.equal(typeof childLog.info, 'function')
t.equal(typeof childLog.debug, 'function')
t.equal(typeof childLog.trace, 'function')
t.equal(typeof childLog.child, 'function')
t.assert.deepEqual(typeof childLog, 'object')
t.assert.deepEqual(typeof childLog.fatal, 'function')
t.assert.deepEqual(typeof childLog.error, 'function')
t.assert.deepEqual(typeof childLog.warn, 'function')
t.assert.deepEqual(typeof childLog.info, 'function')
t.assert.deepEqual(typeof childLog.debug, 'function')
t.assert.deepEqual(typeof childLog.trace, 'function')
t.assert.deepEqual(typeof childLog.child, 'function')
})
t.test('Should set a custom logLevel for a plugin', async (t) => {
await t.test('Should set a custom logLevel for a plugin', async (t) => {
const lines = ['incoming request', 'Hello', 'request completed']
t.plan(lines.length + 2)
const stream = split(JSON.parse)
const logger = pino({ level: 'error' }, stream)
const loggerInstance = pino({ level: 'error' }, stream)
const fastify = Fastify({
logger
loggerInstance
})
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())
fastify.get('/', (req, reply) => {
req.log.info('Not Exist') // we should not see this log
@@ -73,33 +71,33 @@ t.test('logger options', (t) => {
{
const response = await fastify.inject({ method: 'GET', url: '/' })
const body = await response.json()
t.same(body, { hello: 'world' })
t.assert.deepEqual(body.hello, 'world')
}
{
const response = await fastify.inject({ method: 'GET', url: '/plugin' })
const body = await response.json()
t.same(body, { hello: 'world' })
t.assert.deepEqual(body.hello, 'world')
}
for await (const [line] of on(stream, 'data')) {
t.same(line.msg, lines.shift())
t.assert.deepEqual(line.msg, lines.shift())
if (lines.length === 0) break
}
})
t.test('Should set a custom logSerializers for a plugin', async (t) => {
await t.test('Should set a custom logSerializers for a plugin', async (t) => {
const lines = ['incoming request', 'XHello', 'request completed']
t.plan(lines.length + 1)
const stream = split(JSON.parse)
const logger = pino({ level: 'error' }, stream)
const loggerInstance = pino({ level: 'error' }, stream)
const fastify = Fastify({
logger
loggerInstance
})
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())
fastify.register(function (instance, opts, done) {
instance.get('/plugin', (req, reply) => {
@@ -114,28 +112,28 @@ t.test('logger options', (t) => {
{
const response = await fastify.inject({ method: 'GET', url: '/plugin' })
const body = await response.json()
t.same(body, { hello: 'world' })
t.assert.deepEqual(body.hello, 'world')
}
for await (const [line] of on(stream, 'data')) {
// either test or msg
t.equal(line.test || line.msg, lines.shift())
t.assert.deepEqual(line.test || line.msg, lines.shift())
if (lines.length === 0) break
}
})
t.test('Should set a custom logLevel for every plugin', async (t) => {
await t.test('Should set a custom logLevel for every plugin', async (t) => {
const lines = ['incoming request', 'info', 'request completed', 'incoming request', 'debug', 'request completed']
t.plan(lines.length * 2 + 3)
const stream = split(JSON.parse)
const logger = pino({ level: 'error' }, stream)
const loggerInstance = pino({ level: 'error' }, stream)
const fastify = Fastify({
logger
loggerInstance
})
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())
fastify.get('/', (req, reply) => {
req.log.warn('Hello') // we should not see this log
@@ -165,39 +163,39 @@ t.test('logger options', (t) => {
{
const response = await fastify.inject({ method: 'GET', url: '/' })
const body = await response.json()
t.same(body, { hello: 'world' })
t.assert.deepEqual(body, { hello: 'world' })
}
{
const response = await fastify.inject({ method: 'GET', url: '/info' })
const body = await response.json()
t.same(body, { hello: 'world' })
t.assert.deepEqual(body, { hello: 'world' })
}
{
const response = await fastify.inject({ method: 'GET', url: '/debug' })
const body = await response.json()
t.same(body, { hello: 'world' })
t.assert.deepEqual(body, { hello: 'world' })
}
for await (const [line] of on(stream, 'data')) {
t.ok(line.level === 30 || line.level === 20)
t.equal(line.msg, lines.shift())
t.assert.ok(line.level === 30 || line.level === 20)
t.assert.deepEqual(line.msg, lines.shift())
if (lines.length === 0) break
}
})
t.test('Should set a custom logSerializers for every plugin', async (t) => {
await t.test('Should set a custom logSerializers for every plugin', async (t) => {
const lines = ['incoming request', 'Hello', 'request completed', 'incoming request', 'XHello', 'request completed', 'incoming request', 'ZHello', 'request completed']
t.plan(lines.length + 3)
const stream = split(JSON.parse)
const logger = pino({ level: 'info' }, stream)
const loggerInstance = pino({ level: 'info' }, stream)
const fastify = Fastify({
logger
loggerInstance
})
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())
fastify.get('/', (req, reply) => {
req.log.warn({ test: 'Hello' })
@@ -225,38 +223,38 @@ t.test('logger options', (t) => {
{
const response = await fastify.inject({ method: 'GET', url: '/' })
const body = await response.json()
t.same(body, { hello: 'world' })
t.assert.deepEqual(body, { hello: 'world' })
}
{
const response = await fastify.inject({ method: 'GET', url: '/test1' })
const body = await response.json()
t.same(body, { hello: 'world' })
t.assert.deepEqual(body, { hello: 'world' })
}
{
const response = await fastify.inject({ method: 'GET', url: '/test2' })
const body = await response.json()
t.same(body, { hello: 'world' })
t.assert.deepEqual(body, { hello: 'world' })
}
for await (const [line] of on(stream, 'data')) {
t.equal(line.test || line.msg, lines.shift())
t.assert.deepEqual(line.test || line.msg, lines.shift())
if (lines.length === 0) break
}
})
t.test('Should override serializers from route', async (t) => {
await t.test('Should override serializers from route', async (t) => {
const lines = ['incoming request', 'ZHello', 'request completed']
t.plan(lines.length + 1)
const stream = split(JSON.parse)
const logger = pino({ level: 'info' }, stream)
const loggerInstance = pino({ level: 'info' }, stream)
const fastify = Fastify({
logger
loggerInstance
})
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())
fastify.register(function (instance, opts, done) {
instance.get('/', {
@@ -275,26 +273,26 @@ t.test('logger options', (t) => {
{
const response = await fastify.inject({ method: 'GET', url: '/' })
const body = await response.json()
t.same(body, { hello: 'world' })
t.assert.deepEqual(body, { hello: 'world' })
}
for await (const [line] of on(stream, 'data')) {
t.equal(line.test || line.msg, lines.shift())
t.assert.deepEqual(line.test || line.msg, lines.shift())
if (lines.length === 0) break
}
})
t.test('Should override serializers from plugin', async (t) => {
await t.test('Should override serializers from plugin', async (t) => {
const lines = ['incoming request', 'ZHello', 'request completed']
t.plan(lines.length + 1)
const stream = split(JSON.parse)
const logger = pino({ level: 'info' }, stream)
const loggerInstance = pino({ level: 'info' }, stream)
const fastify = Fastify({
logger
loggerInstance
})
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())
fastify.register(function (instance, opts, done) {
instance.register(context1, {
@@ -318,27 +316,27 @@ t.test('logger options', (t) => {
{
const response = await fastify.inject({ method: 'GET', url: '/' })
const body = await response.json()
t.same(body, { hello: 'world' })
t.assert.deepEqual(body, { hello: 'world' })
}
for await (const [line] of on(stream, 'data')) {
t.equal(line.test || line.msg, lines.shift())
t.assert.deepEqual(line.test || line.msg, lines.shift())
if (lines.length === 0) break
}
})
t.test('Should increase the log level for a specific plugin', async (t) => {
await t.test('Should increase the log level for a specific plugin', async (t) => {
const lines = ['Hello']
t.plan(lines.length * 2 + 1)
const stream = split(JSON.parse)
const logger = pino({ level: 'info' }, stream)
const loggerInstance = pino({ level: 'info' }, stream)
const fastify = Fastify({
logger
loggerInstance
})
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())
fastify.register(function (instance, opts, done) {
instance.get('/', (req, reply) => {
@@ -353,28 +351,28 @@ t.test('logger options', (t) => {
{
const response = await fastify.inject({ method: 'GET', url: '/' })
const body = await response.json()
t.same(body, { hello: 'world' })
t.assert.deepEqual(body, { hello: 'world' })
}
for await (const [line] of on(stream, 'data')) {
t.equal(line.level, 50)
t.equal(line.msg, lines.shift())
t.assert.deepEqual(line.level, 50)
t.assert.deepEqual(line.msg, lines.shift())
if (lines.length === 0) break
}
})
t.test('Should set the log level for the customized 404 handler', async (t) => {
await t.test('Should set the log level for the customized 404 handler', async (t) => {
const lines = ['Hello']
t.plan(lines.length * 2 + 1)
const stream = split(JSON.parse)
const logger = pino({ level: 'warn' }, stream)
const loggerInstance = pino({ level: 'warn' }, stream)
const fastify = Fastify({
logger
loggerInstance
})
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())
fastify.register(function (instance, opts, done) {
instance.setNotFoundHandler(function (req, reply) {
@@ -388,28 +386,28 @@ t.test('logger options', (t) => {
{
const response = await fastify.inject({ method: 'GET', url: '/' })
t.equal(response.statusCode, 404)
t.assert.deepEqual(response.statusCode, 404)
}
for await (const [line] of on(stream, 'data')) {
t.equal(line.level, 50)
t.equal(line.msg, lines.shift())
t.assert.deepEqual(line.level, 50)
t.assert.deepEqual(line.msg, lines.shift())
if (lines.length === 0) break
}
})
t.test('Should set the log level for the customized 500 handler', async (t) => {
await t.test('Should set the log level for the customized 500 handler', async (t) => {
const lines = ['Hello']
t.plan(lines.length * 2 + 1)
const stream = split(JSON.parse)
const logger = pino({ level: 'warn' }, stream)
const loggerInstance = pino({ level: 'warn' }, stream)
const fastify = Fastify({
logger
loggerInstance
})
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())
fastify.register(function (instance, opts, done) {
instance.get('/', (req, reply) => {
@@ -428,28 +426,28 @@ t.test('logger options', (t) => {
{
const response = await fastify.inject({ method: 'GET', url: '/' })
t.equal(response.statusCode, 500)
t.assert.deepEqual(response.statusCode, 500)
}
for await (const [line] of on(stream, 'data')) {
t.equal(line.level, 60)
t.equal(line.msg, lines.shift())
t.assert.deepEqual(line.level, 60)
t.assert.deepEqual(line.msg, lines.shift())
if (lines.length === 0) break
}
})
t.test('Should set a custom log level for a specific route', async (t) => {
await t.test('Should set a custom log level for a specific route', async (t) => {
const lines = ['incoming request', 'Hello', 'request completed']
t.plan(lines.length + 2)
const stream = split(JSON.parse)
const logger = pino({ level: 'error' }, stream)
const loggerInstance = pino({ level: 'error' }, stream)
const fastify = Fastify({
logger
loggerInstance
})
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())
fastify.get('/log', { logLevel: 'info' }, (req, reply) => {
req.log.info('Hello')
@@ -466,35 +464,116 @@ t.test('logger options', (t) => {
{
const response = await fastify.inject({ method: 'GET', url: '/log' })
const body = await response.json()
t.same(body, { hello: 'world' })
t.assert.deepEqual(body, { hello: 'world' })
}
{
const response = await fastify.inject({ method: 'GET', url: '/no-log' })
const body = await response.json()
t.same(body, { hello: 'world' })
t.assert.deepEqual(body, { hello: 'world' })
}
for await (const [line] of on(stream, 'data')) {
t.equal(line.msg, lines.shift())
t.assert.deepEqual(line.msg, lines.shift())
if (lines.length === 0) break
}
})
t.test('should pass when using unWritable props in the logger option', (t) => {
await t.test('should pass when using unWritable props in the logger option', (t) => {
t.plan(8)
const fastify = Fastify({
logger: Object.defineProperty({}, 'level', { value: 'info' })
})
t.teardown(fastify.close.bind(fastify))
t.after(() => fastify.close())
t.equal(typeof fastify.log, 'object')
t.equal(typeof fastify.log.fatal, 'function')
t.equal(typeof fastify.log.error, 'function')
t.equal(typeof fastify.log.warn, 'function')
t.equal(typeof fastify.log.info, 'function')
t.equal(typeof fastify.log.debug, 'function')
t.equal(typeof fastify.log.trace, 'function')
t.equal(typeof fastify.log.child, 'function')
t.assert.deepEqual(typeof fastify.log, 'object')
t.assert.deepEqual(typeof fastify.log.fatal, 'function')
t.assert.deepEqual(typeof fastify.log.error, 'function')
t.assert.deepEqual(typeof fastify.log.warn, 'function')
t.assert.deepEqual(typeof fastify.log.info, 'function')
t.assert.deepEqual(typeof fastify.log.debug, 'function')
t.assert.deepEqual(typeof fastify.log.trace, 'function')
t.assert.deepEqual(typeof fastify.log.child, 'function')
})
await t.test('Should throw an error if logger instance is passed to `logger`', async (t) => {
t.plan(2)
const stream = split(JSON.parse)
const logger = require('pino')(stream)
try {
Fastify({ logger })
} catch (err) {
t.assert.ok(err)
t.assert.deepEqual(err.code, 'FST_ERR_LOG_INVALID_LOGGER_CONFIG')
}
})
await t.test('Should throw an error if options are passed to `loggerInstance`', async (t) => {
t.plan(2)
try {
Fastify({ loggerInstance: { level: 'log' } })
} catch (err) {
t.assert.ok(err)
t.assert.strictEqual(err.code, 'FST_ERR_LOG_INVALID_LOGGER_INSTANCE')
}
})
await t.test('If both `loggerInstance` and `logger` are provided, an error should be thrown', async (t) => {
t.plan(2)
const loggerInstanceStream = split(JSON.parse)
const loggerInstance = pino({ level: 'error' }, loggerInstanceStream)
const loggerStream = split(JSON.parse)
try {
Fastify({
logger: {
stream: loggerStream,
level: 'info'
},
loggerInstance
})
} catch (err) {
t.assert.ok(err)
t.assert.deepEqual(err.code, 'FST_ERR_LOG_LOGGER_AND_LOGGER_INSTANCE_PROVIDED')
}
})
await t.test('`logger` should take pino configuration and create a pino logger', async (t) => {
const lines = ['hello', 'world']
t.plan(2 * lines.length + 2)
const loggerStream = split(JSON.parse)
const fastify = Fastify({
logger: {
stream: loggerStream,
level: 'error'
}
})
t.after(() => fastify.close())
fastify.get('/hello', (req, reply) => {
req.log.error('hello')
reply.code(404).send()
})
fastify.get('/world', (req, reply) => {
req.log.error('world')
reply.code(201).send()
})
await fastify.ready()
{
const response = await fastify.inject({ method: 'GET', url: '/hello' })
t.assert.deepEqual(response.statusCode, 404)
}
{
const response = await fastify.inject({ method: 'GET', url: '/world' })
t.assert.deepEqual(response.statusCode, 201)
}
for await (const [line] of on(loggerStream, 'data')) {
t.assert.deepEqual(line.level, 50)
t.assert.deepEqual(line.msg, lines.shift())
if (lines.length === 0) break
}
})
})