Skip to main content

A Python logging helper

Project description

sharklog

Python logging helper.

PyPI License PyPI Version

Quick Start

  • Install sharklog:
python -m pip install sharklog
  • Use in standalone script:
# standalone.py
from sharklog import logging

logging.init(debug=True)    # init all loggers with level=logging.DEBUG
# or logging.init(level=logging.DEBUG)
logging.debug("debug message")
logging.info("info message")
logging.warning("warning message")
logging.error("error message")

If you want to change logging level for a module, you can set it in the module by:

from sharklog import logging
logging.getLogger().setLevel(logging.DEBUG)

or set it outside the module by specifying the logger name:

from sharklog import logging
logging.getLogger("module_name").setLevel(logging.DEBUG)

The default format of log messages is:

"[%(levelname)s]: %(message)s [%(asctime)s](%(filename)s:%(lineno)d)"

Usage in Package Development

Now I assume your file structure is like this:

--- parent_package
    |--- __init__.py
    |--- parent_module.py
    |--- logger.py
    |--- sub_package
        |--- __init__.py
        |--- sub_module.py
  • First, you can add NullHandler to the root logger in parent_package/__init__.py, which logger named parent_package:
# parent/__init__.py
from sharklog import logging

logging.getLogger().addHandler(logging.NullHandler())

This is mentioned in the Python Logging HOWTO to identify the logger's default behavior.

  • Then, use logging in your package, they will be prefixed with the logger name parent_package.:
# parent_module.py which is placed under package `parent_package`
from sharklog import logging

logger = logging.getLogger()    # the logger name will be `parent_package.parent_module`

logger.debug("debug message")
logger.info("info message")
logger.warning("warning message")
logger.error("error message")

If you already using builtin logging module, you can use sharklog as a drop-in replacement.

Just change import logging into from sharklog import logging. Then you can use logging as usual:

# sub_module.py
from sharklog import logging

# these log messages will be prefixed with the logger name `xxxpackage.xxmodule.module_name`
# here, as an example, the logger name will be `parent_package.sub_package.sub_module`
logging.debug("debug message")
logging.info("info message")
logging.warning("warning message")
logging.error("error message")
  • Finally, you can set the logging level for the package in the main script which using the package:
# main.py
from sharklog import logging

from parent_package import parent_module
from parent_package.sub_package import sub_module

if __name__ == "__main__":
    logging.init(debug=True)    # init all loggers with level=logging.DEBUG
    # or logging.init(level=logging.DEBUG)
    # logging inside the package will use the level set here

Or if you want to change the logging level for a specific module, you can set it in the module by:

from sharklog import logging
logging.getLogger().setLevel(logging.WARNING)

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

sharklog-0.0.4.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

sharklog-0.0.4-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file sharklog-0.0.4.tar.gz.

File metadata

  • Download URL: sharklog-0.0.4.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.8.10

File hashes

Hashes for sharklog-0.0.4.tar.gz
Algorithm Hash digest
SHA256 5dae9c0fb49e57af77f396d0bd044eca25087b9934bbc02c599e6a6c79c56198
MD5 ac2ab0aa92ca69fc57060ea7a6fff356
BLAKE2b-256 5773e7cd065011a862ce76158cfffcceae7880d8354dcd49b45d5b78d69df8f4

See more details on using hashes here.

File details

Details for the file sharklog-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: sharklog-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.8.10

File hashes

Hashes for sharklog-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5a32a2a2a4fff4cd2dc113401d4127b3f03d04ec8f2aa99eff9ace56b5ae3b0f
MD5 5b96ac4fa6d8246c6fdf0782c61058ab
BLAKE2b-256 ea2f635c9b39e404c48397eede2579e5e6726755ea3e2c101febd983ec2e3ab7

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