From 1d7a567d6bbcf59bdf82e89415516b7ab4b9ef2d Mon Sep 17 00:00:00 2001 From: dd84ai Date: Sun, 28 Jan 2024 21:50:03 +0100 Subject: [PATCH] docs: update readme for examples --- README.md | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index cee3d33..e950ffe 100644 --- a/README.md +++ b/README.md @@ -14,15 +14,94 @@ Static typing approach brings here consistent way to define key values to final - Ability to define different log levels for different created loggers - Easier turning complex objects into structured logging - accepts maps and structs as its params. It will parse them on their own. -[See folder examples](./examples) +[See folder for up to date examples](./examples) # Python specifics -In order to function with python well, recommendation to turn on -- [strict mypy]() -- or pyright in one of [its mods]() +- In order to function with python extra well, recommendation to turn on + - [strict mypy]() + - or pyright in one of [its mods]() +- [Published at pypi](https://pypi.org/project/typelog/) -[Published at pypi](https://pypi.org/project/typelog/) +# How to use + +examples/types.py +```py +from typing import NewType +from dataclasses import dataclass + +TaskID = NewType("TaskID", int) + + +@dataclass(frozen=True) +class Task: + smth: str + b: int +``` + +examples/logtypes.py +```py +from . import types +from typelog import LogType +from typing import Dict, Any + + +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 +```py +import unittest +from typelog import get_logger +import typelog +from . import logtypes +from . import types +from typelog import Loggers, LogConfig +from typelog.types import LibName, LogLevel, RootLogLevel +import logging + +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") + +``` # Alternative Versions