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

Flush stdout kwarg #39

Merged
merged 2 commits into from
May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion dpytools/logging/logger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import sys
from datetime import datetime, timezone
from typing import Dict, List, Optional

Expand All @@ -8,12 +9,13 @@


class DpLogger:
def __init__(self, namespace: str):
def __init__(self, namespace: str, flush_stdout_after_log_entry: bool = False):
"""
Simple python logger to create structured logs in keeping
with https://github.com/ONSdigital/dp-standards/blob/main/LOGGING_STANDARDS.md

namespace: (required) the namespace for the app in question
flush_stdout_after_log_entry: (optional) whether to flush the stdout buffer after each log entry
"""

logging.getLogger().addHandler(logging.StreamHandler())
Expand All @@ -28,6 +30,7 @@ def __init__(self, namespace: str):

self._logger = structlog.get_logger()
self.namespace = namespace
self.flush_stdout_after_log_entry = flush_stdout_after_log_entry

def _log(
self,
Expand All @@ -54,6 +57,9 @@ def _log(

self._logger.log(**log_event)

if self.flush_stdout_after_log_entry:
sys.stdout.flush()

def debug(self, event: str, raw: str = None, data: Dict = None):
"""
Log at the debug level.
Expand Down
Loading