Skip to content

Commit 780996b

Browse files
committed
feat(logs@3): allow json logging; be colorless by default
1 parent 13ab610 commit 780996b

File tree

5 files changed

+92
-22
lines changed

5 files changed

+92
-22
lines changed

logs/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@
44

55
Exports a `Console` class in ESM and CJS formats.
66

7+
Usage:
8+
9+
```js
10+
import { Console } from '@hackbg/logs'
11+
// or
12+
const { Console } = require('@hackbg/logs')
13+
14+
const console = new Console("label", {
15+
color: true, // or false
16+
json: true, // or false
17+
})
18+
console("asdf")
19+
console.log("asdf")
20+
console.warn("asdf")
21+
console.error("asdf")
22+
console.debug("asdf")
23+
console.trace("asdf")
24+
console.table([["asdf","qwer"],["zxcv","uiop"]])
25+
console.table([{asdf:"qwer"},{zxcv:"uiop"}])
26+
console.table({foo:{asdf:"qwer"},bar:{zxcv:"uiop"}})
27+
```
28+
729
## TODO:
830

931
* [ ] Honor `NO_COLOR`

logs/logs.cjs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,35 @@ const timestamp = module.exports.timestamp = function timestamp (d = new Date())
1010
.replace(/[T]/g, '_')
1111
.slice(0, -3)
1212
}
13+
const serializer = (k, v) => (typeof v === 'bigint') ? String(v) : v
14+
const toJSON = data => JSON.stringify(data, serializer)
1315
const Console = module.exports.Console = class Console extends defineCallable(function log(...args){
1416
this.log(...args)
1517
}) {
1618
constructor (label, options = {}) {
1719
super()
1820
this.label = options.label ?? label ?? ''
1921
this.parent = options.parent ?? console
22+
this._print = options.json
23+
? (method, tag, message) => {
24+
this.parent[method](toJSON({ logTag: tag, logMethod: method, logMessage: message }))
25+
return this
26+
}
27+
: (method, tag, ...args) => {
28+
this.parent[method](tag, ...args)
29+
return this
30+
}
31+
this._tag = (!options.noColor || options.color)
32+
? (color, string) => {
33+
const tag1 = (string ? (chalk.inverse(color(bold(string))) + (this.label ? ' ' : '')) : '')
34+
const tag2 = (this.label ? color(this.label) : '')
35+
return tag1 + tag2
36+
}
37+
: (color, string) => {
38+
const tag1 = string
39+
const tag2 = this.label
40+
return tag1 + tag2
41+
}
2042
hideProperties(this,
2143
'label', 'tags', 'tag', '_tag', 'parent', 'sub',
2244
'br', 'log', 'info', 'warn', 'error', 'debug', 'trace', 'table',
@@ -37,13 +59,6 @@ const Console = module.exports.Console = class Console extends defineCallable(fu
3759
this._print('log', this._tag(chalk.white, 'TABLE '));
3860
this._print('table', ...args)
3961
}
40-
_print = (method, tag, ...args) => {
41-
this.parent[method](tag, ...args);
42-
return this
43-
}
44-
_tag = (color, string) => {
45-
return (string ? (chalk.inverse(color(bold(string))) + ' ') : '') + color(this.label)
46-
}
4762
get [Symbol.toStringTag]() {
4863
return this.label
4964
}

logs/logs.mjs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,48 @@ export function timestamp (d = new Date()) {
1414
.slice(0, -3)
1515
}
1616

17+
const serializer = (k, v) => (typeof v === 'bigint') ? String(v) : v
18+
const toJSON = data => JSON.stringify(data, serializer)
19+
1720
export class Console extends defineCallable(function log(...args){
1821
this.log(...args)
1922
}) {
2023

2124
constructor (label, options = {}) {
2225
super()
23-
this.label = options.label ?? label ?? this.label ?? ''
26+
this.label = options.label ?? label ?? this.label ?? ''
2427
this.parent = options.parent ?? console
28+
this._print = options.json
29+
? (method, tag, message) => {
30+
this.parent[method](toJSON({ logTag: tag, logMethod: method, logMessage: message }))
31+
return this
32+
}
33+
: (method, tag, ...args) => {
34+
this.parent[method](tag, ...args)
35+
return this
36+
}
37+
this._tag = (options.color && !options.json)
38+
? (color, string) => {
39+
const tag1 = (string ? (chalk.inverse(color(bold(string))) + (this.label ? ' ' : '')) : '')
40+
const tag2 = (this.label ? color(this.label) : '')
41+
return tag1 + tag2
42+
}
43+
: (color, string) => {
44+
const tag1 = string
45+
const tag2 = this.label
46+
return tag1 + tag2
47+
}
2548
hideProperties(this,
26-
'label', 'tags', 'tag', '_tag', 'parent', 'sub',
49+
'label', 'tags', 'tag', '_tag', '_print', 'parent', 'sub',
2750
'br', 'log', 'info', 'warn', 'error', 'debug', 'trace', 'table',
2851
'_print',
2952
)
3053
}
3154

3255
label
33-
3456
parent
57+
_print
58+
_tag
3559

3660
sub = (label, options = {}) => new SubConsole(label, { ...options, parent: this })
3761
br = () => { this.parent.log(); return this }
@@ -46,16 +70,6 @@ export class Console extends defineCallable(function log(...args){
4670
this._print('table', ...args)
4771
}
4872

49-
_print = (method, tag, ...args) => {
50-
this.parent[method](tag, ...args);
51-
return this
52-
}
53-
54-
_tag = (color, string) => {
55-
const tag1 = (string ? (chalk.inverse(color(bold(string))) + (this.label ? ' ' : '')) : '')
56-
const tag2 = (this.label ? color(this.label) : '')
57-
return tag1 + tag2
58-
}
5973

6074
get [Symbol.toStringTag]() {
6175
return this.label

logs/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hackbg/logs",
3-
"version": "2.3.3",
3+
"version": "3.0.0",
44
"main": "./logs.mjs",
55
"types": "./logs.d.ts",
66
"type": "module",
@@ -29,7 +29,7 @@
2929
},
3030
"scripts": {
3131
"ubik": "npm test && npm run check && ubik",
32-
"test": "true || ganesha-node README.md",
32+
"test": "node test.cjs",
3333
"check": "tsc --noEmit",
3434
"clean": "ubik clean && rm -rf dist types *.dist.*",
3535
"release": "pnpm check && pnpm test && ubik release --access public --otp 123123"

logs/test.cjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
;(async function testLogs () {
2+
const logsMjs = await import('./logs.mjs')
3+
const logsCjs = require('./logs.cjs')
4+
for (const [variant, { Console }] of Object.entries({logsMjs, logsCjs})) {
5+
for (const color of [true, false]) {
6+
for (const json of [true, false]) {
7+
console.log('\nTesting with', { variant, color, json })
8+
const logger = new Console("Testing!", { color, json })
9+
for (const method of ['log', 'info', 'warn', 'error', 'debug', 'trace']) {
10+
logger[method]("hello")
11+
logger[method]({ json: "object", bigint: 100n })
12+
logger.table([["asdf","qwer"],["zxcv","uiop"]])
13+
logger.table([{asdf:"qwer"},{zxcv:"uiop"}])
14+
logger.table({foo:{asdf:"qwer"},bar:{zxcv:"uiop"}})
15+
}
16+
}
17+
}
18+
}
19+
})()

0 commit comments

Comments
 (0)