# log
<subtitle>How to view template construction logs</subtitle>

You can use the SDK to retrieve build logs.

##Default logger

We provide a default logger that you can use to filter logs by level:

```python
from ucloud_sandbox import Template, default_build_logger

Template.build(
    template,
    'my-template',
    on_build_logs=default_build_logger(
        min_level="info", #The minimum log level to be displayed
    )
)
```

## Custom logger

You can customize how logs are processed:

```python
# Simple logging
on_build_logs=lambda log_entry: print(log_entry)

# Custom format
def custom_logger(log_entry):
    time = log_entry.timestamp.isoformat()
    print(f"[{time}] {log_entry.level.upper()}: {log_entry.message}")

Template.build(template, 'my-template', on_build_logs=custom_logger)

# Filter by log level
def error_logger(log_entry):
    if log_entry.level in ["error", "warn"]:
        print(f"Error/Warning: {log_entry}", file=sys.stderr)

Template.build(template, 'my-template', on_build_logs=error_logger)
```

The `on_build_logs` callback receives a structured `LogEntry` object with the following properties:

```python
LogEntryLevel = Literal["debug", "info", "warn", "error"]

@dataclass
class LogEntry:
    timestamp: datetime
    level: LogEntryLevel
    message: str

    def __str__(self) -> str: # Return the formatted log string


# Indicates the start of the build process
@dataclass
class LogEntryStart(LogEntry):
    level: LogEntryLevel = field(default="debug", init=False)

# Indicates the end of the build process
@dataclass
class LogEntryEnd(LogEntry):
    level: LogEntryLevel = field(default="debug", init=False)
```

In addition to the `LogEntry` type, there are also `LogEntryStart` and `LogEntryEnd` types that indicate the start and end of the build process. Their default log level is `debug`, you can use them like this:

```python
if isinstance(log_entry, LogEntryStart):
    #Build has started
    return

if isinstance(log_entry, LogEntryEnd):
    # The build has ended
    return
```
