The BOHICA Logging Library provides a configured logger for you module or application
Project description
BOHICA Logging Library
The BOHICA Logging Library provides a configured logger for you module or application
💪 Getting Started
Example Usage
from bohicalog import logger
logger.debug("hello")
logger.info("info")
logger.warning("warn")
logger.error("error")
# This is how you'd log an exception
try:
raise Exception("this is a demo exception")
except Exception as e:
logger.exception(e)
# JSON logging
import bohicalog
bohicalog.json()
logger.info("JSON test")
# Start writing into a logfile
bohicalog.logfile("/tmp/bohicalog-demo.log")
# Set a minimum loglevel
bohicalog.loglevel(bohicalog.WARNING)
This is the output:
Note: You can find more examples in the documentation: https://bohicalog.readthedocs.io
JSON logging
JSON logging can be enabled for the default logger with bohicalog.json(), or with setup_logger(json=True) for custom loggers:
>>> bohicalog.json()
>>> logger.info("test")
{"asctime": "2022-12-21 10:42:45,808", "filename": "<stdin>", "funcName": "<module>", "levelname": "INFO", "levelno": 20, "lineno": 1, "module": "<stdin>", "message": "test", "name": "bohicalog_default", "pathname": "<stdin>", "process": 76179, "processName": "MainProcess", "threadName": "MainThread"}
>>> my_logger = setup_logger(json=True)
>>> my_logger.info("test")
{"asctime": "2022-12-21 10:42:45,808", "filename": "<stdin>", "funcName": "<module>", "levelname": "INFO", "levelno": 20, "lineno": 1, "module": "<stdin>", "message": "test", "name": "bohicalog_default", "pathname": "<stdin>", "process": 76179, "processName": "MainProcess", "threadName": "MainThread"}
The logged JSON object has these fields:
{
"asctime": "2022-12-21 10:43:40,765",
"filename": "test.py",
"funcName": "test_this",
"levelname": "INFO",
"levelno": 20,
"lineno": 9,
"module": "test",
"message": "info",
"name": "bohicalog",
"pathname": "_tests/test.py",
"process": 76204,
"processName": "MainProcess",
"threadName": "MainThread"
}
Exceptions logged with logger.exception(e) have these additional JSON fields:
{
"levelname": "ERROR",
"levelno": 40,
"message": "this is a demo exception",
"exc_info": "Traceback (most recent call last):\n File \"_tests/test.py\", line 15, in test_this\n raise Exception(\"this is a demo exception\")\nException: this is a demo exception"
}
Telegram logging
Telegram logging can be enabled for the default logger with bohicalog.telegram(), or with setup_logger(telegram=True) for custom loggers:
import logging
from bohicalog.handlers import TelegramLoggingHandler
BOT_TOKEN = '1612485124:AAFW9JXxjqY9d-XayMKh8Q4-_iyHkXSw3N8'
CHANNEL_NAME = 'example_channel_logger'
def main():
telegram_log_handler = TelegramLoggingHandler(BOT_TOKEN, CHANNEL_NAME)
my_logger = logging.getLogger('My-Logger')
my_logger.setLevel(logging.INFO)
my_logger.addHandler(logging.StreamHandler())
my_logger.addHandler(telegram_log_handler)
for i in range(5):
my_logger.error(f'iterating {i}..')
if __name__ == '__main__':
main()
Take a look at the documentation for more information and examples:
- Documentation: https://bohicalog.readthedocs.io.
🚀 Installation
The most recent code and data can be installed directly from GitHub with:
$ pip install git+https://github.com/BOHICA-Labs/bohicalog.git
👐 Contributing
Contributions, whether filing an issue, making a pull request, or forking, are appreciated. See CONTRIBUTING.md for more information on getting involved.
👋 Attribution
⚖️ License
The code in this package is licensed under the MIT License.
🍪 Cookiecutter
This package was created with @audreyfeldroy's cookiecutter package using @cthoyt's cookiecutter-snekpack template.
🛠️ For Developers
See developer instructions
The final section of the README is for if you want to get involved by making a code contribution.
Development Installation
To install in development mode, use the following:
$ git clone git+https://github.com/BOHICA-Labs/bohicalog.git
$ cd bohicalog
$ pip install -e .
🥼 Testing
After cloning the repository and installing tox with pip install tox, the unit tests in the tests/ folder can be
run reproducibly with:
$ tox
Additionally, these tests are automatically re-run with each commit in a GitHub Action.
📖 Building the Documentation
The documentation can be built locally using the following:
$ git clone git+https://github.com/BOHICA-Labs/bohicalog.git
$ cd bohicalog
$ tox -e docs
$ open docs/build/html/index.html
The documentation automatically installs the package as well as the docs
extra specified in the setup.cfg. sphinx plugins
like texext can be added there. Additionally, they need to be added to the
extensions list in docs/source/conf.py.
📦 Making a Release
After installing the package in development mode and installing
tox with pip install tox, the commands for making a new release are contained within the finish environment
in tox.ini. Run the following from the shell:
$ tox -e finish
This script does the following:
- Uses Bump2Version to switch the version number in the
setup.cfg,src/bohicalog/version.py, anddocs/source/conf.pyto not have the-devsuffix - Packages the code in both a tar archive and a wheel using
build - Uploads to PyPI using
twine. Be sure to have a.pypircfile configured to avoid the need for manual input at this step - Push to GitHub. You'll need to make a release going with the commit where the version was bumped.
- Bump the version to the next patch. If you made big changes and want to bump the version by minor, you can
use
tox -e bumpversion minorafter.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bohicalog-1.1.3.tar.gz.
File metadata
- Download URL: bohicalog-1.1.3.tar.gz
- Upload date:
- Size: 37.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d109cd8d6e67eb6310c87b1e6c17212a6aaab8d82c39397a665c9c0a67b481fb
|
|
| MD5 |
a536c409a7a5f70094f8aa57d89f3c9b
|
|
| BLAKE2b-256 |
e9b2b66a420c8deab193c406b153801363d84c0e0aba672980555af9deacc566
|
File details
Details for the file bohicalog-1.1.3-py3-none-any.whl.
File metadata
- Download URL: bohicalog-1.1.3-py3-none-any.whl
- Upload date:
- Size: 26.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6099a674c2f09cf0cf3d47eeac6bde07cc81134d21fdade46817b648186f3bea
|
|
| MD5 |
2674c53772b164fd1441a84c96bff0eb
|
|
| BLAKE2b-256 |
f8d0da0fe1dbbf0eb6afe731b2cca2e71d6080b50ca27c4b0040bd70c310d46b
|