Skip to content

Instantly share code, notes, and snippets.

@davidohana
davidohana / layconf_example.py
Last active May 20, 2020 15:37
LayConf Example usage
from layconf import LayConf
# the default config file is cfg/default.ini unless specified other
# custom_config_file_path is an optional layer
# env_prefix is empty by default, which means that environment variable names will be
# mapped to {section}_{option}
LayConf.init_config(custom_config_file_path="cfg/staging.ini", env_prefix="example")
print("env_name:", LayConf.get("DATABASE", "env_name"))
print("console_enabled:", LayConf.getboolean("LOG", "console_enabled"))
[LOG]
console_enabled = true
console_level = INFO
file_enabled = true
file_rotation_size_mb = 10
file_backup_count = 1000
[DATABASE]
endpoint =
env_name = default
[DATABASE]
env_name = staging
endpoint = staging:3001
logger.info("Hello World")
logger.info("Request from {} handled in {:.3f} ms", socket.gethostname(), 11)
logger.info("Request from {} handled in {:.3f} ms", "127.0.0.1", 33.1)
logger.info("My favorite drinks are {}, {}, {}, {}", "milk", "wine", "tea", "beer")
logger.debug("this is a {} message", logging.getLevelName(logging.DEBUG))
logger.info("this is a {} message", logging.getLevelName(logging.INFO))
logger.warning("this is a {} message", logging.getLevelName(logging.WARNING))
logger.error("this is a {} message", logging.getLevelName(logging.ERROR))
logger.critical("this is a {} message", logging.getLevelName(logging.CRITICAL))
logger.info("Does old-style formatting also work? %s it is, but no colors (yet)", True)
import logging
from colargulog import ColorizedArgsFormatter
root_logger = logging.getLogger()
console_handler = logging.StreamHandler(stream=sys.stdout)
console_format = "%(asctime)s - %(levelname)-8s - %(name)-25s - %(message)s"
colored_formatter = ColorizedArgsFormatter(console_format)
console_handler.setFormatter(colored_formatter)
root_logger.addHandler(console_handler)
import logging
import logging.handlers
import re
class ColorCodes:
grey = "\x1b[38;21m"
green = "\x1b[1;32m"
yellow = "\x1b[33;21m"
red = "\x1b[31;21m"
import logging
import socket
import sys
from colargulog import ColorizedArgsFormatter
from colargulog import BraceFormatStyleFormatter
def init_logging():
root_logger = logging.getLogger()
def retry(func, ex_type=Exception, limit=0, wait_ms=100, wait_increase_ratio=2, logger=None):
"""
Retry a function invocation until no exception occurs
:param func: function to invoke
:param ex_type: retry only if exception is subclass of this type
:param limit: maximum number of invocation attempts
:param wait_ms: initial wait time after each attempt in milliseconds.
:param wait_increase_ratio: increase wait period by multiplying this value after each attempt.
:param logger: if not None, retry attempts will be logged to this logging.logger
:return: result of first successful invocation
service_filter = "service1"
priority=3
batch_size_limit=1000
def get_alert_list():
return alerts_client.get_alerts_list(
service=service_filter,
priority=min_priority_level,
page=1,
pageSize=batch_size_limit
alerts = alerts_client.get_alerts_list(
service=service_filter,
priority=min_priority_level,
page=1,
pageSize=batch_size_limit)