add debug logger

This commit is contained in:
Andre Staltz 2023-06-06 16:08:19 +03:00
parent 979ab1589e
commit e36dd10e5b
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
3 changed files with 44 additions and 4 deletions

View File

@ -9,11 +9,12 @@ const fastify = require('fastify')
const fastifyView = require('@fastify/view') const fastifyView = require('@fastify/view')
const fastifyStatic = require('@fastify/static') const fastifyStatic = require('@fastify/static')
const ejs = require('ejs') const ejs = require('ejs')
const logger = require('./logger.cjs')
const staticsPath = path.join(__dirname, 'public') const staticsPath = path.join(__dirname, 'public')
const viewsPath = path.join(__dirname, 'views') const viewsPath = path.join(__dirname, 'views')
const app = fastify({ logger: true }) const app = fastify({ logger })
app.register(fastifyView, { engine: { ejs }, root: viewsPath }) app.register(fastifyView, { engine: { ejs }, root: viewsPath })

37
lib/logger.cjs Normal file
View File

@ -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

View File

@ -26,8 +26,10 @@
"dependencies": { "dependencies": {
"@fastify/static": "6.10.2", "@fastify/static": "6.10.2",
"@fastify/view": "7.4.1", "@fastify/view": "7.4.1",
"debug": "4.3.4",
"ejs": "3.1.9", "ejs": "3.1.9",
"fastify": "4.17.0", "fastify": "4.17.0",
"pino": "8.14.1",
"unified": "10.1.2", "unified": "10.1.2",
"remark-parse": "10.0.2", "remark-parse": "10.0.2",
"remark-rehype": "10.1.0", "remark-rehype": "10.1.0",
@ -42,10 +44,10 @@
}, },
"scripts": { "scripts": {
"setup": "npm install --omit=dev", "setup": "npm install --omit=dev",
"start": "node lib/index.js", "start": "DEBUG=*,-avvio node lib/index.js",
"test": "tape test/*.js | tap-arc --bail", "test": "tape test/*.js | tap-arc --bail",
"format-code": "prettier --write \"(lib|test)/**/*.js\"", "format-code": "prettier --write \"(lib|test)/**/*.(c)?js\"",
"format-code-staged": "pretty-quick --staged --pattern \"(lib|test)/**/*.js\"", "format-code-staged": "pretty-quick --staged --pattern \"(lib|test)/**/*.(c)?js\"",
"coverage": "c8 --reporter=lcov npm run test" "coverage": "c8 --reporter=lcov npm run test"
}, },
"husky": { "husky": {