3个版本
0.1.2 | 2021年4月16日 |
---|---|
0.1.1 | 2021年2月17日 |
0.1.0 | 2021年2月10日 |
#1610 in WebAssembly
12KB
142 行代码(不含注释)
wasmCloud Logging Actor Interface
此crate提供了对wasmcloud:logging
合约的抽象。这允许演员使用正常的日志宏(如info!
、warn!
、error!
等)在演员中写入日志。
示例
extern crate wasmcloud_actor_http_server as http;
extern crate wasmcloud_actor_logging as logging;
extern crate wasmcloud_actor_core as actor;
use wapc_guest::HandlerResult;
use http::{Request, Response, Handlers};
use log::{info, warn, error, trace, debug};
#[actor::init]
pub fn init() {
http::Handlers::register_handle_request(method_logger);
/// Initialize the logger to enable log macros
logging::enable_macros();
}
/// Actor must be signed with `wasmcloud:logging` to log messages
fn method_logger(msg: http::Request) -> HandlerResult<http::Response> {
/// Logs can be directly written via `write_log`
logging::default().write_log("", "trace", "Coercing Rust String to str");
/// After initialization, logs can be directly written from the actor using macros
match &*msg.method {
"GET" => info!("Received a GET request"),
"POST" => info!("Received a POST request"),
"PUT" => info!("Received a PUT request"),
"DELETE" => warn!("Received a DELETE request"),
req => error!("Received an unsupported HTTP Request: {}", req),
};
debug!("Finished matching HTTP method, returning OK");
Ok(http::Response::ok())
}
依赖项
~0.8–1.6MB
~33K SLoC