Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
2115: Add support for setting module log levels via the LoggerConfig r=jojolepro a=bonsairobo

## Description

Make it so you set your module log levels without recompiling, as so:

```
amethyst::Logger::from_config(LoggerConfig::load(&logger_config_path)?).start();
```

## Additions

- Add a field to the `LoggerConfig` that represents the log levels for some set of modules.
- Add `#[serde(default)]` to the `LoggerConfig` so it's easier to configure the logger from a RON file.

## PR Checklist

By placing an x in the boxes I certify that I have:

- [ ] Updated the content of the book if this PR would make the book outdated.
- [x] Added a changelog entry if this will impact users, or modified more than 5 lines of Rust that wasn't a doc comment.
- [ ] Added unit tests for new code added in this PR.
- [x] Acknowledged that by making this pull request I release this code under an MIT/Apache 2.0 dual licensing scheme.

If this modified or created any rs files:

- [x] Ran `cargo +stable fmt --all`
- [x] Ran `cargo clippy --all --features "empty"`
- [x] Ran `cargo test --all --features "empty"`


Co-authored-by: Duncan <[email protected]>
  • Loading branch information
bors[bot] and bonsairobo authored Feb 1, 2020
2 parents a34ede1 + d7b32ed commit 9590395
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ The format is based on [Keep a Changelog][kc], and this project adheres to

### Added

- Support settings module log levels from a RON file ([#2115])

### Changed

- Re-export `TargetedEvent` from amethyst_ui. ([#2114])
Expand All @@ -26,6 +28,7 @@ The format is based on [Keep a Changelog][kc], and this project adheres to
### Security

[#2114]: https://github.com/amethyst/amethyst/pull/2114
[#2115]: https://github.com/amethyst/amethyst/pull/2115

## [0.14.0] - 2020-01-30

Expand Down
16 changes: 10 additions & 6 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub use log::LevelFilter;
use log::debug;
use serde::{Deserialize, Serialize};

use std::{env, fmt, io, path::PathBuf, str::FromStr};
use std::{borrow::Cow, env, fmt, io, path::PathBuf, str::FromStr};

/// An enum that contains options for logging to the terminal.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)]
Expand All @@ -18,6 +18,7 @@ pub enum StdoutLog {

/// Logger configuration object.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct LoggerConfig {
/// Determines whether to log to the terminal or not.
pub stdout: StdoutLog,
Expand All @@ -31,6 +32,8 @@ pub struct LoggerConfig {
pub log_gfx_backend_level: Option<LevelFilter>,
/// Sets a different level for gfx_rendy if Some
pub log_gfx_rendy_level: Option<LevelFilter>,
/// Sets the levels for specific modules.
pub module_levels: Vec<(String, LevelFilter)>,
}

impl Default for LoggerConfig {
Expand All @@ -42,6 +45,7 @@ impl Default for LoggerConfig {
allow_env_override: true,
log_gfx_backend_level: Some(LevelFilter::Warn),
log_gfx_rendy_level: Some(LevelFilter::Warn),
module_levels: Vec::new(),
}
}
}
Expand Down Expand Up @@ -149,6 +153,10 @@ impl Logger {
.level_for("rendy_wsi", LevelFilter::Warn);
}

for (module, level) in config.module_levels.into_iter() {
logger.dispatch = logger.dispatch.level_for(Cow::Owned(module), level);
}

if let Some(path) = config.log_file {
if let Ok(log_file) = fern::log_file(path) {
logger.dispatch = logger.dispatch.chain(log_file)
Expand Down Expand Up @@ -177,11 +185,7 @@ impl Logger {
}

/// Set individual log levels for modules.
pub fn level_for<T: Into<std::borrow::Cow<'static, str>>>(
mut self,
module: T,
level: LevelFilter,
) -> Self {
pub fn level_for<T: Into<Cow<'static, str>>>(mut self, module: T, level: LevelFilter) -> Self {
self.dispatch = self.dispatch.level_for(module, level);
self
}
Expand Down

0 comments on commit 9590395

Please sign in to comment.