Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from chalk to colorette #218

Merged
merged 16 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [10, 12, 13, 14, 16]
node-version: [10, 12, 14, 16]
os: [macos-latest, ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout@v2.3.4
Expand Down
2 changes: 2 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ ts: false
jsx: false
flow: false
jobs: 1
files:
- 'test/**/*.test.js'
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ with keys corresponding to the options described in [CLI Arguments](#cliargs):

```js
{
colorize: chalk.supportsColor, // --colorize
colorize: colorette.options.enabled, // --colorize
crlf: false, // --crlf
errorLikeObjectKeys: ['err', 'error'], // --errorLikeObjectKeys
errorProps: '', // --errorProps
Expand All @@ -153,7 +153,7 @@ with keys corresponding to the options described in [CLI Arguments](#cliargs):
```

The `colorize` default follows
[`chalk.supportsColor`](https://www.npmjs.com/package/chalk#chalksupportscolor).
[`colorette.options.enabled`](https://github.com/jorgebucaran/colorette#optionsenabled).

`customPrettifiers` option provides the ability to add a custom prettify function
for specific log properties. `customPrettifiers` is an object, where keys are
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const chalk = require('chalk')
const { options: coloretteOptions } = require('colorette')
const jmespath = require('jmespath')
const colors = require('./lib/colors')
const { ERROR_LIKE_KEYS, MESSAGE_KEY, TIMESTAMP_KEY } = require('./lib/constants')
Expand All @@ -25,7 +25,7 @@ const jsonParser = input => {
}

const defaultOptions = {
colorize: chalk.supportsColor,
colorize: coloretteOptions.enabled,
crlf: false,
errorLikeObjectKeys: ERROR_LIKE_KEYS,
errorProps: '',
Expand Down
21 changes: 10 additions & 11 deletions lib/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ const plain = {
greyMessage: nocolor
}

const chalk = require('chalk')
const ctx = new chalk.Instance({ level: 3 })
const colorette = require('colorette')
const colored = {
default: ctx.white,
60: ctx.bgRed,
50: ctx.red,
40: ctx.yellow,
30: ctx.green,
20: ctx.blue,
10: ctx.grey,
message: ctx.cyan,
greyMessage: ctx.grey
default: colorette.white,
60: colorette.bgRed,
50: colorette.red,
40: colorette.yellow,
30: colorette.green,
20: colorette.blue,
10: colorette.gray,
message: colorette.cyan,
greyMessage: colorette.gray
}

function colorizeLevel (level, colorizer) {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function prettifyErrorLog ({
// Print all sibling properties except for the standard exclusions.
propertiesToPrint = Object.keys(log).filter(k => excludeProperties.includes(k) === false)
} else {
// Print only sepcified properties unless the property is a standard exclusion.
// Print only specified properties unless the property is a standard exclusion.
propertiesToPrint = errorProperties.filter(k => excludeProperties.includes(k) === false)
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"pino-pretty": "./bin.js"
},
"scripts": {
"ci": "standard && tap 'test/**/*.test.js' --coverage-report=lcovonly && npm run test-types",
"ci": "standard && tap --color --coverage-report=lcovonly && npm run test-types",
"lint": "standard | snazzy",
"test": "tap --100 'test/**/*.test.js'",
"test": "tap --100 --color",
"test-types": "tsc && tsd"
},
"repository": {
Expand All @@ -34,7 +34,7 @@
"dependencies": {
"@hapi/bourne": "^2.0.0",
"args": "^5.0.1",
"chalk": "^4.0.0",
"colorette": "^1.3.0",
"dateformat": "^4.5.1",
"fast-safe-stringify": "^2.0.7",
"jmespath": "^0.15.0",
Expand Down
12 changes: 1 addition & 11 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const test = require('tap').test
const bin = require.resolve(path.join(__dirname, '..', 'bin.js'))
const epoch = 1522431328992
const logLine = '{"level":30,"time":1522431328992,"msg":"hello world","pid":42,"hostname":"foo"}\n'
const env = { TERM: 'dumb' }

test('cli', (t) => {
t.test('does basic reformatting', (t) => {
t.plan(1)
const env = { TERM: 'dumb' }
const child = spawn(process.argv[0], [bin], { env })
child.on('error', t.threw)
child.stdout.on('data', (data) => {
Expand All @@ -24,7 +24,6 @@ test('cli', (t) => {
;['--levelFirst', '-l'].forEach((optionName) => {
t.test(`flips epoch and level via ${optionName}`, (t) => {
t.plan(1)
const env = { TERM: 'dumb' }
const child = spawn(process.argv[0], [bin, optionName], { env })
child.on('error', t.threw)
child.stdout.on('data', (data) => {
Expand All @@ -38,7 +37,6 @@ test('cli', (t) => {
;['--translateTime', '-t'].forEach((optionName) => {
t.test(`translates time to default format via ${optionName}`, (t) => {
t.plan(1)
const env = { TERM: 'dumb' }
const child = spawn(process.argv[0], [bin, optionName], { env })
child.on('error', t.threw)
child.stdout.on('data', (data) => {
Expand All @@ -52,7 +50,6 @@ test('cli', (t) => {
;['--search', '-s'].forEach((optionName) => {
t.test(`does search via ${optionName}`, (t) => {
t.plan(1)
const env = { TERM: 'dumb' }
const child = spawn(process.argv[0], [bin, optionName, 'msg == `hello world`'], { env })
child.on('error', t.threw)
child.stdout.on('data', (data) => {
Expand All @@ -65,7 +62,6 @@ test('cli', (t) => {

t.test('does search but finds only 1 out of 2', (t) => {
t.plan(1)
const env = { TERM: 'dumb' }
const child = spawn(process.argv[0], [bin, '-s', 'msg == `hello world`'], { env })
child.on('error', t.threw)
child.stdout.on('data', (data) => {
Expand All @@ -79,7 +75,6 @@ test('cli', (t) => {
;['--ignore', '-i'].forEach((optionName) => {
t.test('does ignore multiple keys', (t) => {
t.plan(1)
const env = { TERM: 'dumb' }
const child = spawn(process.argv[0], [bin, optionName, 'pid,hostname'], { env })
child.on('error', t.threw)
child.stdout.on('data', (data) => {
Expand All @@ -92,7 +87,6 @@ test('cli', (t) => {

t.test('does ignore escaped keys', (t) => {
t.plan(1)
const env = { TERM: 'dumb' }
const child = spawn(process.argv[0], [bin, '-i', 'log\\.domain\\.corp/foo'], { env })
child.on('error', t.threw)
child.stdout.on('data', (data) => {
Expand All @@ -105,7 +99,6 @@ test('cli', (t) => {

t.test('passes through stringified date as string', (t) => {
t.plan(1)
const env = { TERM: 'dumb' }
const child = spawn(process.argv[0], [bin], { env })
child.on('error', t.threw)

Expand All @@ -124,7 +117,6 @@ test('cli', (t) => {
;['--timestampKey', '-a'].forEach((optionName) => {
t.test(`uses specified timestamp key via ${optionName}`, (t) => {
t.plan(1)
const env = { TERM: 'dumb' }
const child = spawn(process.argv[0], [bin, optionName, '@timestamp'], { env })
child.on('error', t.threw)
child.stdout.on('data', (data) => {
Expand All @@ -146,7 +138,6 @@ test('cli', (t) => {
}
})) + '\n'

const env = { TERM: 'dumb' }
const child = spawn(process.argv[0], [bin, optionName], { env })
child.on('error', t.threw)
child.stdout.on('data', (data) => {
Expand All @@ -170,7 +161,6 @@ test('cli', (t) => {
}
})) + '\n'

const env = { TERM: 'dumb' }
const child = spawn(process.argv[0], [bin, '-S', '-i', 'extra.foo,extra.nested,extra.nested.miss'], { env })
child.on('error', t.threw)
child.stdout.on('data', (data) => {
Expand Down
31 changes: 31 additions & 0 deletions test/example/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Run this to see how colouring works

const _prettyFactory = require('../../')
const pino = require('pino')
const { Writable } = require('readable-stream')

function prettyFactory () {
return _prettyFactory({
colorize: true
})
}

const pretty = prettyFactory()
const formatted = pretty('this is not json\nit\'s just regular output\n')
console.log(formatted)

const opts = {
base: {
hostname: 'localhost',
pid: process.pid
}
}
const log = pino(opts, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
console.log(formatted)
cb()
}
}))

log.info('foobar')