backtrace-logging
Advanced tools
Comparing version 0.1.3 to 0.2.0
48
index.js
import BufferedFunction, { BufferQueue } from 'fn-buffer'; | ||
/** | ||
* @property {fn} fn | ||
*/ | ||
export default class BacktraceLogging { | ||
@@ -14,14 +17,8 @@ /** | ||
this.store = opts?.store ?? new Map(); | ||
for (const event of this.queue?.constructor?.events ?? []) { | ||
this.queue.on(event, data => this.emit(event, data)); | ||
} | ||
} | ||
/** | ||
* Patch a function to buffer its calls | ||
* @param {function} fn Function to patch | ||
* @param {object} [opts] options for fn-buffer | ||
* @returns {function} The patched function | ||
* Example: | ||
* ```js | ||
* console.log = fn(console.log) | ||
* ``` | ||
*/ | ||
/** @type {fn} */ | ||
fn = (fn, opts) => { | ||
@@ -85,2 +82,33 @@ if (this.store.has(fn)) return this.store.get(fn); | ||
} | ||
on = (event, listener) => { | ||
if (!this.listeners) this.listeners = []; | ||
this.listeners.push([event, listener]); | ||
} | ||
emit = (event, data) => { | ||
for (const [e, listener] of this.listeners ?? []) { | ||
if (event === e) listener(data); | ||
} | ||
} | ||
tee = (stream, format = JSON.stringify) => { | ||
this.on('push', data => { | ||
const [fn, , args] = data[0]; | ||
const fnName = fn.name ? `[${fn.name}]` : ''; | ||
const message = format([fnName, ...args]); | ||
stream.write(message); | ||
}); | ||
} | ||
} | ||
/** | ||
* @callback fn | ||
* Patch a function to buffer its calls | ||
* @param {function} fn Function to patch | ||
* @param {object} [opts] options for fn-buffer | ||
* @returns {function} The patched function | ||
* Example: | ||
* ```js | ||
* console.log = fn(console.log) | ||
* ``` | ||
*/ |
import util from 'util'; | ||
import process from 'process'; | ||
import fs from 'fs'; | ||
import BacktraceLogging from './index.js' | ||
@@ -27,4 +28,10 @@ import { arrify, getMeta } from './utils.js' | ||
process.on('beforeExit', code => code && bl.flush()); | ||
if (meta.file) file(meta.file, bl); | ||
} | ||
export function file(file, b = bl, format = util.format) { | ||
const stream = fs.createWriteStream(file, { flags: 'a' }); | ||
b.tee(stream, a => format(...a, '\n')); | ||
} | ||
/** | ||
@@ -31,0 +38,0 @@ * @typedef meta |
{ | ||
"name": "backtrace-logging", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "Hide unnecessary logs until an error occurs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -105,2 +105,3 @@ # backtrace-logging | ||
* **`meta.process`** `[array]` Keys to patch in [process] (`stdout|stderr`) | ||
* **`meta.file`** `[string]` File to append skipped logs to | ||
@@ -107,0 +108,0 @@ ### Auto Register |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
9388
183
128
2