-
Notifications
You must be signed in to change notification settings - Fork 5
Add structured logs #60
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
base: main
Are you sure you want to change the base?
Conversation
381a613
to
0f63865
Compare
Organizing log data in a structured format makes easier to read and analyze them. With this commit, log messages will always have a structured format. The default mode is to print the messages to the console in a plain format, but it can also be configured to print them in JSON format. We have added structured logs to GrimoireLab using the 'structlog' package. By default, tests will run silent. If developers want to get log messages printed to the console, activate the environment variable 'GRIMOIRELAB_TESTING_VERBOSE'. Signed-off-by: Santiago Dueñas <[email protected]>
Some messages were updated to follow the structured format. Signed-off-by: Santiago Dueñas <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I include some comments about the handlers and future improvements.
task = job_db.task | ||
|
||
logger.error(f"Job #{job_db.job_id} (task: {task.task_id}) failed; error: {value}") | ||
logger.info("job failed", job_uuid=job_db.uuid, task_uuid=task.uuid, error=value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the logs in _on_failure_callback
use info
. Shouldn’t they be error
instead, for easier filtering?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I though of it but I wasn't sure because a failed job doesn't implies an error in the platform but probably, for filtering, it will be easier to get them. I'll change it.
consumer.start(burst=burst) | ||
except Exception as e: | ||
logging.error(f"Consumer {consumer_class.__name__} failed: {e}") | ||
logger.error(f"Consumer {consumer_class.__name__} failed: {e}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logger.error(f"Consumer {consumer_class.__name__} failed: {e}") | |
logger.error(f"Consumer {consumer_class.__name__} failed", exc_info=ex) |
"django.middleware.csrf.CsrfViewMiddleware", | ||
"django.contrib.auth.middleware.AuthenticationMiddleware", | ||
"django.contrib.messages.middleware.MessageMiddleware", | ||
"django.middleware.clickjacking.XFrameOptionsMiddleware", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If, at some point, we want to use a handler to store the logs somewhere, we might need to consider using the middleware, or if we suppress the uwsgi logs we can also use it.
"django.middleware.clickjacking.XFrameOptionsMiddleware", | |
"django_structlog.middlewares.RequestMiddleware", | |
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add it.
"grimoirelab.core.scheduler": { | ||
"handlers": ["scheduler"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are 2 handlers for the module grimoirelab.core.scheduler
, and messages are duplicated. But I don't understand why it is json
format and not not_structured
, but if I disable the second line I only get the default
handler.
[2025-10-10 10:37:38,664 - grimoirelab.core.scheduler.scheduler - INFO] - {'job_uuid': 'e6dfc352-4a71-421c-a4a3-3ee6260caba7', 'job_args': {'datasource_type': 'git', 'datasource_category': 'commit', 'events_stream': 'events', 'stream_max_length': 1000000, 'job_args': {'latest_items': False, 'gitpath': '/home/jjmerchante/.perceval/https://github.com/chaoss/grimoirelab-perceval.git-git', 'uri': 'https://github.com/chaoss/grimoirelab-perceval.git'}}, 'task_uuid': 'd8d499db-4458-4ae2-839f-b06c0ed1bef8', 'queue': 'eventizer_jobs', 'scheduled_at': datetime.datetime(2025, 10, 10, 10, 37, 38, 648956, tzinfo=datetime.timezone.utc), 'event': 'job scheduled', 'timestamp': '2025-10-10T10:37:38.664211Z', 'level': 'info', 'logger': 'grimoirelab.core.scheduler.scheduler'}
2025-10-10T10:37:38.664211Z [info ] [grimoirelab.core.scheduler.scheduler] job scheduled job_args={'datasource_type': 'git', 'datasource_category': 'commit', 'events_stream': 'events', 'stream_max_length': 1000000, 'job_args': {'latest_items': False, 'gitpath': '/home/jjmerchante/.perceval/https://github.com/chaoss/grimoirelab-perceval.git-git', 'uri': 'https://github.com/chaoss/grimoirelab-perceval.git'}} job_uuid=e6dfc352-4a71-421c-a4a3-3ee6260caba7 queue=eventizer_jobs scheduled_at=2025-10-10 10:37:38.648956+00:00 task_uuid=d8d499db-4458-4ae2-839f-b06c0ed1bef
Organizing log data in a structured format makes easier to read and analyze them.
With this PR, log messages will always have a structured format. The default mode is to print the messages to the
console in a plain format, but it can also be configured to print them in JSON format.
We have added structured logs to GrimoireLab using the 'structlog' package.