conflog


Nameconflog JSON
Version 1.5.1 PyPI version JSON
download
home_pagehttps://github.com/cliffano/pyconflog
SummaryPython logging setup via environment variables and configuration files
upload_time2023-08-09 11:09:50
maintainer
docs_urlNone
authorCliffano Subagio
requires_python>=3.8
license
keywords log logger logging config configuration environment envvar ini json xml yaml
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/>

Pyconflog
---------

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

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

    pip3 install pyconflog

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"

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": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "log,logger,logging,config,configuration,environment,envvar,ini,json,xml,yaml",
    "author": "Cliffano Subagio",
    "author_email": "cliffano@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/12/be/be35b5cadcf0a5b2def1c874720893972ebeba68c2ecbf536a525ba657ba/conflog-1.5.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\nPyconflog\n---------\n\nPyconflog library provides Python logging setup via environment variables and configuration files.\n\nInstallation\n------------\n\n    pip3 install pyconflog\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\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": "",
    "summary": "Python logging setup via environment variables and configuration files",
    "version": "1.5.1",
    "project_urls": {
        "Homepage": "https://github.com/cliffano/pyconflog"
    },
    "split_keywords": [
        "log",
        "logger",
        "logging",
        "config",
        "configuration",
        "environment",
        "envvar",
        "ini",
        "json",
        "xml",
        "yaml"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4717b23e3da166b9f24a14e9289bbee5fadda9e600fd27979ce22280c04af0c7",
                "md5": "6387e2830affc470603c7528ddfb409e",
                "sha256": "ae24edcb4f38165f54404e610ececd36c732fcf365c744d22b9b7b7d5edbb356"
            },
            "downloads": -1,
            "filename": "conflog-1.5.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6387e2830affc470603c7528ddfb409e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 21340,
            "upload_time": "2023-08-09T11:09:49",
            "upload_time_iso_8601": "2023-08-09T11:09:49.124504Z",
            "url": "https://files.pythonhosted.org/packages/47/17/b23e3da166b9f24a14e9289bbee5fadda9e600fd27979ce22280c04af0c7/conflog-1.5.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "12bebe35b5cadcf0a5b2def1c874720893972ebeba68c2ecbf536a525ba657ba",
                "md5": "45387a406c8fb0bdb0a1e30194769de4",
                "sha256": "145ffedce4cd18bb5983fcb883880e2f007a855829eca2844d6eaea971f36f8a"
            },
            "downloads": -1,
            "filename": "conflog-1.5.1.tar.gz",
            "has_sig": false,
            "md5_digest": "45387a406c8fb0bdb0a1e30194769de4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 15843,
            "upload_time": "2023-08-09T11:09:50",
            "upload_time_iso_8601": "2023-08-09T11:09:50.707211Z",
            "url": "https://files.pythonhosted.org/packages/12/be/be35b5cadcf0a5b2def1c874720893972ebeba68c2ecbf536a525ba657ba/conflog-1.5.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-09 11:09:50",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "cliffano",
    "github_project": "pyconflog",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "requirements": [],
    "lcname": "conflog"
}
        
Elapsed time: 0.10602s