Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logger #1797

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

logger #1797

wants to merge 6 commits into from

Conversation

riccardobl
Copy link
Member

@riccardobl riccardobl commented Jan 4, 2025

Description

A zero dependency logger with extensible attachments.
It is like log4js but a single file with some performance oriented nuances.

It comes with two attachments:

  • ConsoleLogAttachment : just a console.* passthrough
  • JSONLogAttachment : sends json logs via http to a collector

The log collector

The advantage of using a log collector is that it can merge, store, display and filter logs from the backend, workers and multiple frontend-users, in a single place, so it easier to follow the execution flow and filter exactly what is needed.

There are several log collectors that could be used, but they all have webuis and complex querying.
I wanted to keep things simple and just use the terminal, so i've built logpipe, and added it to the docker-compose, it basically streams colored logs in realtime to wscat (or any other ws clients).

It also supports some simple filtering features that can be changed by typing commands in the terminal:

filter=csv-list,of,tags
level=<MIN_LEVEL> (eg. level=INFO level=ERROR...etc)

The logger

The logger can be instantiated with

import { getLogger } from '@/lib/logger'
const LOGGER = getLogger("<logger name>", ["optional", "tags", "that can be used for filtering"])

every file should have its own logger instance.

And it can be used with

LOGGER.trace(...)
LOGGER.debug(...)
LOGGER.info(...)
LOGGER.warn(...)
LOGGER.error(...)
LOGGER.fatal(...)

the lazy versions of these methods use a function to generate the logs

default attachment

In production:

  • ConsoleLogAttachment

In development:

  • ConsoleLogAttachment
  • JSONLogAttachment configured to connect to the sndev logpipe container

Screenshots

image

Additional Context

Was anything unclear during your work on this PR? Anything we should definitely take a closer look at?

Checklist

Are your changes backwards compatible? Please answer below:

On a scale of 1-10 how well and how have you QA'd this change and any features it might affect? Please answer below:

For frontend changes: Tested on mobile, light and dark mode? Please answer below:

Did you introduce any new environment variables? If so, call them out explicitly here:

@riccardobl riccardobl force-pushed the logs branch 4 times, most recently from 9adacd5 to f32d69e Compare January 5, 2025 15:28
@riccardobl riccardobl marked this pull request as ready for review January 7, 2025 23:38
Copy link
Member

@huumn huumn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Soxasora said they already found this useful for debugging the service worker. Nice job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants