Coverage for .nox/test-3-9/lib/python3.9/site-packages/nskit/common/logging/config.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.4.2, created at 2024-02-25 17:38 +0000

1"""Manage logging config.""" 

2from pathlib import Path 

3from typing import Any, Dict, Optional 

4 

5from pydantic import AliasChoices, Field 

6 

7from nskit.common.configuration import BaseConfiguration 

8from nskit.common.logging.formatter import BASE_FORMAT_STR, LoggingFormatter 

9 

10JSON_ENV_VAR = 'LOG_JSON' 

11LOGLEVEL_ENV_VAR = 'LOGLEVEL' 

12LOGFILE_ENV_VAR = 'LOGFILE' 

13LOGFORMATSTRING_ENV_VAR = 'LOG_FORMAT' 

14DEFAULT_LOGLEVEL = 'INFO' 

15 

16 

17class LoggingConfig(BaseConfiguration): 

18 """This is a basic config for logging.""" 

19 

20 # TODO setup as settings 

21 level: str = Field(DEFAULT_LOGLEVEL, description='Set the log level for the logger') 

22 logfile: Optional[Path] = Field(None, validation_alias=AliasChoices('logfile', LOGFILE_ENV_VAR), description='Set the log file for the logger') 

23 format_string: str = Field(BASE_FORMAT_STR, validation_alias=AliasChoices('format_string', LOGFORMATSTRING_ENV_VAR), description='Set the log format for the logger') 

24 json_format: bool = Field(True, validation_alias=AliasChoices('json_format', JSON_ENV_VAR), serialization_alias='json', description='Output JSON Logs', alias='json') 

25 extra: Dict[str, Any] = Field(default_factory=dict, description="Extra kwargs") 

26 

27 @property 

28 def formatter(self): 

29 """Return the logging formatter for the format string.""" 

30 return LoggingFormatter(self.format_string)