Package: conflog
PyconflogPyconflog library provides Python logging setup via environment variables and configuration files.Installationpip3 install pyconflogUsageCreate a configuration file, e.g.conflog.yaml:---
handlers: "stream,file"
datefmt: "%Y-%m-%d %H:%M:%S"
filename: "conflog.log"
filemode: "w"
format: "[SOMEAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s"
level: "info"
extras:
  env: "dev"
  id: "123"And then use it in your Python code:from conflog import Conflog

cfl = Conflog(conf_files=['conflog.yaml'])
logger = cfl.get_logger('somename')
logger.debug('Some debug message')
logger.info('Some info message')
logger.critical('Some critical message')It will write the log messages to stdout and fileconflog.log:[SOMEAPP] [dev-123] 2023-06-07 10:49:01 INFO Some info message
[SOMEAPP] [dev-123] 2023-06-07 10:49:52 CRITICAL Some critical messageIf you specify environment variables configuration, it will overwrite the configuration files:import os
from conflog import Conflog

os.environ['CONFLOG_FORMAT'] = '[ENVAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s'
cfl = Conflog(conf_files=['conflog.yaml'])
logger.debug('Some debug message')
logger.info('Some info message')
logger.critical('Some critical message')It will write the log messages using the format fromCONFLOG_FORMATenvironment variable:[ENVAPP] [dev-123] 2023-06-07 10:49:01 INFO Some info message
[ENVAPP] [dev-123] 2023-06-07 10:49:52 CRITICAL Some critical messageAnd if you specify configuration dictionary, it will overwrite everything else:import os
from conflog import Conflog

os.environ['CONFLOG_FORMAT'] = '[ENVAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s'
cfl = Conflog(
    conf_files=['conflog.yaml'],
    conf_dict={'format': '[DICTAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s'})
logger.debug('Some debug message')
logger.info('Some info message')
logger.critical('Some critical message')It will write the log messages using the format from configuration dictionary:[DICTAPP] [dev-123] 2023-06-07 10:49:01 INFO Some info message
[DICTAPP] [dev-123] 2023-06-07 10:49:52 CRITICAL Some critical messageConfigurationConfiguration properties:PropertyDescriptionDefaultExamplehandlersComma separated list of handlers, supported values arestreamandfilestreamstream,filedatefmtDate format%d-%b-%y %H:%M:%S%Y-%m-%d %H:%M:%SfilenameLog file nameconflog.logsomeconflog.logfilemodeLog file modewwformatLog message format%(asctime)s --> %(name)s - %(levelname)s - %(message)s[SOMEAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)slevelLog level, supported values aredebug,info,warning,error,criticalinfocriticalextrasExtra fields to be added to log message. It can be comma separated key value pairs with equal separator, or a key value pairs map for JSON and YAML configuration filesNoneenv=dev,id=123Configuration files can be in YAML, JSON, XML, or INI format. Multiple files can be specified in theconf_filesparameter when initialisingConflog, the configuration will be merged in the order of the files, the latter file will overwrites the former file.Environment variables configuration overwrites configuration files' properties. And finally, configuration dictionary overwrite everything else.YAMLExample YAML configuration file:---
handlers: "stream,file"
datefmt: "%Y-%m-%d %H:%M:%S"
filename: "conflog.log"
filemode: "w"
format: "[SOMEAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s"
level: "info"
extras:
  env: "dev"
  id: "123"JSONExample JSON configuration file:{
  "handlers": "stream,file",
  "datefmt": "%Y-%m-%d %H:%M:%S",
  "filename": "conflog.log",
  "filemode": "w",
  "format": "[SOMEAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s",
  "level": "info",
  "extras": {
    "env": "dev",
    "id": "123"
  }
}XMLExample XML configuration file:<?xml version="1.0" encoding="UTF-8"?>
<conflog>
  <handlers>stream,file</handlers>
  <datefmt>%Y-%m-%d %H:%M:%S</datefmt>
  <filename>conflog.log</filename>
  <filemode>w</filemode>
  <format>[SOMEAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s</format>
  <level>info</level>
  <extras>env=dev,id=123</extras>
</conflog>INIExample INI configuration file:[conflog]
handlers: stream,file
datefmt: %%Y-%%m-%%d %%H:%%M:%%S
filename: conflog.log
filemode: w
format: [SOMEAPP] [%%(env)s-%%(id)s] %%(asctime)s %%(levelname)s %%(message)s
level: info
extras: env=dev,id=123Environment VariablesExample configuration environment variables:CONFLOG_HANDLERS="stream,file"
CONFLOG_DATEFMT="%Y-%m-%d %H:%M:%S"
CONFLOG_FILENAME="conflog.log"
CONFLOG_FILEMODE="w"
CONFLOG_FORMAT="[SOMEAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s"
CONFLOG_LEVEL="info"
CONFLOG_EXTRAS="env=dev,id=123"ColophonDeveloper's GuideBuild reports:Lint reportCode complexity reportUnit tests reportTest coverage reportIntegration tests reportAPI Documentation