Skip to main content

Fyers Internal Logging Library

Project description

README

This module is installed via pip:

pip install otel-fyers-logger

Packages Required

  • python-json-logger

Usage

Create a simple JSON Logger

import logging
from otel-fyers_logger import FyersLogger

logger = FyersLogger("fyers_logger", level=logging.DEBUG)

Set parameters for a request

import uuid

logger.set_fyId("ABC123")
logger.set_requestId(f"{uuid.uuid4()}")

Define logger with a custom handlers

from logging import FileHandler
from logging.handlers import TimedRotatingFileHandler
formatter = FyersLogger.get_json_formatter()

timed_rotating_handler = TimedRotatingFileHandler("fyers_logger_time_rotating.log", when="midnight", interval=1)
timed_rotating_handler.setFormatter(formatter)

simple_file_handler = FileHandler("fyers_logger.log")
simple_file_handler.setFormatter(formatter)

logger = FyersLogger("fyers_logger", handlers=[timed_rotating_handler, simple_file_handler], level=logging.DEBUG)

Add more handlers to the logger

new_file_handler = logging.FileHandler("fyers_logger_new_file.log")
new_file_handler.setFormatter(FyersLogger.get_json_formatter())
logger.add_handler(new_file_handler)

Log Data

logger.debug("This is a debug message", 1, 2,4, foo="bar", bar="baz")
logger.info("This is a info message", 1, 2,4, foo="bar", bar="baz", stacklevel=2)
logger.warning("This is a warning message", 1, 2,4, foo="bar", bar="baz")
logger.error("This is a error message", 1, 2,4, foo="bar", bar="baz")
logger.critical("This is a critical message", 1, 2,4, foo="bar", bar="baz")

try:
    raise Exception("DB connection error")
except Exception as e:
    logger.exception("getRRSpanFromDB", 1, 2,4, foo="bar", bar="baz")

Example Output

{"timestamp": "2023-09-20T18:29:33.967675Z", "level": "DEBUG", "name": "fyers_logger", "location": "[main:5] test_main", "message": "This is a debug message", "foo": "bar", "bar": "baz", "log_arguments": [1, 2, 4], "fyId": "ABC123", "requestId": "1469283c-cb2d-4ba5-9e8a-fe58930d72cf"}

{"timestamp": "2023-09-20T18:29:33.967944Z", "level": "INFO", "name": "fyers_logger", "location": "[main:5] test_main", "message": "This is an info message", "foo": "bar", "bar": "baz", "log_arguments": [1, 2, 4], "fyId": "ABC123", "requestId": "1469283c-cb2d-4ba5-9e8a-fe58930d72cf"}

{"timestamp": "2023-09-20T18:29:33.968095Z", "level": "WARNING", "name": "fyers_logger", "location": "[main:5] test_main", "message": "This is a warning message", "foo": "bar", "bar": "baz", "log_arguments": [1, 2, 4], "fyId": "ABC123", "requestId": "1469283c-cb2d-4ba5-9e8a-fe58930d72cf"}

{"timestamp": "2023-09-20T18:29:33.968238Z", "level": "ERROR", "name": "fyers_logger", "location": "[main:5] test_main", "message": "This is an error message", "foo": "bar", "bar": "baz", "log_arguments": [1, 2, 4], "fyId": "ABC123", "requestId": "1469283c-cb2d-4ba5-9e8a-fe58930d72cf"}

{"timestamp": "2023-09-20T18:29:33.968375Z", "level": "CRITICAL", "name": "fyers_logger", "location": "[main:5] test_main", "message": "This is a critical message", "foo": "bar", "bar": "baz", "log_arguments": [1, 2, 4], "fyId": "ABC123", "requestId": "1469283c-cb2d-4ba5-9e8a-fe58930d72cf"}

{"timestamp": "2023-09-20T18:29:33.968647Z", "level": "ERROR", "name": "fyers_logger", "location": "[main:5] test_main", "message": "getRRSpanFromDB\n %Traceback (most recent call last):\n  File \"/home/logger/test.py\", line 60, in b\n    raise Exception(\"DB connection error\")\nException: DB connection error\n", "foo": "bar", "bar": "baz", "log_arguments": [1, 2, 4], "fyId": "ABC123", "requestId": "1469283c-cb2d-4ba5-9e8a-fe58930d72cf"}

Clear data after every request

logger.clear_data()

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

fyers-logger-2.0-0.2.1.tar.gz (3.8 kB view hashes)

Uploaded Source

Built Distribution

fyers_logger_2.0-0.2.1-py3-none-any.whl (4.0 kB view hashes)

Uploaded Python 3

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