Skip to content

Commit

Permalink
docs: update readme for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Jan 28, 2024
1 parent 0252512 commit 1d7a567
Showing 1 changed file with 84 additions and 5 deletions.
89 changes: 84 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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](<https://careers.wolt.com/en/blog/tech/professional-grade-mypy-configuration>)
- or pyright in one of [its mods](<https://github.com/microsoft/pyright/blob/main/docs/configuration.md>)
- In order to function with python extra well, recommendation to turn on
- [strict mypy](<https://careers.wolt.com/en/blog/tech/professional-grade-mypy-configuration>)
- or pyright in one of [its mods](<https://github.com/microsoft/pyright/blob/main/docs/configuration.md>)
- [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

Expand Down

0 comments on commit 1d7a567

Please sign in to comment.