Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Polishing log format
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Walter committed Sep 21, 2019
1 parent dc646de commit 13689c7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 26 deletions.
75 changes: 50 additions & 25 deletions index.js
Expand Up @@ -3,10 +3,23 @@
const { print, chalk } = require('@ianwalter/print')
const split = require('split2')
const parseJson = require('fast-json-parse')
const cli = require('@ianwalter/cli')

const config = cli({
name: 'pino-print',
opts: {
alias: {
level: 'l'
},
default: {
level: 'info'
}
}
})

function pinoPrint (line) {
if (line[0] === '{') {
const {
let {
value: {
// General log properties:
level,
Expand All @@ -31,59 +44,71 @@ function pinoPrint (line) {
} = parseJson(line || {})

const messages = []

// Determine emoji/log type to use.
let logType = 'log'
if (req && res) {
messages.push('📨')
} else if (req) {
messages.push('📥')
} else if (res) {
messages.push('📤')
} else if (level < 30) {
logType = 'debug'
} else if (level === 30) {
logType = 'info'
const isRequest = responseTime !== undefined

// Determine type of log to use.
let logType = 'debug'
let logColor = 'magenta'
if (level === 30) {
logType = isRequest ? 'text' : 'info'
logColor = isRequest ? 'white' : 'blue'
} else if (level === 40) {
logType = 'warn'
logColor = 'yellow'
} else if (level === 50) {
logType = 'error'
logColor = 'red'
} else if (level === 60) {
logType = 'fatal'
logColor = 'red'
}

if (time) {
const datetime = new Date(time)
const [second, meridiem] = datetime.toLocaleTimeString().split(' ')
const ms = datetime.getMilliseconds()
const ms = `${datetime.getMilliseconds()}`.padEnd(3, '0')
const date = datetime.toLocaleDateString()
messages.push(`${date} ${second}.${ms}${meridiem.toLowerCase()}`)
messages.push(
chalk.white.bold(`${date} ${second}.${ms}${meridiem.toLowerCase()} ●`)
)
}

if (res && res.statusCode) {
messages.push(chalk[logColor](res.statusCode))
}

if (req && req.method) {
messages.push(chalk.bold.yellow(req.method))
if (req && req.method && isRequest) {
messages.push(chalk[logColor](req.method))
}

if (req && req.url) {
messages.push(chalk.bold.yellow(req.url))
if (req && req.url && isRequest) {
messages.push(chalk[logColor](req.url))
}

if (msg) {
messages.push(chalk.bold.yellow(msg))
messages.push(chalk[logColor](msg))
}

if (responseTime) {
if (isRequest) {
responseTime = responseTime === 0 ? '< 1' : responseTime
// FIXME: use timer to get a better formatted duration string.
messages.push(chalk.bold.yellow(`in ${responseTime}ms`))
messages.push(chalk.dim(`in ${responseTime}ms`))
}

// Add back information if log level is debug.
if (logType === 'debug') {
if (config.level === 'debug' && responseTime) {
rest.hostname = hostname
rest.pid = pid
rest.req = {
id: req.id,
headers: req.headers
}
rest.res = { headers: res.headers }
}

print[logType](...messages, ...Object.keys(rest).length ? [rest] : [])
} else {
print.text(line)
print.write(line)
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -10,6 +10,7 @@
"test": "bff"
},
"dependencies": {
"@ianwalter/cli": "^1.2.0",
"@ianwalter/print": "^3.4.0"
},
"devDependencies": {
Expand Down
11 changes: 10 additions & 1 deletion tests/example.js
Expand Up @@ -6,8 +6,17 @@ const requester = new Requester({ shouldThrow: false })

async function run () {
const server = await createKoaServer()
server.use(pino())
server.use(pino({ level: 'debug' }))
server.use(ctx => {
if (ctx.req.method === 'POST') {
ctx.body = { version: '1.0.0' }
} else {
ctx.log.debug('Entered root GET handler')
ctx.status = 204
}
})
await requester.get(server.url)
await requester.post(server.url, { body: { message: 'in a bottle' } })
await server.close()
}

Expand Down

0 comments on commit 13689c7

Please sign in to comment.