What are loggers?

I have seen a few repositories implement so-called “loggers”. It looks like they are writing some data and information to a file with process.std.out.

What is the purpose of these loggers? I suspect they are used to doing some kind of post-mortem after the program has run and track down bugs, but why not use an SQL database instead of writing to a text file?

Just tools for logging. The console object in browsers & node has a logger: console.log is the most commonly used function, but it also has functions for the rest of the normal log levels (console.info, console.debug, console.warn, console.error).

If there something on the program you want to record (“this thing worked” or “this thing didn’t work”) you just add a log there and the logger will print the message to wherever you want to log to. Contrived example:

async function getUser(id) {
  try {
    const resp = await fetch(`example.com/api/users/${id}`);
    if (!resp.ok) {
      throw new Error(`Request failed with status code ${resp.status}`);
    }
    const user = resp.json();
    console.info(`Succesfully fetched user data for ID ${id}`);
    return user;
  } catch (err) {
    console.error(err.message);
  }
}

Well, you can write logs to wherever you want – the console, or a text file or whatever. Why not use a text file? It’s simple and easy and it works perfectly well

I mean, if you’ve got loads and loads and loads of logs and you have some complex needs and you want to pay the cost in additional complexity, sure, use a database (there are databases specifically built for dealing with logs, doesn’t have to be an SQL database). But just writing to a text file is fine, they’re not going to be kept forever, you use them if there’s been some issue, and with a text file can just use grep to locate the log/s relating to the issue. Most stuff uses a text file (there will be loads of folders on your computer where applications write logs to that get periodically cleared, for example). Web-based systems often use a service that lets you pipe errors/logs/metrics/traces to it (stuff like Sentry or Datadog or whatever)