def colourized_formatter(fmt="", use_colors=True): try: from uvicorn.logging import ColourizedFormatter return ColourizedFormatter(fmt, use_colors=use_colors) except ImportError: return logging.Formatter(fmt)
def configure_logger(config=None, loggers=("quetz", "urllib3.util.retry", "alembic")): """Get quetz logger""" if hasattr(config, "logging_level"): log_level = config.logging_level else: log_level = "INFO" if hasattr(config, "logging_file"): filename = config.logging_file else: filename = None log_level = os.environ.get("QUETZ_LOG_LEVEL", log_level) level = getattr(logging, log_level.upper()) try: from uvicorn.logging import ColourizedFormatter formatter = ColourizedFormatter( fmt="%(levelprefix)s [%(name)s] %(message)s", use_colors=True) except ImportError: formatter = logging.Formatter('%(levelname)s [%(name)s] %(message)s') ch = logging.StreamHandler() ch.setFormatter(formatter) if filename: fh = logging.FileHandler(filename) file_formatter = logging.Formatter( '%(asctime)s %(levelname)s %(name)s %(message)s') fh.setFormatter(file_formatter) else: fh = None # configure selected loggers for logger_name in loggers: logger = logging.getLogger(logger_name) logger.setLevel(level) # add the handlers to the logger logger.addHandler(ch) if fh: logger.addHandler(fh)
"""Implements logger for application """ import os import logging from uvicorn.logging import ColourizedFormatter logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) standard = logging.StreamHandler() standard.setLevel(logging.INFO) formatter = ColourizedFormatter('%(asctime)s - [%(levelname)s] : %(message)s') standard.setFormatter(formatter) logger.addHandler(standard)
#!/usr/bin/env python3 from glob import glob import logging import os import sys from fastapi import FastAPI, File, UploadFile from fastapi.responses import FileResponse from fastapi.middleware.cors import CORSMiddleware from uvicorn.logging import ColourizedFormatter logger = logging.getLogger('traffic-editor-file-server') console_formatter = ColourizedFormatter("{levelprefix:<8} {name}: {message}", style="{", use_colors=True) logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setFormatter(console_formatter) logger.addHandler(ch) logger.debug("hello world") if 'MAP_DIR' not in os.environ: logger.error("MAP_DIR must be set in the environment") sys.exit(1) map_dir = os.getenv("MAP_DIR") logger.info(f"serving from {map_dir}") # spin through MAP_DIR and use the first .building.yaml file we see map_filenames = glob(os.path.join(map_dir, "*.building.yaml"))
) logging_file_handler.setFormatter( logging.Formatter( "{levelname:<8} in {name:<16} at {asctime:<16}: {message}", "%Y-%m-%d %H:%M:%S", style="{", )) logging_file_handler.setLevel("DEBUG") root_logger.addHandler(logging_file_handler) ### console handler logging_console_handler = logging.StreamHandler() logging_console_handler.setFormatter( ColourizedFormatter( "{levelprefix:<8} @ {name:<10} : {message}", "%Y-%m-%d %H:%M:%S", style="{", use_colors=True, )) logging_console_handler.setLevel(log_level) root_logger.addHandler(logging_console_handler) # create app logger.info("initializing PaperBack app") ## search plugins names: Set[str] = set() plugin_type2class: Dict[str, List[Any]] = { "AUTH": [], "DOCS": [], "MISC": [], }