Skip to main content

Static typed structured logging

Project description

Typelog - Static typed structured logging

Description

This project araised from the need to log backend applications, aws lambdas and other stuff in modern cloud ecosystem. Logging systems today are able easily parsing JSON format out of the box. Static typing approach brings here consistent way to define key values to final msg, as well as easier following Domain Driven Design, where logs consistently describe what they log. Static typed logging brings easy refactoring to any present logs.

Features

  • Accepts static typed components as optional params
    • it will not accept any options
    • has shortcut WithFields, to make clone of the logger with default logging fields
  • Easy to turn on/off parameters by environment variables
    • Ability to define different log levels for different created loggers
  • Easier turning complex objects into structured logging

Alternative Versions

Python specifics

How to use

install with pip install typelog

examples/types.py

from dataclasses import dataclass
from typing import NewType

TaskID = NewType("TaskID", int)


@dataclass(frozen=True)
class Task:
    smth: str
    b: int

examples/logtypes.py

from typing import Any, Dict

from typelog import LogType

from . import types


def TaskID(value: types.TaskID) -> LogType:
    def wrapper(params: Dict[str, Any]) -> None:
        params["task_id"] = str(value)

    return wrapper


def Task(value: types.Task) -> LogType:
    def wrapper(params: Dict[str, Any]) -> None:
        params.update(value.__dict__)

    return wrapper

examples/test_examples.py

import logging
import unittest

import typelog
from typelog import LogConfig, Loggers, get_logger
from typelog.types import LibName, LogLevel, RootLogLevel

from . import logtypes, types

logger = get_logger(__name__)


class TestExamples(unittest.TestCase):
    def setUp(self) -> None:
        Loggers(
            RootLogLevel(logging.DEBUG),
            LogConfig(LibName("examples"), LogLevel(logging.DEBUG)),
            add_time=True,
        ).configure()

    def test_basic(self) -> None:
        logger.warn("Writing something", logtypes.TaskID(types.TaskID(123)))

    def test_another_one(self) -> None:
        task = types.Task(smth="abc", b=4)
        logger.warn("Writing something", logtypes.Task(task))

    def test_with_fields(self) -> None:
        logger2 = logger.with_fields(logtypes.Task(types.Task(smth="aaa", b=1)))
        logger3 = logger.with_fields(
            typelog.String("smth", "asd"), typelog.Int("number", 2)
        )

        logger.info("logger printed")
        logger2.info("logger2 printed")
        logger3.info("logger3 printed")

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

typelog-0.3.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

typelog-0.3.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file typelog-0.3.0.tar.gz.

File metadata

  • Download URL: typelog-0.3.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for typelog-0.3.0.tar.gz
Algorithm Hash digest
SHA256 9ecc5f8c07a242a6fffaccb072deefedbd2bea35167b228ed403d8859fc53658
MD5 30f6fcf2fc8fb11b5b47e979e325b7f5
BLAKE2b-256 feed625a97be9b68a98e7baf221722853ad1e2d3013fac8e14b2a5b478487ebb

See more details on using hashes here.

File details

Details for the file typelog-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: typelog-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for typelog-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 523a54a663798c3cccd7710ae63db4d6167bcfb8e2ee38f7a2fe8f6adedad0b3
MD5 df81e4df07d4422dfe5e1338f38fa2fd
BLAKE2b-256 c4a185bbc1e0a2665477e1d229453ecaf435643681b78a52eff03cf90c9711c9

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