Merge pull request #12 from fluencelabs/logger_env

Initialize logger based on the WASM_LOG env variable
This commit is contained in:
vms 2020-12-09 12:11:55 +03:00 committed by GitHub
commit b5b9dcb47d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 6 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "fluence"
version = "0.2.9" # remember to update html_root_url
version = "0.2.10" # remember to update html_root_url
description = "Fluence backend SDK for developing backend applications for the Fluence network"
documentation = "https://docs.rs/fluence/"
repository = "https://github.com/fluencelabs/rust-sdk"

View File

@ -48,6 +48,8 @@ pub use export_allocator::deallocate;
pub use logger::WasmLoggerBuilder;
#[cfg(feature = "logger")]
pub use logger::TargetMap;
#[cfg(feature = "logger")]
pub use logger::WASM_LOG_ENV_NAME;
pub use result::get_result_ptr;
pub use result::get_result_size;

View File

@ -42,6 +42,14 @@
//! [`WasmLogger`]: struct.WasmLogger.html
//! [`log`]: https://docs.rs/log
use log::Level as LogLevel;
/// By default, logger will be initialized with log level from this environment variable.
pub const WASM_LOG_ENV_NAME: &'static str = "WASM_LOG";
/// If WASM_LOG_ENV isn't set, then this level will be used as the default.
const WASM_DEFAULT_LOG_LEVEL: LogLevel = LogLevel::Info;
pub type TargetMap = std::collections::HashMap<&'static str, i64>;
/// The Wasm Logger.
@ -67,12 +75,19 @@ pub struct WasmLoggerBuilder {
}
impl WasmLoggerBuilder {
/// Initializes a builder of the global logger with log level set to `Level::Info`.
/// It is a initial method in this builder chain, please note, that logger wouldn't work without
/// subsequent build() call.
/// Initializes a builder of the global logger. Set log level based on the WASM_LOG environment variable if it set,
/// or [[WASM_DEFAULT_LOG_LEVEL]] otherwise. It is an initial method in this builder chain, please note,
/// that logger wouldn't work without subsequent build() call.
pub fn new() -> Self {
use std::str::FromStr;
let log_level = std::env::var(WASM_LOG_ENV_NAME)
.map_or(WASM_DEFAULT_LOG_LEVEL, |log_level_str| {
LogLevel::from_str(&log_level_str).unwrap_or(WASM_DEFAULT_LOG_LEVEL)
});
Self {
log_level: log::Level::Info,
log_level,
target_map: <_>::default(),
}
}

View File

@ -55,7 +55,7 @@
//! pub fn curl_get(url: String) -> String;
//! }
//! ```
#![doc(html_root_url = "https://docs.rs/fluence/0.2.9")]
#![doc(html_root_url = "https://docs.rs/fluence/0.2.10")]
#![deny(
dead_code,
nonstandard_style,