conflog


Nameconflog JSON
Version 2.0.1 PyPI version JSON
download
home_pagehttps://github.com/cliffano/pyconflog
SummaryPython logging setup via environment variables and configuration files
upload_time2024-11-09 13:22:13
maintainerNone
docs_urlNone
authorCliffano Subagio
requires_python<4.0,>=3.10
licenseApache-2.0
keywords log logger logging config configuration environment envvar
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            <img align="right" src="https://raw.github.com/cliffano/pyconflog/main/avatar.jpg" alt="Avatar"/>

[![Build Status](https://github.com/cliffano/pyconflog/workflows/CI/badge.svg)](https://github.com/cliffano/pyconflog/actions?query=workflow%3ACI)
[![Security Status](https://snyk.io/test/github/cliffano/pyconflog/badge.svg)](https://snyk.io/test/github/cliffano/pyconflog)
[![Published Version](https://img.shields.io/pypi/v/conflog.svg)](https://pypi.python.org/pypi/conflog)
<br/>

Conflog
-------

Conflog library provides Python logging setup via environment variables and configuration files.

Installation
------------

    pip3 install conflog

Usage
-----

Create 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 file `conflog.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 message

If 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 from `CONFLOG_FORMAT` environment 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 message

And 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 message

Configuration
-------------

Configuration properties:

| Property | Description | Default | Example |
| -------- | ----------- | ------- | ------- |
| handlers | Comma separated list of handlers, supported values are `stream` and `file` | `stream` | `stream,file` |
| datefmt | Date format | `%d-%b-%y %H:%M:%S` | `%Y-%m-%d %H:%M:%S` |
| filename | Log file name | `conflog.log` | `someconflog.log` |
| filemode | Log file mode | `w` | `w` |
| format | Log message format | %(asctime)s --> %(name)s - %(levelname)s - %(message)s | `[SOMEAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s` |
| level | Log level, supported values are `debug`, `info`, `warning`, `error`, `critical` | `info` | `critical` |
| extras | Extra 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 files | None | `env=dev,id=123` |

Configuration files can be in YAML, JSON, XML, or INI format. Multiple files can be specified in the `conf_files` parameter when initialising `Conflog`, 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.

### YAML

Example 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"

### JSON

Example 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"
      }
    }

### XML

Example 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>

### INI

Example 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=123

### Environment Variables

Example 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"

FAQ
---

*Q: Why am I getting duplicated log messages?*

A: You might be getting the same log message displayed multiple times when there are other libraries adding handlers to the logger. You can clear those handlers prior to getting a logger from Conflog:

    from conflog import Conflog

    cfl = Conflog(conf_files=['conflog.yaml'])
    cfl.close_logger_handlers()
    logger = cfl.get_logger('somename')

Colophon
--------

[Developer's Guide](https://cliffano.github.io/developers_guide.html#python)

Build reports:

* [Lint report](https://cliffano.github.io/pyconflog/lint/pylint/index.html)
* [Code complexity report](https://cliffano.github.io/pyconflog/complexity/wily/index.html)
* [Unit tests report](https://cliffano.github.io/pyconflog/test/pytest/index.html)
* [Test coverage report](https://cliffano.github.io/pyconflog/coverage/coverage/index.html)
* [Integration tests report](https://cliffano.github.io/pyconflog/test-integration/pytest/index.html)
* [API Documentation](https://cliffano.github.io/pyconflog/doc/sphinx/index.html)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/cliffano/pyconflog",
    "name": "conflog",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": "log, logger, logging, config, configuration, environment, envvar",
    "author": "Cliffano Subagio",
    "author_email": "cliffano@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/79/79/7f1b0bca8bdc993ee8975bcf2bf5ce81caff99b384eebf57f06fc9c96c22/conflog-2.0.1.tar.gz",
    "platform": null,
    "description": "<img align=\"right\" src=\"https://raw.github.com/cliffano/pyconflog/main/avatar.jpg\" alt=\"Avatar\"/>\n\n[![Build Status](https://github.com/cliffano/pyconflog/workflows/CI/badge.svg)](https://github.com/cliffano/pyconflog/actions?query=workflow%3ACI)\n[![Security Status](https://snyk.io/test/github/cliffano/pyconflog/badge.svg)](https://snyk.io/test/github/cliffano/pyconflog)\n[![Published Version](https://img.shields.io/pypi/v/conflog.svg)](https://pypi.python.org/pypi/conflog)\n<br/>\n\nConflog\n-------\n\nConflog library provides Python logging setup via environment variables and configuration files.\n\nInstallation\n------------\n\n    pip3 install conflog\n\nUsage\n-----\n\nCreate a configuration file, e.g. `conflog.yaml`:\n\n    ---\n    handlers: \"stream,file\"\n    datefmt: \"%Y-%m-%d %H:%M:%S\"\n    filename: \"conflog.log\"\n    filemode: \"w\"\n    format: \"[SOMEAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s\"\n    level: \"info\"\n    extras:\n      env: \"dev\"\n      id: \"123\"\n\nAnd then use it in your Python code:\n\n    from conflog import Conflog\n\n    cfl = Conflog(conf_files=['conflog.yaml'])\n    logger = cfl.get_logger('somename')\n    logger.debug('Some debug message')\n    logger.info('Some info message')\n    logger.critical('Some critical message')\n\nIt will write the log messages to stdout and file `conflog.log`:\n\n    [SOMEAPP] [dev-123] 2023-06-07 10:49:01 INFO Some info message\n    [SOMEAPP] [dev-123] 2023-06-07 10:49:52 CRITICAL Some critical message\n\nIf you specify environment variables configuration, it will overwrite the configuration files:\n\n    import os\n    from conflog import Conflog\n\n    os.environ['CONFLOG_FORMAT'] = '[ENVAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s'\n    cfl = Conflog(conf_files=['conflog.yaml'])\n    logger.debug('Some debug message')\n    logger.info('Some info message')\n    logger.critical('Some critical message')\n\nIt will write the log messages using the format from `CONFLOG_FORMAT` environment variable:\n\n    [ENVAPP] [dev-123] 2023-06-07 10:49:01 INFO Some info message\n    [ENVAPP] [dev-123] 2023-06-07 10:49:52 CRITICAL Some critical message\n\nAnd if you specify configuration dictionary, it will overwrite everything else:\n\n    import os\n    from conflog import Conflog\n\n    os.environ['CONFLOG_FORMAT'] = '[ENVAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s'\n    cfl = Conflog(\n        conf_files=['conflog.yaml'],\n        conf_dict={'format': '[DICTAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s'})\n    logger.debug('Some debug message')\n    logger.info('Some info message')\n    logger.critical('Some critical message')\n\nIt will write the log messages using the format from configuration dictionary:\n\n    [DICTAPP] [dev-123] 2023-06-07 10:49:01 INFO Some info message\n    [DICTAPP] [dev-123] 2023-06-07 10:49:52 CRITICAL Some critical message\n\nConfiguration\n-------------\n\nConfiguration properties:\n\n| Property | Description | Default | Example |\n| -------- | ----------- | ------- | ------- |\n| handlers | Comma separated list of handlers, supported values are `stream` and `file` | `stream` | `stream,file` |\n| datefmt | Date format | `%d-%b-%y %H:%M:%S` | `%Y-%m-%d %H:%M:%S` |\n| filename | Log file name | `conflog.log` | `someconflog.log` |\n| filemode | Log file mode | `w` | `w` |\n| format | Log message format | %(asctime)s --> %(name)s - %(levelname)s - %(message)s | `[SOMEAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s` |\n| level | Log level, supported values are `debug`, `info`, `warning`, `error`, `critical` | `info` | `critical` |\n| extras | Extra 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 files | None | `env=dev,id=123` |\n\nConfiguration files can be in YAML, JSON, XML, or INI format. Multiple files can be specified in the `conf_files` parameter when initialising `Conflog`, the configuration will be merged in the order of the files, the latter file will overwrites the former file.\n\nEnvironment variables configuration overwrites configuration files' properties. And finally, configuration dictionary overwrite everything else.\n\n### YAML\n\nExample YAML configuration file:\n\n    ---\n    handlers: \"stream,file\"\n    datefmt: \"%Y-%m-%d %H:%M:%S\"\n    filename: \"conflog.log\"\n    filemode: \"w\"\n    format: \"[SOMEAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s\"\n    level: \"info\"\n    extras:\n      env: \"dev\"\n      id: \"123\"\n\n### JSON\n\nExample JSON configuration file:\n\n    {\n      \"handlers\": \"stream,file\",\n      \"datefmt\": \"%Y-%m-%d %H:%M:%S\",\n      \"filename\": \"conflog.log\",\n      \"filemode\": \"w\",\n      \"format\": \"[SOMEAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s\",\n      \"level\": \"info\",\n      \"extras\": {\n        \"env\": \"dev\",\n        \"id\": \"123\"\n      }\n    }\n\n### XML\n\nExample XML configuration file:\n\n    <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n    <conflog>\n      <handlers>stream,file</handlers>\n      <datefmt>%Y-%m-%d %H:%M:%S</datefmt>\n      <filename>conflog.log</filename>\n      <filemode>w</filemode>\n      <format>[SOMEAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s</format>\n      <level>info</level>\n      <extras>env=dev,id=123</extras>\n    </conflog>\n\n### INI\n\nExample INI configuration file:\n\n    [conflog]\n    handlers: stream,file\n    datefmt: %%Y-%%m-%%d %%H:%%M:%%S\n    filename: conflog.log\n    filemode: w\n    format: [SOMEAPP] [%%(env)s-%%(id)s] %%(asctime)s %%(levelname)s %%(message)s\n    level: info\n    extras: env=dev,id=123\n\n### Environment Variables\n\nExample configuration environment variables:\n\n    CONFLOG_HANDLERS=\"stream,file\"\n    CONFLOG_DATEFMT=\"%Y-%m-%d %H:%M:%S\"\n    CONFLOG_FILENAME=\"conflog.log\"\n    CONFLOG_FILEMODE=\"w\"\n    CONFLOG_FORMAT=\"[SOMEAPP] [%(env)s-%(id)s] %(asctime)s %(levelname)s %(message)s\"\n    CONFLOG_LEVEL=\"info\"\n    CONFLOG_EXTRAS=\"env=dev,id=123\"\n\nFAQ\n---\n\n*Q: Why am I getting duplicated log messages?*\n\nA: You might be getting the same log message displayed multiple times when there are other libraries adding handlers to the logger. You can clear those handlers prior to getting a logger from Conflog:\n\n    from conflog import Conflog\n\n    cfl = Conflog(conf_files=['conflog.yaml'])\n    cfl.close_logger_handlers()\n    logger = cfl.get_logger('somename')\n\nColophon\n--------\n\n[Developer's Guide](https://cliffano.github.io/developers_guide.html#python)\n\nBuild reports:\n\n* [Lint report](https://cliffano.github.io/pyconflog/lint/pylint/index.html)\n* [Code complexity report](https://cliffano.github.io/pyconflog/complexity/wily/index.html)\n* [Unit tests report](https://cliffano.github.io/pyconflog/test/pytest/index.html)\n* [Test coverage report](https://cliffano.github.io/pyconflog/coverage/coverage/index.html)\n* [Integration tests report](https://cliffano.github.io/pyconflog/test-integration/pytest/index.html)\n* [API Documentation](https://cliffano.github.io/pyconflog/doc/sphinx/index.html)\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Python logging setup via environment variables and configuration files",
    "version": "2.0.1",
    "project_urls": {
        "Documentation": "https://github.com/cliffano/pyconflog",
        "Homepage": "https://github.com/cliffano/pyconflog",
        "Repository": "https://github.com/cliffano/pyconflog"
    },
    "split_keywords": [
        "log",
        " logger",
        " logging",
        " config",
        " configuration",
        " environment",
        " envvar"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "baf329c46e7732158083b9b69b10fb02443710f80c0c7a7203e19678640e465e",
                "md5": "614a15385fa67b021ed1b97d8abdf08e",
                "sha256": "72973aac807d532c7cc4f82fef4cf483acb2fcc37fdd68d59f1e49f29c0c35ea"
            },
            "downloads": -1,
            "filename": "conflog-2.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "614a15385fa67b021ed1b97d8abdf08e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 13184,
            "upload_time": "2024-11-09T13:22:12",
            "upload_time_iso_8601": "2024-11-09T13:22:12.236643Z",
            "url": "https://files.pythonhosted.org/packages/ba/f3/29c46e7732158083b9b69b10fb02443710f80c0c7a7203e19678640e465e/conflog-2.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "79797f1b0bca8bdc993ee8975bcf2bf5ce81caff99b384eebf57f06fc9c96c22",
                "md5": "6c4cd8bb8544b8e699f5917cf94a5e6d",
                "sha256": "e09da6eecba49155138eec50c1206aefde7fe3c9e0d2ad9157ac87664138df07"
            },
            "downloads": -1,
            "filename": "conflog-2.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "6c4cd8bb8544b8e699f5917cf94a5e6d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 11524,
            "upload_time": "2024-11-09T13:22:13",
            "upload_time_iso_8601": "2024-11-09T13:22:13.660088Z",
            "url": "https://files.pythonhosted.org/packages/79/79/7f1b0bca8bdc993ee8975bcf2bf5ce81caff99b384eebf57f06fc9c96c22/conflog-2.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-09 13:22:13",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "cliffano",
    "github_project": "pyconflog",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "conflog"
}
        
Elapsed time: 0.59115s