Skip to main content

Logging made simple.

Project description

logpie

Logging made simple.

This is a simple logging module with thread & file locking abilities.


Installation:

python -m pip install [--upgrade] logpie

Usage:

# your `main.py` module

from os.path import dirname, realpath, join
from sys import modules
from types import ModuleType

from cfgpie import get_config, CfgParser
from logpie import get_logger, Logger

# **************************** config constants: **************************** #

# main module
MODULE: ModuleType = modules.get("__main__")

# root directory
ROOT: str = dirname(realpath(MODULE.__file__))

# default config file path:
CONFIG: str = join(ROOT, "config", "cfgpie.ini")

# backup config params:
BACKUP: dict = {
    "LOGGER": {
        "basename": "logpie",  # if handler is `file`
        "folder": r"${DEFAULT:directory}\logs",
        "handlers": ["file"],  # or `console` or both (always as list)
        "level": 0,  # logs all messages above this level
    }
}

# NOTE: The constants above serve the usage example,
#       and you can use whatever suits you best.

# ********************** get `ConfigParser` instance: *********************** #

cfg: CfgParser = get_config(name="my_config")
cfg.set_defaults(directory=ROOT)
cfg.open(file_path=CONFIG, encoding="UTF-8", fallback=BACKUP)

# ************************* get `Logger` instance: ************************** #

# we can pass a config instance:
log: Logger = get_logger(name="my_logger", config=cfg)

or

# it will look for a config instance named `my_config`:
log: Logger = get_logger(name="my_logger", config="my_config")

or

# it will create and use its own config instance:
log: Logger = get_logger(name="my_logger", level="debug", handlers="file", basename="logpie")


if __name__ == '__main__':
    log.debug("Testing debug messages...")
    log.info("Testing info messages...")
    log.warning("Testing warning messages...")
    log.error("Testing error messages...")
    log.critical("Testing critical messages...")

By default, logging level is set to NOTSET (0) and it logs all messages.

The log file is prefixed with a date and will have an index number attached before the extension (ex: 2022-08-01_logpie.1.log). When it reaches 1 Mb the file handler will switch to another file by incrementing its index with 1.

The folder tree is by default structured as follows:

.
└───logs
    └───year (ex: 2022)
        └───month (ex: january)
            ├───2022-08-01_logpie.1.log
            ├───2022-08-01_logpie.2.log
            └───2022-08-01_logpie.3.log

When the current month changes, a new folder is created and the previous one is archived.


Project details


Download files

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

Source Distribution

logpie-2.1.1.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

logpie-2.1.1-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file logpie-2.1.1.tar.gz.

File metadata

  • Download URL: logpie-2.1.1.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for logpie-2.1.1.tar.gz
Algorithm Hash digest
SHA256 f69dcae40ceb38e59b52e2fe2d520f7fa50a41a59c6f6591dfa5932f4282b02f
MD5 61c17a7d3cdc663f7daf38994274117c
BLAKE2b-256 9efc6652b20e6085d2e784005f77772f27a087b8a15bbdd0f12e2f18361e3af1

See more details on using hashes here.

File details

Details for the file logpie-2.1.1-py3-none-any.whl.

File metadata

  • Download URL: logpie-2.1.1-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for logpie-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b00725342331020ec0f54d057ca2e367dca7f646f5932dedda6d7ebd0317d55f
MD5 88fcd19a620c129517528040a206f873
BLAKE2b-256 a41ff015129d4f5e5a886cefc62886e0277a00d7046a946eb29968eb2acce1d1

See more details on using hashes here.

Supported by

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