Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow customizing log prefixes for wasmtime serve command #9821

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading