DEV Community

Cover image for You need a universal logger right now!
Khokon M.
Khokon M.

Posted on

You need a universal logger right now!

How many loggers have you set up so far to keep track of events in your different projects? If you took more than a couple of seconds to answer this question, this article is for you. And if you were unable to answer the question, well, this article is definitely for you.

The Problem

For me, it was a kind of repetitive task that I had to set up loggers for every project. Sure enough, there are a good number of npm packages for logging available. But that still didn't satisfy me. Because I still need to visit each project to check its log. And suddenly, this started feeling like a burden to me. So I had to come up with a solution for this.

The Solution

So, what's the solution? The idea was straightforward. I just had to create a centralized logger and use it for all the projects. And let the centralized system handle everything else. Like, separating logs for each project. So I can peacefully focus on building projects only and not on loggers. That was quite fun, so here I am to share the journey as my #devjournal

How My Universal Logger Works?

First, I created a backend that will accept any logs, as long as the request contains an API Key and a log message. The idea is to send logs from all projects to this particular endpoint. And then, I created this interface to manage and handle all the submitted records from different projects. Now, as I'm putting all my logs from the various projects into one place, I had to make sure I could identify which log was coming from which project.

To do that, I created the API key system. So for each project, I will simply create an API key from my interface with just one click. And to supply logs from a project, I will use the particular API key I made for the project.

How I use the logger in different projects

As the primary goal of the whole project was just to simplify managing logs, I had to find the easiest way to use the logger on all projects. For that particular reason, I created an NPM package called kLogs

So, for each project, I install the package from NPM
npm install @khokonm/klogs
And wherever I need to take log, I write a few lines of code.
First, I require the npm package
const kLogs = require("@khokonm/klogs")
and after that, just a few lines of code for any logs. E.g.:

const Logs = new kLogs('MY_API_KEY_FOR_THIS_PROJECT');
const log = {
  "message": "This is a log message",
  "additional_info": {
    aJavascriptObject
  }
};
Logs.prepare(log);
Logs.send();
Enter fullscreen mode Exit fullscreen mode

The message is the only required parameter, but I can pass all the optional parameters like source (string), medium (string), additional info(object) as well.

Conclusion

If you're like me and hate repetitive tasks too, I hope this idea will help you to better organize your logs in your next project. Any suggestions or improvement ideas are highly appreciated.

If you want to help me improve my logger or use my logger for your project. You're most welcome. You can make a pull request to this repo, and I will be more than happy to merge it with my original work.

And if you want to use the interface to manage your logs, please visit logs.khokon.dev

Discussion (0)