wasmcloud_runtime/component/
logging.rs

1use async_trait::async_trait;
2use tracing::instrument;
3
4use crate::capability::logging::logging;
5
6use super::{Ctx, Handler};
7
8pub mod unversioned_logging_bindings {
9    wasmtime::component::bindgen!({
10        world: "unversioned-logging",
11        async: true,
12        with: {
13           "wasi:logging/logging": crate::capability::unversioned_logging,
14        },
15    });
16}
17
18pub mod logging_bindings {
19    wasmtime::component::bindgen!({
20        world: "logging",
21        async: true,
22        with: {
23           "wasi:logging/logging": crate::capability::logging,
24        },
25    });
26}
27
28/// `wasi:logging/logging` implementation
29#[async_trait]
30pub trait Logging {
31    /// Handle `wasi:logging/logging.log`
32    async fn log(
33        &self,
34        level: logging::Level,
35        context: String,
36        message: String,
37    ) -> anyhow::Result<()>;
38}
39
40#[async_trait]
41impl<H: Handler> logging::Host for Ctx<H> {
42    #[instrument(skip_all)]
43    async fn log(
44        &mut self,
45        level: logging::Level,
46        context: String,
47        message: String,
48    ) -> anyhow::Result<()> {
49        self.attach_parent_context();
50        self.handler.log(level, context, message).await
51    }
52}
53
54#[async_trait]
55impl<H: Handler> crate::capability::unversioned_logging::logging::Host for Ctx<H> {
56    #[instrument(skip_all)]
57    async fn log(
58        &mut self,
59        level: crate::capability::unversioned_logging::logging::Level,
60        context: String,
61        message: String,
62    ) -> anyhow::Result<()> {
63        self.attach_parent_context();
64        // NOTE(thomastaylor312): I couldn't figure out the proper incantation for using `with` to
65        // avoid this. If there is a better way, we can fix it
66        use crate::capability::unversioned_logging::logging::Level;
67        let level = match level {
68            Level::Trace => logging::Level::Trace,
69            Level::Debug => logging::Level::Debug,
70            Level::Info => logging::Level::Info,
71            Level::Warn => logging::Level::Warn,
72            Level::Error => logging::Level::Error,
73            Level::Critical => logging::Level::Critical,
74        };
75        self.handler.log(level, context, message).await
76    }
77}