Skip to content

Commit fc38c62

Browse files
author
winches
committed
feat: 新增pino优化日志输出
1 parent 6363f01 commit fc38c62

File tree

4 files changed

+140
-11
lines changed

4 files changed

+140
-11
lines changed

app.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
11
const { getOptions } = require('./src/utils/index.js')
22

33
const fastify = require('fastify')({
4-
logger: true
4+
logger: {
5+
transport: {
6+
target: 'pino-pretty',
7+
options: {
8+
translateTime: 'HH:MM:ss',
9+
ignore: 'pid,hostname,level,req.remoteAddress,req.remotePort,reqId',
10+
},
11+
serializers: {
12+
res (reply) {
13+
// The default
14+
return {
15+
statusCode: reply.statusCode,
16+
}
17+
},
18+
}
19+
}
20+
}
521
})
622

723
// Declare a route
824
fastify.register(require('./routes/index.js'))
925

1026
// Run the server!
1127
let { port } = getOptions()
12-
let regPort = process.argv[process.argv.length-1].match(/\d+/g)
28+
let regPort = process.argv[process.argv.length - 1].match(/\d+/g)
1329
port = port || (regPort && regPort[0]) || 5050
1430
fastify.listen({ port }, function (err, address) {
1531
if (err) {
1632
fastify.log.error(err)
1733
process.exit(1)
1834
}
35+
36+
// 打印已添加的全部路由
37+
console.log(fastify.printRoutes());
1938
// Server is now listening on ${address}
2039
})

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@winches/auto-mock",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "约定式路由的mock工具",
55
"main": "dist/app.js",
66
"directories": {
@@ -34,7 +34,8 @@
3434
"commander": "^10.0.0",
3535
"execa": "^5.0.0",
3636
"fastify": "^4.0.0",
37-
"nodemon": "^2.0.20"
37+
"nodemon": "^2.0.20",
38+
"pino-pretty": "^10.0.0"
3839
},
3940
"devDependencies": {
4041
"tap": "^16.1.0",

pnpm-lock.yaml

+114-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

routes/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ async function routes (fastify, options) {
3939
// 判断是否为动态路径
4040
if (newItem.includes(':')) {
4141
const path = newItem.replace(':', '/:')
42-
return addRoutes(path, item)
42+
addRoutes(path, item)
43+
continue
4344
}
4445
addRoutes(newItem, item)
4546
}

0 commit comments

Comments
 (0)