# Python Loguru Module

Loguru is a library which aims to make logging in Python enjoyable. It provides a pre-configured logger that just works, without the boilerplate of the standard `logging` module.

## Installation

```bash
pip install loguru
```

## Basic Usage

You don't need to initialize a logger; just import the global `logger` object.

```python
from loguru import logger

logger.debug("That's it, beautiful and simple logging!")
logger.info("If you're using Python {}, prefer {feature} of course!", 3.6, feature="f-strings")
```

## Sinks (Output Destinations)

Loguru uses "sinks" to manage where logs go. By default, it logs to stderr.

### Logging to a File

```python
from loguru import logger

logger.add("file_{time}.log")
```

### Rotation and Retention

You can easily configure log rotation and retention (cleanup).

```python
# Rotate file when it reaches 500 MB
logger.add("file_1.log", rotation="500 MB")

# Rotate every day at 12:00
logger.add("file_2.log", rotation="12:00")

# Cleanup logs older than 10 days
logger.add("file_3.log", retention="10 days")

# Compress logs
logger.add("file_4.log", compression="zip")
```

## Exception Handling

Loguru provides a decorator to automatically catch exceptions and log the traceback.

```python
from loguru import logger

@logger.catch
def my_function(x, y):
    return x / y

my_function(1, 0)
# This will log the ZeroDivisionError automatically
```

## Serialization

Loguru can log messages as JSON, which is useful for parsing.

```python
logger.add("file.json", serialize=True)
```

[[programming/python/python]]