Last active
July 27, 2021 02:06
-
-
Save Hebilicious/907fe771448364f073b56c0a9ec72216 to your computer and use it in GitHub Desktop.
Proxy useful use case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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