Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Hebilicious
Last active July 27, 2021 02:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hebilicious/907fe771448364f073b56c0a9ec72216 to your computer and use it in GitHub Desktop.
Save Hebilicious/907fe771448364f073b56c0a9ec72216 to your computer and use it in GitHub Desktop.
Proxy useful use case
import fs from "fs"
import path from "path"
import { inspect } from "util"
const { Console } = console
const makeFile = (name = "result") => fs.createWriteStream(path.join(__dirname, `../${name}.log`))
const inspectOptions = { maxArrayLength: null, depth: null }
const FileLogger = new Console({ stdout: makeFile(), stderr: makeFile("err"), inspectOptions })
const Logger = new Proxy(
{
log: thing => console.log(inspect(thing, { ...inspectOptions, colors: true })),
error: thing => console.error(thing)
},
{
get: (target, property) => (...things) => {
FileLogger[property](`[${new Date()}] |${property.toString()}|`, ...things)
return target[property](...things)
}
}
)
Logger.log({ message: "Hello World" })
Logger.log("String")
Logger.error(new Error("Big Error"))
Logger.log(["thing", 1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment