Logging Utilites Python Code

title: Contents 
style: nestedList # TOC style (nestedList|inlineFirstLevel)
minLevel: 1 # Include headings from the specified level
maxLevel: 4 # Include headings up to the specified level
includeLinks: true # Make headings clickable
debugInConsole: false # Print debug info in Obsidian console

Overview

Sources:

  • **

Code

  • src/utils/logging_utils.py:
"""Logging Utilities"""
import logging
from typing import Optional
 
from settings import Settings
 
 
def get_logger(settings: Optional[Settings] = None) -> Logger:
    """Get Logger
    :param: settings: (Optional)
    :result: Logger
    """
    
    logger = logging.getLogger("<project>")
    logger.setLevel(settings.log_level if settings else "INFO")
 
    # Clear the existing handlers
    for handler in logger.handlers[:]:
        logger.removeHandler(handler)
 
    handler = logging.StreamHandler()
    handler.setLevel(settings.log_level if settings else "INFO")
    formatter = logging.Formatter(
        "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    )
    handler.setFormatter(formatter)
    logger.addHandler(handler)
 
    return logger

where src/settings.py is:

import typing
import os
 
from enum import Enum
from functools import lru_cache
from pydantic_yaml import YamlModel
 
 
class Settings(YamlModel):
  api_key: typing.Optional[str] = None
  my_other_setting: typing.Optional[str] = None
 
 
@lru_cache()
def get_settings_from_file(path: str = "config.yml") -> Settings:
	"""Get Settings From File
	:param: path: (String) Defaults to 'config.yml'
	:returns: Settings Object
	"""
	settings = Settings.parse_file(path)
	return settings
	

Details

About

This note is about …

See Also


Appendix

Note created on 2024-04-15 and last modified on 2024-04-15.

LIST FROM [[Python - Logging Utilites]] AND -"CHANGELOG" AND -"04-RESOURCES/Code/Python/Python - Logging Utilites"

(c) No Clocks, LLC | 2024