debug-logfmt 1.2.3 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -9
- package/package.json +32 -21
- package/src/encode.d.ts +8 -0
- package/src/encode.js +15 -0
- package/src/index.d.ts +21 -0
- package/{index.js → src/index.js} +5 -10
package/README.md
CHANGED
|
@@ -6,9 +6,12 @@
|
|
|
6
6
|
|
|
7
7
|
## Highlights
|
|
8
8
|
|
|
9
|
-
- Based on [`debug`](https://www.npmjs.com/package/debug)
|
|
10
|
-
-
|
|
11
|
-
-
|
|
9
|
+
- Based on the popular [`debug`](https://www.npmjs.com/package/debug) module.
|
|
10
|
+
- Lazy level evaluation used logs levels.
|
|
11
|
+
- Level support: `info`, `warn` & `error` based from [RFC 5424](https://datatracker.ietf.org/doc/html/rfc5424).
|
|
12
|
+
- Message formatting Heroku [logfmt](https://brandur.org/logfmt) syntax.
|
|
13
|
+
- Colorized output via [`DEBUG_COLORS`](https://github.com/debug-js/debug#environment-variables) by default.
|
|
14
|
+
- [`debug.duration`](#measurement) for measurement.
|
|
12
15
|
|
|
13
16
|
## Install
|
|
14
17
|
|
|
@@ -16,15 +19,38 @@
|
|
|
16
19
|
$ npm install debug-logfmt --save
|
|
17
20
|
```
|
|
18
21
|
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Multiple levels
|
|
25
|
+
|
|
26
|
+
Given a code like this one:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
const debug = require('debug-logfmt')('metascraper')
|
|
30
|
+
|
|
31
|
+
debug('retry', { url: 'https://kikobeats.com' })
|
|
32
|
+
debug.info('done', { time: Date.now() })
|
|
33
|
+
debug.warn('token expired', { timestamp: Date.now() })
|
|
34
|
+
debug.error('whoops', { message: 'expected `number`, got `NaN`' })
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
You can:
|
|
38
|
+
- Allow all the levels: `DEBUG=debug-logfmt*`
|
|
39
|
+
- Discard specific levels: `DEBUG="*,-metascraper:info*" node example.js`
|
|
40
|
+
|
|
41
|
+
### Measurement
|
|
42
|
+
|
|
43
|
+
Sometimes you need to log the duration of a function:
|
|
44
|
+
|
|
19
45
|
```js
|
|
20
|
-
const
|
|
46
|
+
const { setTimeout } = require('timers/promises')
|
|
21
47
|
|
|
22
|
-
const
|
|
48
|
+
const debug = require('debug-logfmt')('metascraper')
|
|
23
49
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
50
|
+
const duration = debug.duration()
|
|
51
|
+
|
|
52
|
+
setTimeout(1001).then(() => duration.error('timeout!'))
|
|
53
|
+
setTimeout(1100).then(() => duration.info('success'))
|
|
28
54
|
```
|
|
29
55
|
|
|
30
56
|
## API
|
|
@@ -47,6 +73,16 @@ Default: `['debug', 'info', 'warn', 'error']`
|
|
|
47
73
|
|
|
48
74
|
The log levels available.
|
|
49
75
|
|
|
76
|
+
### debug.duration([...args])
|
|
77
|
+
|
|
78
|
+
It returns a function will print the duration in the next call.
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
const duration = debug.duration('query')
|
|
82
|
+
const result = await db.query(query)
|
|
83
|
+
duration(result)
|
|
84
|
+
```
|
|
85
|
+
|
|
50
86
|
## License
|
|
51
87
|
|
|
52
88
|
**debug-logfmt** © [Kiko Beats](https://kikobeats.com), released under the [MIT](https://github.com/Kikobeats/debug-logfmt/blob/master/LICENSE.md) License.<br>
|
package/package.json
CHANGED
|
@@ -2,13 +2,25 @@
|
|
|
2
2
|
"name": "debug-logfmt",
|
|
3
3
|
"description": "debug module using logfmt format",
|
|
4
4
|
"homepage": "https://github.com/Kikobeats/debug-logfmt",
|
|
5
|
-
"version": "1.
|
|
6
|
-
"
|
|
5
|
+
"version": "1.4.0",
|
|
6
|
+
"types": "./src/index.d.ts",
|
|
7
|
+
"main": "src/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.d.ts",
|
|
11
|
+
"default": "./src/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./encode": {
|
|
14
|
+
"types": "./src/encode.d.ts",
|
|
15
|
+
"default": "./src/encode.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
7
18
|
"author": {
|
|
8
19
|
"email": "josefrancisco.verdu@gmail.com",
|
|
9
20
|
"name": "Kiko Beats",
|
|
10
21
|
"url": "https://kikobeats.com"
|
|
11
22
|
},
|
|
23
|
+
"contributors": [],
|
|
12
24
|
"repository": {
|
|
13
25
|
"type": "git",
|
|
14
26
|
"url": "git+https://github.com/Kikobeats/debug-logfmt.git"
|
|
@@ -27,9 +39,8 @@
|
|
|
27
39
|
"winston"
|
|
28
40
|
],
|
|
29
41
|
"dependencies": {
|
|
30
|
-
"@
|
|
31
|
-
"
|
|
32
|
-
"debug-fabulous": "2.0.2",
|
|
42
|
+
"@kikobeats/time-span": "~1.0.5",
|
|
43
|
+
"debug-fabulous": "~2.0.8",
|
|
33
44
|
"pretty-ms": "~7.0.1"
|
|
34
45
|
},
|
|
35
46
|
"devDependencies": {
|
|
@@ -37,25 +48,24 @@
|
|
|
37
48
|
"@commitlint/config-conventional": "latest",
|
|
38
49
|
"@ksmithut/prettier-standard": "latest",
|
|
39
50
|
"ava": "latest",
|
|
51
|
+
"c8": "latest",
|
|
40
52
|
"ci-publish": "latest",
|
|
41
|
-
"coveralls": "latest",
|
|
42
|
-
"debug": "latest",
|
|
43
53
|
"finepack": "latest",
|
|
44
54
|
"git-authors-cli": "latest",
|
|
45
|
-
"git-dirty": "latest",
|
|
46
55
|
"github-generate-release": "latest",
|
|
47
56
|
"nano-staged": "latest",
|
|
48
|
-
"nyc": "latest",
|
|
49
57
|
"simple-git-hooks": "latest",
|
|
50
58
|
"standard": "latest",
|
|
51
59
|
"standard-markdown": "latest",
|
|
52
|
-
"standard-version": "latest"
|
|
60
|
+
"standard-version": "latest",
|
|
61
|
+
"strip-ansi": "6",
|
|
62
|
+
"tinyspawn": "latest"
|
|
53
63
|
},
|
|
54
64
|
"engines": {
|
|
55
65
|
"node": ">= 8"
|
|
56
66
|
},
|
|
57
67
|
"files": [
|
|
58
|
-
"
|
|
68
|
+
"src"
|
|
59
69
|
],
|
|
60
70
|
"license": "MIT",
|
|
61
71
|
"commitlint": {
|
|
@@ -68,18 +78,19 @@
|
|
|
68
78
|
]
|
|
69
79
|
}
|
|
70
80
|
},
|
|
81
|
+
"eslintIgnore": [
|
|
82
|
+
"*.d.ts"
|
|
83
|
+
],
|
|
71
84
|
"nano-staged": {
|
|
72
85
|
"*.js": [
|
|
73
|
-
"
|
|
74
|
-
"
|
|
86
|
+
"prettier-standard",
|
|
87
|
+
"standard --fix"
|
|
75
88
|
],
|
|
76
89
|
"*.md": [
|
|
77
|
-
"git add",
|
|
78
90
|
"standard-markdown"
|
|
79
91
|
],
|
|
80
92
|
"package.json": [
|
|
81
|
-
"finepack"
|
|
82
|
-
"git add"
|
|
93
|
+
"finepack"
|
|
83
94
|
]
|
|
84
95
|
},
|
|
85
96
|
"simple-git-hooks": {
|
|
@@ -88,15 +99,15 @@
|
|
|
88
99
|
},
|
|
89
100
|
"scripts": {
|
|
90
101
|
"clean": "rm -rf node_modules",
|
|
102
|
+
"contributors": "(npx git-authors-cli && npx finepack && git add package.json && git commit -m 'build: contributors' --no-verify) || true",
|
|
91
103
|
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
|
92
|
-
"lint": "standard
|
|
93
|
-
"postrelease": "npm run release:tags && npm run release:github && ci-publish",
|
|
104
|
+
"lint": "standard",
|
|
105
|
+
"postrelease": "npm run release:tags && npm run release:github && (ci-publish || npm publish --access=public)",
|
|
94
106
|
"pretest": "npm run lint",
|
|
95
107
|
"pretty": "prettier-standard index.js {core,test,bin,scripts}/**/*.js --single-quote --print-width 100",
|
|
96
|
-
"release": "
|
|
108
|
+
"release": "standard-version -a",
|
|
97
109
|
"release:github": "github-generate-release",
|
|
98
110
|
"release:tags": "git push --follow-tags origin HEAD:master",
|
|
99
|
-
"test": "
|
|
100
|
-
"update": "ncu -a"
|
|
111
|
+
"test": "c8 ava"
|
|
101
112
|
}
|
|
102
113
|
}
|
package/src/encode.d.ts
ADDED
package/src/encode.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
function stringify (input) {
|
|
2
|
+
if (typeof input !== 'string') input = String(input)
|
|
3
|
+
if (input === '') return '""'
|
|
4
|
+
let encoded = input.split(/\n/g).join(' ')
|
|
5
|
+
if (/["\\]/.test(encoded)) encoded = encoded.replace(/["\\]/g, '\\$&')
|
|
6
|
+
if (/[\s=]/.test(encoded)) encoded = `"${encoded}"`
|
|
7
|
+
return encoded
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
module.exports = obj => {
|
|
11
|
+
let result = ''
|
|
12
|
+
let i = 0
|
|
13
|
+
for (const key in obj) result += (i++ ? ' ' : '') + `${key}=${stringify(obj[key])}`
|
|
14
|
+
return result
|
|
15
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
declare namespace DebugLogfmt {
|
|
2
|
+
interface DebugOptions {
|
|
3
|
+
levels?: string[]
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
interface DurationLogger {
|
|
7
|
+
(...args: any[]): boolean
|
|
8
|
+
error(...args: any[]): boolean
|
|
9
|
+
info(...args: any[]): boolean
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface DebugLogger {
|
|
13
|
+
(...args: any[]): void
|
|
14
|
+
duration(...args: any[]): DurationLogger
|
|
15
|
+
[level: string]: ((...args: any[]) => void) | DurationLogger | ((...args: any[]) => DurationLogger)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare function createDebugLogger(env: string, options?: DebugLogfmt.DebugOptions): DebugLogfmt.DebugLogger
|
|
20
|
+
|
|
21
|
+
export = createDebugLogger
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { encode } = require('@jclem/logfmt2')
|
|
4
3
|
const origDebug = require('debug')
|
|
5
4
|
|
|
6
5
|
const timeSpan = require('@kikobeats/time-span')({
|
|
7
6
|
format: require('pretty-ms')
|
|
8
7
|
})
|
|
9
8
|
|
|
9
|
+
const encode = require('./encode')
|
|
10
|
+
|
|
10
11
|
/**
|
|
11
12
|
* This methods override the default `formatArgs` to don't print diff information.
|
|
12
13
|
*/
|
|
@@ -16,9 +17,7 @@ if (process.env.DEBUG_COLORS === 'false') {
|
|
|
16
17
|
}
|
|
17
18
|
} else {
|
|
18
19
|
origDebug.formatArgs = function formatArgs (args) {
|
|
19
|
-
const colorCode = `\u001B[3${
|
|
20
|
-
this.color < 8 ? this.color : `8;5;${this.color}`
|
|
21
|
-
}`
|
|
20
|
+
const colorCode = `\u001B[3${this.color < 8 ? this.color : `8;5;${this.color}`}`
|
|
22
21
|
const prefix = ` ${colorCode};1m${this.namespace} \u001B[0m`
|
|
23
22
|
args[0] = prefix + args[0].replace(/\n/g, `\n${prefix}`)
|
|
24
23
|
}
|
|
@@ -31,15 +30,11 @@ const LEVELS = ['info', 'warn', 'error']
|
|
|
31
30
|
const createLogger =
|
|
32
31
|
log =>
|
|
33
32
|
(...args) =>
|
|
34
|
-
log(
|
|
35
|
-
args.map(arg => (typeof arg === 'string' ? arg : encode(arg))).join(' ')
|
|
36
|
-
)
|
|
33
|
+
log(args.map(arg => (typeof arg === 'string' ? arg : encode(arg))).join(' '))
|
|
37
34
|
|
|
38
35
|
module.exports = (env, { levels = LEVELS } = {}) => {
|
|
39
36
|
const debug = createLogger(createDebug(env))
|
|
40
|
-
levels.forEach(
|
|
41
|
-
level => (debug[level] = createLogger(createDebug(`${env}:${level}`)))
|
|
42
|
-
)
|
|
37
|
+
levels.forEach(level => (debug[level] = createLogger(createDebug(`${env}:${level}`))))
|
|
43
38
|
|
|
44
39
|
debug.duration = (...args) => {
|
|
45
40
|
const duration = timeSpan()
|