When it comes to logging message text while preserving the original context (class, line number), console output does the job perfectly with console.log.bind(console). However, the challenge arises when I also need to send the message to an HTTP server for logging.
For example 1: By using a getter & bind, I can pass console.log to the caller and retrieve the message and context (class, line number). But in this scenario, the getter function won't have access to arguments at runtime as they are passed directly to console. Therefore, I am unable to log the message.
Example 2: If I switch from a getter to a function, I will have access to arguments (i.e., the message) which I can then send to another location while simultaneously logging it using console.log. However, the console.log line will display my logger class instead of being bound.
Here is Example 1:
get debug() {
let message = Array.prototype.slice.call(arguments);
// send message over to some http server
// ...
return console.log.bind(console);
}
And here is Example 2:
public debug() {
let message = Array.prototype.slice.call(arguments);
// send message over to some http server
// ...
console.log.apply(console, args);
}
My question is whether there is a way to maintain the context for console.log while also obtaining the log message for further manipulation (such as sending it to an HTTP server).