From e36dd10e5b4ffcee0e3cfd7d3d5739e42fb4b42c Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Tue, 6 Jun 2023 16:08:19 +0300 Subject: [PATCH] add debug logger --- lib/index.js | 3 ++- lib/logger.cjs | 37 +++++++++++++++++++++++++++++++++++++ package.json | 8 +++++--- 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 lib/logger.cjs diff --git a/lib/index.js b/lib/index.js index b83a6b4..49ece89 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,11 +9,12 @@ const fastify = require('fastify') const fastifyView = require('@fastify/view') const fastifyStatic = require('@fastify/static') const ejs = require('ejs') +const logger = require('./logger.cjs') const staticsPath = path.join(__dirname, 'public') const viewsPath = path.join(__dirname, 'views') -const app = fastify({ logger: true }) +const app = fastify({ logger }) app.register(fastifyView, { engine: { ejs }, root: viewsPath }) diff --git a/lib/logger.cjs b/lib/logger.cjs new file mode 100644 index 0000000..93e2bbc --- /dev/null +++ b/lib/logger.cjs @@ -0,0 +1,37 @@ +const debug = require('debug') +const { pino } = require('pino') + +const _info = debug('fastify:info') +const _error = debug('fastify:error') +const _warn = debug('fastify:warn') +const _fatal = debug('fastify:fatal') +const _trace = debug('fastify:trace') +const _debug = debug('fastify:debug') + +const logger = { + info(o, ...n) { + _info(o, ...n) + }, + warn(o, ...n) { + _warn(o, ...n) + }, + error(o, ...n) { + _error(o, ...n) + }, + fatal(o, ...n) { + _fatal(o, ...n) + }, + trace(o, ...n) { + _trace(o, ...n) + }, + debug(o, ...n) { + _debug(o, ...n) + }, + child() { + const child = Object.create(this) + child.pino = pino().child(...arguments) + return child + }, +} + +module.exports = logger diff --git a/package.json b/package.json index 0fee159..0d3d56c 100644 --- a/package.json +++ b/package.json @@ -26,8 +26,10 @@ "dependencies": { "@fastify/static": "6.10.2", "@fastify/view": "7.4.1", + "debug": "4.3.4", "ejs": "3.1.9", "fastify": "4.17.0", + "pino": "8.14.1", "unified": "10.1.2", "remark-parse": "10.0.2", "remark-rehype": "10.1.0", @@ -42,10 +44,10 @@ }, "scripts": { "setup": "npm install --omit=dev", - "start": "node lib/index.js", + "start": "DEBUG=*,-avvio node lib/index.js", "test": "tape test/*.js | tap-arc --bail", - "format-code": "prettier --write \"(lib|test)/**/*.js\"", - "format-code-staged": "pretty-quick --staged --pattern \"(lib|test)/**/*.js\"", + "format-code": "prettier --write \"(lib|test)/**/*.(c)?js\"", + "format-code-staged": "pretty-quick --staged --pattern \"(lib|test)/**/*.(c)?js\"", "coverage": "c8 --reporter=lcov npm run test" }, "husky": {