Skip to content

Commit

Permalink
allow customizing log prefixes for wasmtime serve command (#9821)
Browse files Browse the repository at this point in the history
* allow customizing log prefixes for wasmtime serve command

Signed-off-by: Joseph Zhang <jzhang@absolute.com>

* pr feedback - use simple boolean flag instead

---------

Signed-off-by: Joseph Zhang <jzhang@absolute.com>
  • Loading branch information
jzhn authored Dec 16, 2024
1 parent 392df4a commit d3f05ee
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/commands/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,14 @@ pub struct ServeCommand {
run: RunCommon,

/// Socket address for the web server to bind to.
#[arg(long = "addr", value_name = "SOCKADDR", default_value_t = DEFAULT_ADDR )]
#[arg(long = "addr", value_name = "SOCKADDR", default_value_t = DEFAULT_ADDR)]
addr: SocketAddr,

/// Disable log prefixes of wasi-http handlers.
/// if unspecified, logs will be prefixed with 'stdout|stderr [{req_id}] :: '
#[arg(long = "no-logging-prefix")]
no_logging_prefix: bool,

/// The WebAssembly component to run.
#[arg(value_name = "WASM", required = true)]
component: PathBuf,
Expand Down Expand Up @@ -153,15 +158,17 @@ impl ServeCommand {

builder.env("REQUEST_ID", req_id.to_string());

builder.stdout(LogStream::new(
format!("stdout [{req_id}] :: "),
Output::Stdout,
));

builder.stderr(LogStream::new(
format!("stderr [{req_id}] :: "),
Output::Stderr,
));
let stdout_prefix: String;
let stderr_prefix: String;
if self.no_logging_prefix {
stdout_prefix = "".to_string();
stderr_prefix = "".to_string();
} else {
stdout_prefix = format!("stdout [{req_id}] :: ");
stderr_prefix = format!("stderr [{req_id}] :: ");
}
builder.stdout(LogStream::new(stdout_prefix, Output::Stdout));
builder.stderr(LogStream::new(stderr_prefix, Output::Stderr));

let mut host = Host {
table: wasmtime::component::ResourceTable::new(),
Expand Down
46 changes: 46 additions & 0 deletions tests/all/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,52 @@ stderr [1] :: after empty
Ok(())
}

#[tokio::test]
async fn cli_serve_with_print_no_prefix() -> Result<()> {
let server = WasmtimeServe::new(CLI_SERVE_WITH_PRINT_COMPONENT, |cmd| {
cmd.arg("-Scli");
cmd.arg("--no-logging-prefix");
})?;

for _ in 0..2 {
let resp = server
.send_request(
hyper::Request::builder()
.uri("http://localhost/")
.body(String::new())
.context("failed to make request")?,
)
.await?;
assert!(resp.status().is_success());
}

let (out, err) = server.finish()?;
assert_eq!(
out,
"\
this is half a print to stdout
\n\
after empty
this is half a print to stdout
\n\
after empty
"
);
assert_eq!(
err,
"\
this is half a print to stderr
\n\
after empty
this is half a print to stderr
\n\
after empty
"
);

Ok(())
}

#[tokio::test]
async fn cli_serve_authority_and_scheme() -> Result<()> {
let server = WasmtimeServe::new(CLI_SERVE_AUTHORITY_AND_SCHEME_COMPONENT, |cmd| {
Expand Down

0 comments on commit d3f05ee

Please sign in to comment.