Skip to content

Commit

Permalink
Fix baml_log_json logs for py, ruby, and TS (#1153)
Browse files Browse the repository at this point in the history
<!-- ELLIPSIS_HIDDEN -->



> [!IMPORTANT]
> Adds JSON logging support for Python, Ruby, and TypeScript language
clients using `tracing-subscriber`, controlled by the `BAML_LOG_JSON`
environment variable.
> 
>   - **Behavior**:
> - Adds JSON logging support for Python, Ruby, and TypeScript language
clients by using `tracing-subscriber`.
> - Checks `BAML_LOG_JSON` environment variable to determine if JSON
logging is enabled.
> - If JSON logging is enabled, configures `tracing-subscriber` with
JSON formatting and environment filter.
> - If JSON logging is not enabled, falls back to `env_logger` for
regular logging.
>   - **Dependencies**:
> - Adds `tracing-subscriber` to `Cargo.toml` for Python, Ruby, and
TypeScript language clients.
>   - **Files**:
> - Updates `lib.rs` in `language_client_python`,
`language_client_ruby/ext/ruby_ffi`, and `language_client_typescript` to
implement the logging changes.
> 
> <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 86ad707. It will automatically
update as commits are pushed.</sup>

<!-- ELLIPSIS_HIDDEN -->
  • Loading branch information
aaronvg authored Nov 10, 2024
1 parent c6b1167 commit 9e08642
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 18 deletions.
3 changes: 3 additions & 0 deletions engine/Cargo.lock

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

1 change: 1 addition & 0 deletions engine/language_client_python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ regex.workspace = true
serde.workspace = true
serde_json.workspace = true
tokio = { version = "1", features = ["full"] }
tracing-subscriber = { version = "0.3.18", features = ["json", "env-filter","valuable"] }

[build-dependencies]
pyo3-build-config = "0.21.2"
35 changes: 29 additions & 6 deletions engine/language_client_python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod types;

use pyo3::prelude::{pyfunction, pymodule, PyAnyMethods, PyModule, PyResult};
use pyo3::{wrap_pyfunction, Bound, Python};
use tracing_subscriber::{self, EnvFilter};

#[pyfunction]
fn invoke_runtime_cli(py: Python) -> PyResult<()> {
Expand All @@ -21,14 +22,36 @@ fn invoke_runtime_cli(py: Python) -> PyResult<()> {

#[pymodule]
fn baml_py(m: Bound<'_, PyModule>) -> PyResult<()> {
if let Err(e) = env_logger::try_init_from_env(
env_logger::Env::new()
.filter("BAML_LOG")
.write_style("BAML_LOG_STYLE"),
) {
eprintln!("Failed to initialize BAML logger: {:#}", e);
let use_json = match std::env::var("BAML_LOG_JSON") {
Ok(val) => val.trim().eq_ignore_ascii_case("true") || val.trim() == "1",
Err(_) => false,
};

if use_json {
// JSON formatting
tracing_subscriber::fmt()
.with_target(false)
.with_file(false)
.with_line_number(false)
.json()
.with_env_filter(
EnvFilter::try_from_env("BAML_LOG").unwrap_or_else(|_| EnvFilter::new("info")),
)
.flatten_event(true)
.with_current_span(false)
.with_span_list(false)
.init();
} else {
// Regular formatting
if let Err(e) = env_logger::try_init_from_env(
env_logger::Env::new()
.filter("BAML_LOG")
.write_style("BAML_LOG_STYLE"),
) {
eprintln!("Failed to initialize BAML logger: {:#}", e);
}
}

m.add_class::<runtime::BamlRuntime>()?;

m.add_class::<types::FunctionResult>()?;
Expand Down
1 change: 1 addition & 0 deletions engine/language_client_ruby/ext/ruby_ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ serde.workspace = true
serde_json.workspace = true
serde_magnus = { git = "https://github.com/BoundaryML/serde-magnus.git", branch = "sam/magnus-0.7.1" }
tokio = { version = "1", features = ["full"] }
tracing-subscriber = { version = "0.3.18", features = ["json", "env-filter","valuable"] }

[dev-dependencies.magnus]
version = "0.7.1"
Expand Down
35 changes: 29 additions & 6 deletions engine/language_client_ruby/ext/ruby_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use magnus::{class, function, method, prelude::*, Error, RHash, Ruby};
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tracing_subscriber::EnvFilter;

use function_result::FunctionResult;
use function_result_stream::FunctionResultStream;
Expand Down Expand Up @@ -201,14 +202,36 @@ fn invoke_runtime_cli(ruby: &Ruby, argv0: String, argv: Vec<String>) -> Result<(

#[magnus::init(name = "ruby_ffi")]
fn init(ruby: &Ruby) -> Result<()> {
if let Err(e) = env_logger::try_init_from_env(
env_logger::Env::new()
.filter("BAML_LOG")
.write_style("BAML_LOG_STYLE"),
) {
eprintln!("Failed to initialize BAML logger: {:#}", e);
let use_json = match std::env::var("BAML_LOG_JSON") {
Ok(val) => val.trim().eq_ignore_ascii_case("true") || val.trim() == "1",
Err(_) => false,
};

if use_json {
// JSON formatting
tracing_subscriber::fmt()
.with_target(false)
.with_file(false)
.with_line_number(false)
.json()
.with_env_filter(
EnvFilter::try_from_env("BAML_LOG").unwrap_or_else(|_| EnvFilter::new("info")),
)
.flatten_event(true)
.with_current_span(false)
.with_span_list(false)
.init();
} else {
// Regular formatting
if let Err(e) = env_logger::try_init_from_env(
env_logger::Env::new()
.filter("BAML_LOG")
.write_style("BAML_LOG_STYLE"),
) {
eprintln!("Failed to initialize BAML logger: {:#}", e);
}
}

let module = ruby.define_module("Baml")?.define_module("Ffi")?;

module.define_module_function("invoke_runtime_cli", function!(invoke_runtime_cli, 2))?;
Expand Down
1 change: 1 addition & 0 deletions engine/language_client_typescript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ napi-derive = "2"
serde.workspace = true
serde_json.workspace = true
tokio = { version = "1", features = ["full"] }
tracing-subscriber = { version = "0.3.18", features = ["json", "env-filter","valuable"] }

[build-dependencies]
napi-build = "2.1.3"
36 changes: 30 additions & 6 deletions engine/language_client_typescript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod runtime;
mod types;

pub(crate) use runtime::BamlRuntime;
use tracing_subscriber::{self, EnvFilter};

#[napi(js_name = "invoke_runtime_cli")]
pub fn run_cli(env: Env, params: Vec<String>) -> napi::Result<JsUndefined> {
Expand All @@ -21,11 +22,34 @@ pub fn run_cli(env: Env, params: Vec<String>) -> napi::Result<JsUndefined> {

#[napi::module_init]
fn module_init() {
if let Err(e) = env_logger::try_init_from_env(
env_logger::Env::new()
.filter("BAML_LOG")
.write_style("BAML_LOG_STYLE"),
) {
eprintln!("Failed to initialize BAML logger: {:#}", e);
// Check if JSON logging is enabled
let use_json = match std::env::var("BAML_LOG_JSON") {
Ok(val) => val.trim().eq_ignore_ascii_case("true") || val.trim() == "1",
Err(_) => false,
};

if use_json {
// JSON formatting
tracing_subscriber::fmt()
.with_target(false)
.with_file(false)
.with_line_number(false)
.json()
.with_env_filter(
EnvFilter::try_from_env("BAML_LOG").unwrap_or_else(|_| EnvFilter::new("info")),
)
.flatten_event(true)
.with_current_span(false)
.with_span_list(false)
.init();
} else {
// Regular formatting
if let Err(e) = env_logger::try_init_from_env(
env_logger::Env::new()
.filter("BAML_LOG")
.write_style("BAML_LOG_STYLE"),
) {
eprintln!("Failed to initialize BAML logger: {:#}", e);
}
}
}

0 comments on commit 9e08642

Please sign in to comment.