Skip to main content

Log chains for webserver

Project description

logtc: Python Logging Library

Overview

logtc is a versatile Python logging library designed for modern web services. It provides advanced logging capabilities including context-aware logging for request and internal tasks, automatic file rotation based on time intervals, and socket-based logging with automatic Socket.IO support.

Key Features

  • Context-Aware Logging: Log messages are tagged with a unique context, making it easier to trace logs back to specific requests or internal tasks.
  • Automatic File Rotation: Log files are automatically rotated based on configurable time intervals, helping manage log file sizes and organization.
  • Socket Logging: Real-time logging through web sockets, with built-in support for Socket.IO.
  • Easy Integration with Web Frameworks: Designed to be easily integrated with frameworks like FastAPI.

Installation

You can install logtc via pip:

pip install logtc

Setup and Configuration

Basic Logger Setup

To set up the logger, import the Logtc from logtc and configure it with your preferences:

from logtc import Logger

Logtc().setup(name="test")
Logtc().setup_file_handler(file_path="../log/.log", when="M", interval=1)
Logtc().setup_socket_handler(url="http://localhost:8000", handshake_path="/logtc/socket.io")
Logtc().setup_stream_handler()

Parameters:

  • name: Name of the logging service.
  • file_path: Path to the log file.
  • when and interval: Controls the log file rotation. For instance, "M" and 1 means the file rotates every minute.
  • url: URL for the socket logging server.
  • handshake_path: Handshake path for the Socket.IO server.

Context-Aware Logging

For logging with request or task-specific contexts:

from logtc import logtc_context

# Set a unique identifier for the current context
logtc_context.set(UNIQUE_ID)

Each log message will include this unique identifier, making it easier to correlate logs with specific requests or tasks.

Socket Logging with FastAPI

To use socket logging with FastAPI, mount the socket from logtc:

from fastapi import FastAPI
from logtc import sockettc

app = FastAPI()

# Mount the socket logger
app.mount("/logtc", sockettc())

This sets up an endpoint for the Socket.IO server, enabling real-time log streaming.

Usage

After setting up logtc, you can use its logger to log messages of different severities:

from logtc import Logtc

# Logging an info message
Logtc().logger.info("This is an info message.")

# Logging a warning message
Logtc().logger.warning("This is a warning message.")

# Logging an error message
Logtc().logger.error("This is an error message.")

# Logging a debug message
Logtc().logger.debug("This is a debug message.")

# Logging a critical message
Logtc().logger.critical("This is a critical message.")

Contributing

Contributions to logtc are welcome! Please refer to the repository's contribution guidelines for more details.

License

logtc is released under MIT License.

Project details


Release history Release notifications | RSS feed

This version

0.1

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

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

loggg-0.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file loggg-0.1-py3-none-any.whl.

File metadata

  • Download URL: loggg-0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for loggg-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9d7a08718d5f7c4abc50c44c784a4fb473f79af8e68cf43a46a9f594221b5566
MD5 fcf691a1bafb1e01fbbee77f84118f3f
BLAKE2b-256 02a0cbb379249cde41155e3c6cf3bd83fae382a51df04daf94840b2aa891b36d

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