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