Skip to content

Commit

Permalink
json log (#1137)
Browse files Browse the repository at this point in the history
Add BAML_LOG_JSON environment variable to enable logging baml_events and
other logs as json blobs. Serialize all the data as json.

<!-- ELLIPSIS_HIDDEN -->


----

> [!IMPORTANT]
> Add JSON logging to BAML server using `tracing` and
`tracing-subscriber`, configurable via environment variable.
> 
>   - **Behavior**:
> - Add JSON logging support using `tracing` and `tracing-subscriber` in
`baml-runtime` and `cli`.
> - JSON logging can be enabled via `BAML_LOG_JSON` environment
variable.
>     - Logs include detailed event and metadata information.
>   - **Configuration**:
> - Update `Cargo.toml` and `Cargo.lock` to include `tracing`,
`tracing-subscriber`, and related dependencies.
>     - Add `rustflags` for `tracing_unstable` in `.cargo/config.toml`.
>   - **Logging**:
> - Implement `Visualize` trait for structured logging in
`tracing/mod.rs`.
> - Add `BamlEventJson` and `TokenUsage` structs for JSON log
serialization.
> - Modify `main.rs` in `cli` to initialize logging based on environment
variable.
>   - **Misc**:
>     - Comment out unused `println` in `old_mod.rs`.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup>
for 46d35c5. It will automatically
update as commits are pushed.</sup>


<!-- ELLIPSIS_HIDDEN -->
  • Loading branch information
aaronvg authored Nov 4, 2024
1 parent 6813c2c commit f140767
Show file tree
Hide file tree
Showing 12 changed files with 2,166 additions and 1,507 deletions.
4 changes: 4 additions & 0 deletions engine/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# to enable json logging we need this serialization feaure. https://docs.rs/tracing-subscriber/latest/tracing_subscriber/#unstable-features
[build]
rustflags = ["--cfg", "tracing_unstable"]

# https://github.com/rust-lang/cargo/issues/8607
[target.x86_64-unknown-linux-musl]
rustflags = ["-C", "target-feature=-crt-static"]
Expand Down
47 changes: 47 additions & 0 deletions engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion engine/baml-lib/jsonish/src/jsonish/old/old_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl Target {
match res {
Ok(value) => {
if !state.is_empty() {
println!("{:?}", state);
// println!("{:?}", state);
}
Ok(value)
}
Expand Down
8 changes: 8 additions & 0 deletions engine/baml-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ ambassador = "0.4.0"
aws-smithy-json = "0.60.7"
jsonwebtoken = "9.3.0"
pretty_assertions = "1.4.0"
valuable = { version = "0.1.0", features = ["derive"] }
tracing = { version = "0.1.40", features = ["valuable"] }
tracing-subscriber = { version = "0.3.18", features = ["json", "env-filter","valuable"] }


[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down Expand Up @@ -128,11 +131,16 @@ reqwest.workspace = true
walkdir = "2.5.0"
which = "6.0.3"




[features]
defaults = []
internal = []
skip-integ-tests = []



[dev-dependencies]
assert_cmd = "2"
console_log = "1"
Expand Down
14 changes: 12 additions & 2 deletions engine/baml-runtime/src/internal/llm_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ pub struct LLMCompleteResponseMetadata {
pub total_tokens: Option<u64>,
}

// This is how the response gets logged if you print the result to the console.
impl std::fmt::Display for LLMCompleteResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(
Expand All @@ -289,17 +290,26 @@ impl std::fmt::Display for LLMCompleteResponse {
}
}

// This is the one that gets logged by BAML_LOG, for baml_events log.
impl crate::tracing::Visualize for LLMCompleteResponse {
fn visualize(&self, max_chunk_size: usize) -> String {
let s = vec![
format!(
"{}",
format!(
"Client: {} ({}) - {}ms. StopReason: {}",
"Client: {} ({}) - {}ms. StopReason: {}. Tokens(in/out): {}/{}",
self.client,
self.model,
self.latency.as_millis(),
self.metadata.finish_reason.as_deref().unwrap_or("unknown")
self.metadata.finish_reason.as_deref().unwrap_or("unknown"),
self.metadata
.prompt_tokens
.map(|t| t.to_string())
.unwrap_or_else(|| "unknown".to_string()),
self.metadata
.output_tokens
.map(|t| t.to_string())
.unwrap_or_else(|| "unknown".to_string()),
)
.yellow()
),
Expand Down
Loading

0 comments on commit f140767

Please sign in to comment.