Skip to main content

Dynamic logging

Project description

Dynamic Logging, dlogging, is a Python library to provide the capability to change logger behaviour at runtime.

Installation

Install using pip:

pip install dlogging

Usage

Example 1 - general logging

Python:

from dlogging import DLogger, FORMATS, Fmt

log1 = DLogger("log1")  # use default logging format, filename
log2 = DLogger("log2", log_fmt=FORMATS[Fmt.FNAME_LINENO_LEVEL])
log3 = DLogger("log3", log_enabled=False, cout_enabled=True)  # log to file disabled

def log_messages():
    log1.info("msg #2")
    log2.info("msg #3")
    log3.error("msg #4") # message not logged to file, but display on stdout

log1.info("msg #1")
log_messages()

By default, messages are logged in logs/app.log

See tests/test_logging.py for more examples

Example 2 - logging override

Python:

import os
import logging
from dlogging import DLogger, LOG_CONF

def write_message(logger: logging.Logger) -> None:
    logger.debug('debug message')
    logger.info('info message')
    logger.warning('warn message')
    logger.error('error message')
    logger.critical('critical message')

def message_count(filename: str) -> int:
    if os.path.exists(filename):
        with open(filename, "r") as f:
            return len(f.readlines())
    return 0

logger = DLogger(name="main", filename="app.log", log_level=logging.INFO)

write_message(logger)  # expect 4 messages written into logs/app.log
assert message_count("logs/app.log") == 4

# override log_level 'logging.INFO' w/ new log level 'logging.WARNING'
LOG_CONF.log_level = logging.WARNING  # set new log_level at LOG_CONF
LOG_CONF.override_logger_settings(True)  # this triggers log_level override

write_message(logger)  # expect 3 messages written into logs/app.log
assert message_count("logs/app.log") == 7  # expect total 7 messages

See tests/test_override.py for more examples

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

MIT License

Copyright (c) 2016 Western Digital Corporation

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Release history Release notifications | RSS feed

This version

0.5

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dlogging-0.5.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dlogging-0.5-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file dlogging-0.5.tar.gz.

File metadata

  • Download URL: dlogging-0.5.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

File hashes

Hashes for dlogging-0.5.tar.gz
Algorithm Hash digest
SHA256 0968288cf644ef8ef0e078e5df28e6b0a3c70d7bce7e55a56a1191ab994531bc
MD5 19c4c447eee3f2facbfc8dac16513225
BLAKE2b-256 2e33ef213d76b15332602ce73f1edd21337c211466c6b8814ef9b1921a585476

See more details on using hashes here.

File details

Details for the file dlogging-0.5-py3-none-any.whl.

File metadata

  • Download URL: dlogging-0.5-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

File hashes

Hashes for dlogging-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 197d19f231fae495c18a2cda7fb82636fc35f6890d9c66296af92a5639e66f45
MD5 4f2bca0a44529c4a884d11326bab109a
BLAKE2b-256 74e7c713315277d2ccb9c023bbeeade67c5214dfb305ebc171f27a012ea1851b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page