Skip to content

Commit

Permalink
Merge pull request #55 from NOAA-GSL/IDSSE-578-contd
Browse files Browse the repository at this point in the history
Added parameterization to get_default_log_config with defaults for ra…
  • Loading branch information
paulhamer-noaa committed Feb 13, 2024
2 parents bbbf7ca + 880ac93 commit 6e12e85
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions python/idsse_common/idsse/common/log_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@


def set_corr_id_context_var(
originator: str,
key: Optional[uuid.UUID] = None,
issue_dt: Optional[Union[str, datetime]] = None
originator: str,
key: Optional[uuid.UUID] = None,
issue_dt: Optional[Union[str, datetime]] = None
) -> None:
"""
Build and set correlation ID ContextVar for logging module, based on originator and
Expand Down Expand Up @@ -63,6 +63,7 @@ def get_corr_id_context_var_parts() -> List[str]:

class AddCorrelationIdFilter(logging.Filter):
""""Provides correlation id parameter for the logger"""

def filter(self, record: logging.LogRecord) -> bool:
try:
record.corr_id = corr_id_context_var.get()
Expand All @@ -73,6 +74,7 @@ def filter(self, record: logging.LogRecord) -> bool:

class CorrIdFilter(logging.Filter):
""""Provides correlation id parameter for the logger"""

def __init__(self, corr_id, name: str = "") -> None:
self._corr_id = corr_id
super().__init__(name)
Expand All @@ -86,7 +88,11 @@ class UTCFormatter(logging.Formatter):
converter = time.gmtime


def get_default_log_config(level: str, with_corr_id=True):
def get_default_log_config(level: str,
with_corr_id: bool = True,
host: str = 'localhost',
port: int = 5672,
report_level: str = 'ERROR'):
"""
Get standardized python logging config (formatters, handlers directing to stdout, rabbitmq etc.)
as a dictionary. This dictionary can be passed directly to logging.config.dictConfig:
Expand All @@ -101,6 +107,9 @@ def get_default_log_config(level: str, with_corr_id=True):
Args:
level (str): logging level, such as 'DEBUG'
with_corr_id (bool): whether to include correlation ID in log messages. Default: True
host (str): rabbitmq hostname
port (int): rabbitmq port number
report_level: rabbitmq logging level
"""
set_corr_id_context_var('None', uuid.UUID('00000000-0000-0000-0000-000000000000'))
if with_corr_id:
Expand Down Expand Up @@ -139,18 +148,18 @@ def get_default_log_config(level: str, with_corr_id=True):
},
'rabbit': {
'class': 'python_logging_rabbitmq.RabbitMQHandler',
'host': 'localhost',
'port': 5672,
'host': host,
'port': port,
'formatter': 'standard',
'filters': filter_list,
'level': 'ERROR',
'level': report_level,
'declare_exchange': True,
},
},
'loggers': {
'': {
'level': level,
'handlers': ['default', 'rabbit'],
'handlers': ['default', 'rabbit']
},
}
}

0 comments on commit 6e12e85

Please sign in to comment.