Skip to main content

Logging handler module that writes the logs to Kusto database.

Project description

Log2Kusto

log2kusto is a Python logging handler library for saving the generated log records to a Kusto database table.

Installation

Use the package manager [pip](link will be updated) to install foobar.

pip install log2kusto

Requirments

  • Python logging library
  • An MS internal library for providing credentials to authenticate to Kusto (working on making it open source.)

Usage

import logging
from log2kusto.kusto_handler import KustoHandler

class CustomFormatter(logging.Formatter):
    def __init__(self, fmt=None, datefmt=None, style='%', validate=True, attributes_list=[]):
        super().__init__(fmt, datefmt, style, validate)
        self.attributes = attributes_list

    def format(self, record):
        print("in format")
        for attr in self.attributes:
            print(attr)
            if not hasattr(record, attr):
                setattr(record, attr, '')
        return super().format(record)

#https://docs.python.org/3/library/logging.html#logrecord-attributes
logrecord_attributes_list = ['asctime', 'levelname', 'filename', 'funcName', 'module', 'message']
custom_attributes_list = ['env', 'domain']
all_attributes_list = logrecord_attributes_list + custom_attributes_list
formatter = CustomFormatter('%(' + ((')s' + " ; " + '%(').join(all_attributes_list)) + ')s', "%Y-%m-%d %H:%M:%S", \
                            attributes_list=all_attributes_list)

kusto_handler = KustoHandler(<kusto cluster name>, <kusto database>, <kusto table name>, all_attributes_list)
kusto_handler.setLevel(logging.INFO)
kusto_handler.setFormatter(formatter)

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(kusto_handler)

d = {'env':'stage', 'domain':'xyz'}
while True:
    log = input("> ")
    if log.strip().lower() != "quit":
        logger.warn(log)
        logger.info(log, extra=d)
    else:
        break

## 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](https://choosealicense.com/licenses/mit/)

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

log2kusto-0.1.4-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file log2kusto-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: log2kusto-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.3

File hashes

Hashes for log2kusto-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bd85e744d30fcbe409f77700469571234f56496acffe06f18e6a104bc1b60e5a
MD5 7389107110dbf018c6ff064d574b64ac
BLAKE2b-256 272f684d25b998e040fc5af72482e262c80e5f3f62e43b82120d759403c975d1

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