How to Improve Firebase Emulator Logs

[ad_1]
Wake up, remove the noise, and improve your logs in the Firebase simulator.
I’m a big fan of Firebase and I use the emulator a lot to develop locally with our social media API. There are many great features of the emulator, such as simulating cloud services, Firestore, and pub/sub. When outputting JSON logs to the console using console.log
the output is tolerable, especially if you format with JSON.stringify({...}, null, "t")
. However, things get worse when you use Firebase’s recommended logger. The Logger is essentially a Google Cloud Logger, which allows you to organize your logs and JSON for easy viewing and searching in the Google Cloud Dashboard.
While the Firebase log output looks good in the Google Cloud Dashboard, things don’t look so good in our terminal output:
Optimizing Firebase Emulator Logs
What can we look for in Firebase emulator logs?
- Formatting of JSON objects when using Logger.
- The color that comes out so we can highlight the mistakes.
- Remove extraneous data that we may not care about, such as “jobs: You are running the Jobs simulator in debug mode (port=9229). This means that the tasks will be performed sequentially rather than concurrently.”
Since Firebase doesn’t “yet” provide these capabilities, let’s build our own enhancements.
We will save the output of the Firebase emulator to a file, monitor the file for changes, process the changes (format, etc), and output the processed data to the console.
- Close the Firebase-Emulator-Logging GitHub repository. This is a Node.js application. Start the routine
npm install
in the designed directory. - Start the Firebase emulator as you normally would and extract the file. For example:
firebase emulators:start > save.txt
ornpm run serve > save.txt
. - Back in the compiled directory, run the Node app with
node index.js --file {file location}
. For example:node index.js --file ./save.txt
- Enjoy the new logs!

Logging parameters have been improved
There are several options when using the Node app. You’ve already seen the -file, but you can also set the output to silent, which means system output starting with “function” or “hosting” is suppressed, and you can turn off nice formatting.
A parameter | Explanation | It is necessary |
– file | Logs source file | yes |
– shut up | Press system logs like “function”, “hosting”, “storage”, and “pubsub” | no |
– very simple | Turn off nice formatting of JSON objects. Default: true | no |
Behind the scenes
If you want to see all the code, go to GitHub, or here’s the index.js file:
import readline from "readline";
import TailFile from "@logdna/tail-file";
import colorizer from "json-colorizer";
const QUIET_STRING = ["functions", "hosting", "storage", "pubsub"];
const quiet = process.argv.indexOf("--quiet");
const prettyOff = process.argv.indexOf("--pretty-off");
const fileIndex = process.argv.indexOf("--file");
if (fileIndex {
console.error("TailFile had an error!", err);
}
);
try {
await tail.start();
const linesplitter = readline.createInterface({
input: tail,
});
linesplitter.on("line", (line) => {
if (
quiet &&
QUIET_STRING.some((str) =>
new RegExp(`(?") && newLine.endsWith("}")) {
const overrideOptions = { ...options };
try {
const json = JSON.parse(newLine.slice(3));
switch (json?.severity) {
case "INFO":
overrideOptions.colors.STRING_KEY = "blue";
overrideOptions.colors.BRACE = "blue";
break;
case "WARNING":
overrideOptions.colors.STRING_KEY = "yellow";
overrideOptions.colors.BRACE = "yellow";
break;
case "ERROR":
overrideOptions.colors.STRING_KEY = "red";
overrideOptions.colors.BRACE = "red";
break;
default:
break;
}
newLine = colorizer(newLine.slice(3), overrideOptions);
} catch (err) {
// ignore
}
}
console.log(newLine);
});
} catch (err) {
console.error("Cannot start. Does the file exist?", err);
}
}
startTail().catch((err) => {
process.nextTick(() => {
throw err;
});
});
Two external NPM packages are used: import TailFile from "@logdna/tail-file";
import colorizer from "json-colorizer";
tail-file is an amazing package that enables the “tail” of files – whenever something changes, an event occurs. JSON colorizer is a package that allows you to specify which JSON elements get a certain color.
About Ayrshare
Ayrshare is a social media API that allows you to publish posts, get analytics, manage comments, and send direct messages to social media directly from your platform. Read more in our social media API documentation.