semvec/pypi-packages huggingface dataset:
betterlogging 1.0.0
My logging improvement
License: MIT

# BetterLogging


![BetterLogging](https://telegra.ph/file/ebd3b272eb1969dd55f58.png "BetterLogging")

Improved version of the standard logger.

* Added `TRACE` level.
* Added `ColorizedFormatter`.
* Added better traceback formatting.

This package patching the standard `logging` library.

Thus, after import, all improvements will be available inside the `logging` module.

But for better typing, I prefer to use `betterlogging` everywhere.

### Requirements

Python 3.7 and above. No additional dependencies.

### Installation

`pip install betterlogging`

### Usage

#### Simple usage

```python
import betterlogging as logging


logging.basic_colorized_config(level=logging.INFO)

logger = logging.getLogger('test')

logger.trace("some trace message")
logger.debug("some debug message")
logger.info("some info message")
logger.warning("some warning message")
logger.error("some error message")
logger.critical("some critical message")
```

#### More advanced usage

```python
import betterlogging as logging


# You can use shortcut:
logger = logging.get_colorized_logger(name="test")

# Or, if you want to configure formatter:
handler = logging.StreamHandler()
handler.setFormatter(logging.ColorizedFormatter(hide_lib_diagnose=False))

logger.addHandler(handler)
logger.setLevel(logging.TRACE)

logger.trace("some trace message")
logger.debug("some debug message")
logger.info("some info message")
logger.warning("some warning message")
logger.error("some error message")
logger.critical("some critical message")

def div(x: int, y: int) -> float:
    return x / y

try:
    div(1, 0)
except:
    logger.exception("Some exception")
```

#### Config for `uvicorn`

```json
{
  "version": 1,
  "disable_existing_loggers": false,
  "formatters": {
    "default": {
      "()": "betterlogging.ColorizedFormatter"
    },
    "access": {
      "()": "betterlogging.ColorizedFormatter",
      "fmt": "%(c_fg_green)s%(asctime)s %(c_color)s%(levelname)-8s%(c_reset)s %(c_fg_cyan)s[%(name)s] %(c_reset)s%(message)s"
    }
  },
  "handlers": {
    "default": {
      "formatter": "default",
      "class": "logging.StreamHandler"
    },
    "access": {
      "formatter": "access",
      "class": "logging.StreamHandler",
      "stream": "ext://sys.stdout"
    }
  },
  "loggers": {
    "": {
      "handlers": [
        "default"
      ],
      "level": "DEBUG"
    },
    "uvicorn.error": {
      "level": "INFO"
    },
    "uvicorn.access": {
      "handlers": [
        "access"
      ],
      "level": "INFO",
      "propagate": false
    }
  }
}
```

```shell script
uvicorn application:app --debug --reload --log-config ./logging.json
```
envreader 1.0.0
Environment variables parser with types!
License: MIT

# EnvReader

Environment variables parser with types! Yes!

Every time when you make new service you need some class to receive, validate and store environment variables.

With this package it’ll be easy and funny.

Just make a class with typed fields and... that’s it.

### Requirements

Python 3.7 and above. There's no additional dependencies.

### Installation

`pip install envreader`

### Simple usage

```python
from envreader import EnvReader


class MyEnv(EnvReader):
    PATH: str
    LIST: list
    NONE_EXIST: int = 1234  # Variable with default value


e = MyEnv()

print(e.PATH)
# /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

print(e.LIST)
# [1, 2, 3, 4]

print(e.NONE_EXIST)
# 1234
```

### Use with transform functions

I don’t want to make a giant validation library like wonderful **Pydantic**. Thus EnvReader supports only simple types(
bool, str, int, float, list, tuple and dict) by default. This is enough in most cases.

Transform functions allows using EnvReader for more complex cases.

```python
from typing import List
from envreader import EnvReader, Field


class MyEnv(EnvReader):
    PATH: List[str] = Field(transform=lambda x: x.split(":"))


e = MyEnv()

print(e.PATH)
# ['/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin', '/sbin']
```

### Using static methods as a transform functions.

You may store all your helper functions inside the same class. But don’t forget to add @staticmethod decorator.

```python
from typing import List
from envreader import EnvReader, Field


class MyEnv(EnvReader):
    @staticmethod
    def trans(x: str) -> List[str]:
        return x.split(':')

    PATH: List[str] = Field(transform=trans)


e = MyEnv()

print(e.PATH)
# ['/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin', '/sbin']
```

### Make your environment variables self-documented.

Documentation is in great demand for all good applications, right?

```python
from envreader import EnvReader, Field


class MyEnv(EnvReader):
    PATH: str = Field("/sbin", description="Application path", example="/usr/bin:/bin:/usr/sbin:/sbin")


e = MyEnv()

print(e.help())
# PATH
#     Application path
#     Example: /usr/bin:/bin:/usr/sbin:/sbin
#     Default: /sbin
```

### Error handling? Easy.

```python
import sys
from envreader import EnvReader, EnvMissingError


class MyEnv(EnvReader):
    SOME_VAR: str


try:
    e = MyEnv()
except EnvMissingError as e:
    print(f"Missing required env var {e.field}")
    sys.exit(-1)
# Missing required env var SOME_VAR
```

#### Enjoy!