Home / File/ encapsulated-child-logger-factory.test.js — fastify Source File

encapsulated-child-logger-factory.test.js — fastify Source File

Architecture documentation for encapsulated-child-logger-factory.test.js, a javascript file in the fastify codebase.

Entity Profile

Source Code

'use strict'

const { test } = require('node:test')
const Fastify = require('..')
const fp = require('fastify-plugin')

test('encapsulates an child logger factory', async t => {
  t.plan(4)

  const fastify = Fastify()
  fastify.register(async function (fastify) {
    fastify.setChildLoggerFactory(function pluginFactory (logger, bindings, opts) {
      const child = logger.child(bindings, opts)
      child.customLog = function (message) {
        t.assert.strictEqual(message, 'custom')
      }
      return child
    })
    fastify.get('/encapsulated', async (req) => {
      req.log.customLog('custom')
    })
  })

  fastify.setChildLoggerFactory(function globalFactory (logger, bindings, opts) {
    const child = logger.child(bindings, opts)
    child.globalLog = function (message) {
      t.assert.strictEqual(message, 'global')
    }
    return child
  })
  fastify.get('/not-encapsulated', async (req) => {
    req.log.globalLog('global')
  })

  const res1 = await fastify.inject('/encapsulated')
  t.assert.strictEqual(res1.statusCode, 200)

  const res2 = await fastify.inject('/not-encapsulated')
  t.assert.strictEqual(res2.statusCode, 200)
})

test('child logger factory set on root scope when using fastify-plugin', async t => {
  t.plan(4)

  const fastify = Fastify()
  fastify.register(fp(async function (fastify) {
    // Using fastify-plugin, the factory should be set on the root scope
    fastify.setChildLoggerFactory(function pluginFactory (logger, bindings, opts) {
      const child = logger.child(bindings, opts)
      child.customLog = function (message) {
        t.assert.strictEqual(message, 'custom')
      }
      return child
    })
    fastify.get('/not-encapsulated-1', async (req) => {
      req.log.customLog('custom')
    })
  }))

  fastify.get('/not-encapsulated-2', async (req) => {
    req.log.customLog('custom')
  })

  const res1 = await fastify.inject('/not-encapsulated-1')
  t.assert.strictEqual(res1.statusCode, 200)

  const res2 = await fastify.inject('/not-encapsulated-2')
  t.assert.strictEqual(res2.statusCode, 200)
})

Frequently Asked Questions

What does encapsulated-child-logger-factory.test.js do?
encapsulated-child-logger-factory.test.js is a source file in the fastify codebase, written in javascript.
Where is encapsulated-child-logger-factory.test.js in the architecture?
encapsulated-child-logger-factory.test.js is located at test/encapsulated-child-logger-factory.test.js (directory: test).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free