Coder Social home page Coder Social logo

adx-logging-handler's Introduction

ADX-logging-handler

A python logging handler for Azure Data explorer

PyPI version

Introduction

This package provides a Python logging handler for Azure Data Explorer (ADX). It allows you to send your application logs directly to an ADX cluster.

Getting Started

Prerequisites

Before you can use this package, you need to set up your environment variables. Create a .env file in your project root and add the following variables:

ADX_CLUSTER_URI=""
ADX_CLUSTER_INGESTION_URI=""
ADX_DATABASE_NAME=""
ADX_HANDLER_LEVEL="INFO"
LOG_TABLE_NAME="Log"
LOG_TABLE_MAPPING_NAME="Log_Mapping"
CLIENT_ID=""
CLIENT_SECRET=""
TENANT_ID=""

Replace the empty strings with your actual values.

Setting up ADX

You also need to create a table and a JSON mapping in your ADX cluster. You can do this with the following Kusto Query Language (KQL) commands:

.create table my_table (timestamp: datetime, logger: string, level: string, message: string, thread_id: string, category: string, user: string)

.create table my_table ingestion json mapping 'my_table_mapping' '[{"column":"timestamp", "path":"$.asctime", "datatype":"datetime"}, {"column":"logger", "path":"$.name", "datatype":"string"}, {"column":"level", "path":"$.levelname", "datatype":"string"}, {"column":"message", "path":"$.message", "datatype":"string"}, {"column":"thread_id", "path":"$.thread", "datatype":"string"}, {"column":"category", "path":"$.category", "datatype":"string"}, {"column":"user", "path":"$.user", "datatype":"string"}]'

Installation

You can install this package with pip:

pip install adx-logging-handler

Usage

Here is an example of how to use this package in your Python code:

import logging
import datetime
import json
from dotenv import load_dotenv
from adx_logging_handler import add_ADX_handler_to_logger

def log_str_func(record: logging.LogRecord):
    asctime_obj = datetime.datetime.utcfromtimestamp(record.created)
    formatted_asctime_str = asctime_obj.strftime("%Y-%m-%dT%H:%M:%S")
    json_str = json.dumps({
        "timestamp": formatted_asctime_str,
        "name": record.name,
        "level": record.levelname,
        "message": record.getMessage(),
        "thread": record.thread,
        "category": getattr(record, "category", None),
        "user": getattr(record, "user", None),
    })
    return json_str

load_dotenv()
logger = logging.getLogger("MyLogger")
add_ADX_handler_to_logger(logger, log_str_func)
logger.info("this is a info message.", extra={"category": "Test", "user": "BOT"})

This will send a log message to your ADX cluster whenever you call logger.info().

License This project is licensed under the MIT License.

adx-logging-handler's People

Contributors

twjackysu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.