mozill...@lists.mozilla.org
Good day frontend-infra team!
In my experience, every reasonably sized application uses a logging
library of some sort. Even the health dashboard has one hidden one
called `reportOrLog` [1]: it is used to shunt errors from the console
to the "Raven" library (whatever that does). I would like for us to
settle on one for all our javascript projects.
Please submit what you use for logging, debugging and errors so we have
a short list to choose from.
The rest of this email is my submission:
The API I have been using has three functions:
Log.note(reason, data) - log message
Log.warning(reason, data, cause) - log error details, no longer an error
Log.error(reason, data, cause) - slightly simpler way to throw an error
Here is an example of throwing an error:
Log.error(
'{{func}} is an unknown string function for template expansion',
{ func }
);
Here is an example of logging an error, along with the original `cause`:
}catch(cause){
Log.warning("Can not evaluate {{variable|json}}", { variable },
cause);
}
Below is link to a PR with my first attempt [2].
I am agnostic about the names, or the actual library that implements
this functionality, so here are the features I am looking for:
* **support structured logging** - Stop using template literals:
The `reason` and the `data` are kept separate so that the `data` remains
machine readable and can be put into a datastore to be queried. I do not
expect to query logs on the first day, but we should pick a library that
does not interfere with that possibility.
* **chain exceptions** - When I catch an error, and I can't actually
deal with it, I would like to add some context (what is the code
supposed to be doing) and raise a new error. Keep the old error (and
stack trace), and chain it as the `cause`.
* **send detailed warnings** - Sometimes my code CAN deal with the
exception, but it is not ideal. In those cases I might show something to
the user, but I definitely want a full description logged so it can be
diagnosed at some later time.
* **switch error mitigation based on reason** - Sometimes exception
classes are made so the exception handler can switch based on the type
of error. One benefit of
separating `reason` from `data` is the `reason` can be used to perform
that same switch; no special exception classes are needed, and logging
continues to stay lightweight.
* **avoid linting errors** - Using `console.log()` makes the linter
angry! I can imagine there are reasons why you would want to log
information, either for debugging, or for production (eg client-side
query response timings). We make logging easier, at the same time these
methods ensure all logs are shunted to the proper location.
The `note`, `warning` and `error` may not implement these features, but
they allow these features to be implemented.
[1] reportOrLog
https://github.com/mozilla-frontend-infra/firefox-health-dashboard/blob/650ef39b64174d0aa4477c18395108a08127c83a/src/hocs/withErrorBoundary.jsx#L6
[2] add logging
https://github.com/mozilla-frontend-infra/firefox-health-dashboard/pull/355