---
title: Builder
summary: A builder type for Logger.
url: https://www.fastly.com/documentation/reference/compute/sdks/rust/log_fastly/struct.Builder/
---

**Kind:** Struct  
**Module:** [log_fastly](https://www.fastly.com/documentation/reference/compute/sdks/rust/log_fastly/)  
**Package:** log-fastly 0.13.1  
**Source:** https://docs.rs/crate/log-fastly/0.13.1/source/src/lib.rs#353

```rust
pub struct Builder { /* private fields */ }
```

A builder type for [`Logger`](https://www.fastly.com/documentation/reference/compute/sdks/rust/log_fastly/struct.Logger/).

You can use this builder to register endpoints, set default endpoints, control the levels of
logging messages emitted, and filter messages based on module name.

A `Builder` is consumed by calling [`Builder::init()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/log_fastly/struct.Builder/#method-init) or [`Builder::try_init()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/log_fastly/struct.Builder/#method-try_init) to build and
automatically initialize the logger, or by calling [`Builder::build()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/log_fastly/struct.Builder/#method-build) to build the logger for
manual initialization or nesting within another logger. After you call any of these methods, you
can no longer call any of them on that same builder.

## Methods

### build

**Source:** https://docs.rs/crate/log-fastly/0.13.1/source/src/lib.rs#684

```rust
fn build(&mut self) -> Result<Logger, Error>
```

Build a `Logger`, using up this builder.

This is mainly useful if you want to manually initialize the logger, or nest it within
another [`log::Log`](https://docs.rs/log/0.4.28/log/trait.Log.html) implementation.

This will fail with an `Err` value if:

- This builder has already been used to build a `Logger`.

- Any of the registered endpoint names are invalid.

- Any of the module name filters set by [`Builder::filter_module()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/log_fastly/struct.Builder/#method-filter_module) have invalid
  [regex syntax](https://docs.rs/regex#syntax).

```rust
let logger = log_fastly::Builder::new()
    .default_endpoint("my_endpoint")
    .build()?;
log::set_boxed_logger(Box::new(logger))?;
```

**Parameters**

- `self`: `&mut Self`

**Returns**

`Result<Logger, Error>`

### default_endpoint

**Source:** https://docs.rs/crate/log-fastly/0.13.1/source/src/lib.rs#478

```rust
fn default_endpoint<E>(&mut self, endpoint: E) -> &mut Self
where
    E: TryInto<Endpoint>,
    E::Error: Into<Error>,
```

Set the default endpoint for all messages.

The default endpoint is used when the logging statement does not use the `target:
"endpoint"` syntax.

This overrides any previous default endpoints, set either by this method or by
[`Builder::default_level_endpoint()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/log_fastly/struct.Builder/#method-default_level_endpoint).

```rust
log_fastly::Logger::builder()
    .max_level(log::LevelFilter::Info)
    .default_level_endpoint("error_only", log::Level::Error)
    .default_endpoint("my_endpoint")
    .endpoint("other_endpoint")
    .init();
log::info!("This will be written to my_endpoint...");
log::error!("...and this will too");
log::warn!(target: "other_endpoint", "This will go to other_endpoint, though");
```

**Parameters**

- `self`: `&mut Self`
- `endpoint`: `E`

**Returns**

`&mut Self`

### default_level_endpoint

**Source:** https://docs.rs/crate/log-fastly/0.13.1/source/src/lib.rs#516

```rust
fn default_level_endpoint<E>(&mut self, endpoint: E, level: Level) -> &mut Self
where
    E: TryInto<Endpoint>,
    E::Error: Into<Error>,
```

Set the default endpoint for all messages of the given level.

The default endpoint is used when the logging statement does not use the `target:
"endpoint"` syntax.

This overrides any previous default endpoints set for this level, either by this method or
by [`Builder::default_endpoint()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/log_fastly/struct.Builder/#method-default_endpoint).

```rust
log_fastly::Logger::builder()
    .max_level(log::LevelFilter::Info)
    .default_endpoint("my_endpoint")
    .default_level_endpoint("error_only", log::Level::Error)
    .endpoint("other_endpoint")
    .init();
log::info!("This will be written to my_endpoint...");
log::error!("...but this will be written to error_only");
log::error!(target: "other_endpoint", "This will go to other_endpoint, though");
```

**Parameters**

- `self`: `&mut Self`
- `endpoint`: `E`
- `level`: `Level`

**Returns**

`&mut Self`

### echo_stderr

**Source:** https://docs.rs/crate/log-fastly/0.13.1/source/src/lib.rs#607

```rust
fn echo_stderr(&mut self, enabled: bool) -> &mut Self
```

Set whether all log messages should be echoed to `stderr` (`false` by default).

If this is set to `true`, all logging statements will write the message to `stderr` in
addition to the specified endpoint. This is particularly useful when debugging with
[Compute Log Tailing](https://www.fastly.com/blog/introducing-compute-edge-log-tailing-for-better-observability-and-easier-debugging).

**Parameters**

- `self`: `&mut Self`
- `enabled`: `bool`

**Returns**

`&mut Self`

### echo_stdout

**Source:** https://docs.rs/crate/log-fastly/0.13.1/source/src/lib.rs#595

```rust
fn echo_stdout(&mut self, enabled: bool) -> &mut Self
```

Set whether all log messages should be echoed to `stdout` (`false` by default).

If this is set to `true`, all logging statements will write the message to `stdout` in
addition to the specified endpoint. This is particularly useful when debugging with
[Compute Log Tailing](https://www.fastly.com/blog/introducing-compute-edge-log-tailing-for-better-observability-and-easier-debugging).

**Parameters**

- `self`: `&mut Self`
- `enabled`: `bool`

**Returns**

`&mut Self`

### endpoint

**Source:** https://docs.rs/crate/log-fastly/0.13.1/source/src/lib.rs#425

```rust
fn endpoint<E>(&mut self, endpoint: E) -> &mut Self
where
    E: TryInto<Endpoint>,
    E::Error: Into<Error>,
```

Register an endpoint.

```rust
log_fastly::Logger::builder()
    .max_level(log::LevelFilter::Trace)
    .endpoint("my_endpoint")
    .init();
log::info!(target: "my_endpoint", "Hello");
```

**Parameters**

- `self`: `&mut Self`
- `endpoint`: `E`

**Returns**

`&mut Self`

### endpoint_level

**Source:** https://docs.rs/crate/log-fastly/0.13.1/source/src/lib.rs#447

```rust
fn endpoint_level<E>(&mut self, endpoint: E, level: LevelFilter) -> &mut Self
where
    E: TryInto<Endpoint>,
    E::Error: Into<Error>,
```

Register an endpoint and set the maximum logging level for its messages.

```rust
log_fastly::Logger::builder()
    .max_level(log::LevelFilter::Trace)
    .endpoint_level("debug_endpoint", log::LevelFilter::Debug)
    .init();
log::info!(target: "debug_endpoint", "This will be written to debug_endpoint...");
log::trace!(target: "debug_endpoint", "...but this won't be...");
```

**Parameters**

- `self`: `&mut Self`
- `endpoint`: `E`
- `level`: `LevelFilter`

**Returns**

`&mut Self`

### filter_module

**Source:** https://docs.rs/crate/log-fastly/0.13.1/source/src/lib.rs#578

```rust
fn filter_module(&mut self, regex: &str, level: LevelFilter) -> &mut Self
```

Set a logging level for modules whose names match the given [regular
expression](https://docs.rs/regex#syntax).

By default, logging statements in any module are emitted if their level is within
[`Builder::max_level()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/log_fastly/struct.Builder/#method-max_level) and the level for their target endpoint. If you configure filters
with this method, the name of the module calling the logging statement must also match one
of the patterns, and be within the filter's specified level.

```rust
mod my_module {
    pub fn do_a_thing() {
        log::warn!("This won't be written, because this module's max level is Error...");
        log::error!("...but this will be written");
    }
}

log_fastly::Logger::builder()
    .max_level(log::LevelFilter::Info)
    .default_endpoint("my_endpoint")
    .filter_module("my_module", log::LevelFilter::Error)
    .init();
log::info!("This won't be written, because it's not in my_module");
// This will only emit one log message, because "my_app$" doesn't match, and
// "my_app::my_module" is limited to Error
my_module::do_a_thing();
```

**Parameters**

- `self`: `&mut Self`
- `regex`: `&str`
- `level`: `LevelFilter`

**Returns**

`&mut Self`

### init

**Source:** https://docs.rs/crate/log-fastly/0.13.1/source/src/lib.rs#624

```rust
fn init(&mut self)
```

Build the logger and initialize it as the global logger.

```rust
log_fastly::Builder::new()
    .default_endpoint("my_endpoint")
    .init();
log::info!("Hello");
```

#### Panics

This may panic for any of the reasons that [`Builder::try_init()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/log_fastly/struct.Builder/#method-try_init) would return an error.

**Parameters**

- `self`: `&mut Self`

### max_level

**Source:** https://docs.rs/crate/log-fastly/0.13.1/source/src/lib.rs#545

```rust
fn max_level(&mut self, level: LevelFilter) -> &mut Self
```

Set the maximum logging level for all messages.

No messages that exceed this level will be emitted, even if a higher level is set for a
specific endpoint or module name.

**Note:** The default level is `LevelFilter::Off`, which emits no logging at all. You'll
want to change this for most configurations.

```rust
log_fastly::Builder::new()
    .max_level(log::LevelFilter::Warn)
    .endpoint_level("my_endpoint", log::LevelFilter::Info)
    .init();
log::warn!(target: "my_endpoint", "This will be written to my_endpoint...");
log::info!(target: "my_endpoint", "...but this won't");
```

**Parameters**

- `self`: `&mut Self`
- `level`: `LevelFilter`

**Returns**

`&mut Self`

### new

**Source:** https://docs.rs/crate/log-fastly/0.13.1/source/src/lib.rs#384

```rust
fn new() -> Self
```

Create a new `Builder`.

By default, no endpoints are registered, the maximum log level is set to `Off`, and no
module name filtering is done.

**Returns**

`Self`

### try_init

**Source:** https://docs.rs/crate/log-fastly/0.13.1/source/src/lib.rs#642

```rust
fn try_init(&mut self) -> Result<(), Error>
```

Build the logger and initialize it as the global logger.

This will fail with an `Err` value if a global logger is already initialized, or for any of
the reasons that [`Builder::build()`](https://www.fastly.com/documentation/reference/compute/sdks/rust/log_fastly/struct.Builder/#method-build) can fail.

```rust
log_fastly::Builder::new()
    .default_endpoint("my_endpoint")
    .try_init()?;
log::info!("Hello");
```

**Parameters**

- `self`: `&mut Self`

**Returns**

`Result<(), Error>`

## Trait implementations

### impl Debug for Builder

#### fmt

**Source:** https://docs.rs/crate/log-fastly/0.13.1/source/src/lib.rs#352

```rust
fn fmt(&self, f: &mut Formatter<'_>) -> Result
```

**Parameters**

- `self`: `&Self`
- `f`: `&mut $crate::fmt::Formatter`

**Returns**

`$crate::fmt::Result`

### impl Default for Builder

#### default

**Source:** https://docs.rs/crate/log-fastly/0.13.1/source/src/lib.rs#374

```rust
fn default() -> Self
```

**Returns**

`Self`

## Auto trait implementations

- `Freeze`
- `RefUnwindSafe`
- `Send`
- `Sync`
- `Unpin`
- `UnsafeUnpin`
- `UnwindSafe`

## Blanket implementations

- `Any`
- `Borrow<T>`
- `BorrowMut<T>`
- `Downcast`
- `DowncastSync`
- `ErasedDestructor`
- `From<T>`
- `Into<U>`
- `IntoEither`
- `Same`
- `TryFrom<U>`
- `TryInto<U>`
