I personally have a module to import that logs to the console in dev and in prod it will log to Rollbar (also free to start). It also has different log levels so that could turn on DEBUG_LOG=true
and it will spew out extra info while on dev only. I don’t have it open sourced yet but it’s basically a simple wrapper like this:
import Logger from "utils/logger/client";
// verbose data for when things go south
Logger.debug("some extra info here", args);
// log info for audit purposes
Logger.info("clicked pay now");
Logger.info("clicked submit");
Logger.info("payment succeeded", {userId: id, amount: 25});
// use this in error callback/try-catch
Logger.error("Stripe failed", errObj);
Here's a fresh start that's not finished, but you get the idea. It just decouples the logging from Rollbar and also adds telemetry data (like info logs) so that you can see that the user clicked "pay now" and then the error was thrown. This gives you a stream of events that led up until the error. If you're using Redux you can even send along your redux store for further analysis. Mixing in Meteor.user data could be handy too (though wrap these in a try catch so you don't cause an error trying to log something). I don't have the dev/prod checks but those can be added pretty easy with if/else.
I would also prob just duplicate this for the server and have a separate module and then adjust it for what makes sense on the server.
// creates a cache that can be sent along with a rollbar error
const telemetry = [];
export default {
info(message, metadata) {
console.log("[INFO] " + message, metadata);
cacheTelemetry("info", message, metadata);
// TODO log to Rollbar
},
warn(message, metadata) {
console.log("[WARN] " + message, metadata);
cacheTelemetry("warn", message, metadata);
// TODO log to Rollbar
},
debug(message, metadata) {
console.debug("[DEBUG] " + message, metadata);
cacheTelemetry("debug", message, metadata);
// TODO log to Rollbar
},
error(message, metadata) {
console.log("[ERROR] " + message, metadata);
cacheTelemetry("error", message, metadata);
// TODO log to Rollbar
},
};
function cacheTelemetry(level, message, metadata) {
try {
telemetry.push({level, message, metadata});
// TODO only keep X messages
}
catch (e) {
console.log("cache failed");
}
}