Skip to main content

A Python logging helper

Project description

sharklog

Python logging helper.

PyPI License PyPI Version

Quick Start

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

sharklog.init(debug=True)    # init current logger with level=sharklog.DEBUG

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

The default format of log messages is:

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

Usage

Use in standalone script

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

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

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

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

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 current logger 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.1.1.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

sharklog-0.1.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sharklog-0.1.1.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.8

File hashes

Hashes for sharklog-0.1.1.tar.gz
Algorithm Hash digest
SHA256 19780c16173b7dee49c289bb7f019a5f4bcc253eb4356ecec3fa3949f8331710
MD5 d6121d846a24144197808f1f8072d367
BLAKE2b-256 b2901ff8e5c74a4cc237560ae96c227043bd6af294c4a4d924cf60e0cc5934d7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sharklog-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.8

File hashes

Hashes for sharklog-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 63065e0b2bbf7b7a2853c606798eeff1b12e7618819bc6435166680d17a02539
MD5 ed68822127c2fd4ad649fef1ed0412f8
BLAKE2b-256 ea1bc1361bbd73cc63fb472b84a32999729ca807f2686073c1512ad52624d10c

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