mirror of
https://github.com/fluencelabs/marine-rs-sdk-test
synced 2025-03-15 04:30:50 +00:00
Merge pull request #12 from fluencelabs/logger_env
Initialize logger based on the WASM_LOG env variable
This commit is contained in:
commit
b5b9dcb47d
@ -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"
|
||||
|
@ -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;
|
||||
|
@ -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(),
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user