Skip to content

Commit d036c4f

Browse files
authored
Add support for manually configuring log level via code (#1600)
- **Allow setting env variables in from user code.** - **add missing files** - **update generated code** <!-- ELLIPSIS_HIDDEN --> ---- > [!IMPORTANT] > Introduce `baml-log` library for configurable logging, replacing existing logging mechanisms and adding tests for functionality. > > - **Logging Library**: > - Introduces `baml-log` library for logging with support for JSON and text formats. > - Allows dynamic configuration of log levels and formats via environment variables and code. > - Replaces existing logging with `baml-log` in `baml-runtime`, `cli`, `language_client_python`, `language_client_ruby`, and `language_client_typescript`. > - **Configuration**: > - Adds functions to set log level, JSON mode, and max message length in `baml-log`. > - Supports environment variables `BAML_LOG`, `BAML_LOG_JSON`, `BAML_LOG_MAX_MESSAGE_LENGTH`, and `BAML_LOG_COLOR_MODE`. > - **Testing**: > - Adds tests for logging functionality in `logger.test.ts`. > - Tests cover setting log levels and capturing stdout to verify log output. > - **Dependencies**: > - Updates `Cargo.lock` and `Cargo.toml` to include `baml-log` and related dependencies. > - Updates Python and TypeScript dependencies to support new logging features. > > <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 dbd53f2. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN -->
1 parent c5fb371 commit d036c4f

118 files changed

Lines changed: 3959 additions & 1338 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

engine/Cargo.lock

Lines changed: 38 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

engine/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ array-macro = "2"
3434
askama = "0.12.1"
3535
baml-cli = { path = "cli" }
3636
baml-types = { path = "baml-lib/baml-types" }
37+
baml-log = { path = "baml-lib/baml-log" }
3738
base64 = "0.22.1"
3839
bstd = { path = "bstd" }
3940
bytes = "1.6.0"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
edition = "2021"
3+
name = "baml-log"
4+
version.workspace = true
5+
authors.workspace = true
6+
license-file.workspace = true
7+
description = "Logging library for BAML for end user logging"
8+
9+
[dependencies]
10+
serde = { version = "1.0", features = ["derive"] }
11+
serde_json.workspace = true
12+
anyhow.workspace = true
13+
thiserror = "2.0.12"
14+
colored = "2"
15+
lazy_static = "1.5.0"
16+
chrono = "0.4.40"
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
use crate::logger::Loggable;
2+
use crate::Level;
3+
use serde_json::Value;
4+
5+
/// Creates a JSON-serializable event for structured logging
6+
pub struct Event<'a, T>
7+
where
8+
T: Loggable,
9+
{
10+
/// Log level
11+
pub level: Level,
12+
/// Event name
13+
pub name: &'a str,
14+
/// Event payload
15+
pub payload: T,
16+
/// Module path
17+
pub module_path: Option<&'a str>,
18+
/// File
19+
pub file: Option<&'a str>,
20+
/// Line
21+
pub line: Option<u32>,
22+
}
23+
24+
impl<'a, T> Event<'a, T>
25+
where
26+
T: Loggable,
27+
{
28+
pub fn new(
29+
level: Level,
30+
name: &'a str,
31+
payload: T,
32+
module_path: Option<&'a str>,
33+
file: Option<&'a str>,
34+
line: Option<u32>,
35+
) -> Self {
36+
Self {
37+
level,
38+
name,
39+
payload,
40+
module_path,
41+
file,
42+
line,
43+
}
44+
}
45+
}
46+
47+
/// Logs a structured event at the specified level
48+
///
49+
/// This can be used for structured logging that works with both regular and JSON formats.
50+
/// When in JSON mode, the payload is serialized as a JSON object.
51+
/// When in regular mode, the payload is formatted as a string.
52+
///
53+
/// # Example
54+
///
55+
/// ```
56+
/// use baml_log::event;
57+
/// use baml_log::Level;
58+
/// use serde::Serialize;
59+
///
60+
/// #[derive(Serialize)]
61+
/// struct UserEvent {
62+
/// user_id: String,
63+
/// action: String,
64+
/// }
65+
///
66+
/// // Log a structured event
67+
/// event!(
68+
/// Level::Info,
69+
/// "user_action",
70+
/// UserEvent {
71+
/// user_id: "123".to_string(),
72+
/// action: "login".to_string(),
73+
/// }
74+
/// );
75+
/// ```
76+
#[macro_export]
77+
macro_rules! event {
78+
($level:expr, $payload:expr) => {
79+
$crate::log_event_internal(
80+
$level,
81+
&$payload,
82+
Some(module_path!()),
83+
Some(file!()),
84+
Some(line!()),
85+
)
86+
};
87+
}
88+
89+
#[macro_export]
90+
macro_rules! elog {
91+
($level:expr, $payload:expr) => {
92+
$crate::log_event_internal(
93+
$level,
94+
$payload,
95+
Some(module_path!()),
96+
Some(file!()),
97+
Some(line!()),
98+
)
99+
};
100+
}
101+
102+
/// Log an event at the ERROR level
103+
#[macro_export]
104+
macro_rules! eerror {
105+
($payload:expr) => {
106+
$crate::event!($crate::Level::Error, $payload)
107+
};
108+
}
109+
110+
/// Log an event at the WARN level
111+
#[macro_export]
112+
macro_rules! ewarn {
113+
($payload:expr) => {
114+
$crate::event!($crate::Level::Warn, $payload)
115+
};
116+
}
117+
118+
/// Log an event at the INFO level
119+
#[macro_export]
120+
macro_rules! einfo {
121+
($payload:expr) => {
122+
$crate::event!($crate::Level::Info, $payload)
123+
};
124+
}
125+
126+
/// Log an event at the DEBUG level
127+
#[macro_export]
128+
macro_rules! edebug {
129+
($payload:expr) => {
130+
$crate::event!($crate::Level::Debug, $payload)
131+
};
132+
}
133+
134+
/// Log an event at the TRACE level
135+
#[macro_export]
136+
macro_rules! etrace {
137+
($payload:expr) => {
138+
$crate::event!($crate::Level::Trace, $payload)
139+
};
140+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//! A custom logging library for BAML that supports JSON and standard formats.
2+
//!
3+
//! This crate provides a simple logging interface similar to the standard `log` crate
4+
//! but with separate configuration controlled by BAML-specific environment variables.
5+
//!
6+
//! # Features
7+
//!
8+
//! - Controlled by BAML-specific environment variables
9+
//! - Support for both text and JSON log formats
10+
//! - Dynamic configuration changes at runtime
11+
//! - Log level filtering
12+
//! - File, line, and module path information
13+
//!
14+
//! # Environment Variables
15+
//!
16+
//! - `BAML_LOG`: Sets the log level (error, warn, info, debug, trace)
17+
//! - `BAML_LOG_JSON`: Enables JSON formatting when set to "true" or "1"
18+
//! - `BAML_LOG_STYLE`: Controls color output ("auto", "always", "never")
19+
//!
20+
//! # Example
21+
//!
22+
//! ```
23+
//! use baml_log::{binfo, bwarn, berror, bdebug, btrace};
24+
//! use baml_log::{Level, set_log_level};
25+
//!
26+
//! // Initialize the logger (optional)
27+
//! baml_log::init();
28+
//!
29+
//! // Log messages at different levels
30+
//! binfo!("This is an info message");
31+
//! bwarn!("This is a warning: {}", "something went wrong");
32+
//!
33+
//! // Dynamically change the log level
34+
//! set_log_level(Level::Debug);
35+
//! bdebug!("This debug message is now visible");
36+
//! ```
37+
38+
// Export the macros
39+
#[macro_use]
40+
mod macros;
41+
42+
#[macro_use]
43+
mod event;
44+
45+
mod logger;
46+
47+
// Re-export the core types and functions
48+
pub use logger::{
49+
get_log_level, init, log_event_internal, log_internal, reload_from_env, set_color_mode,
50+
set_from_env, set_json_mode, set_log_level, set_max_message_length, Level, LogError, Loggable,
51+
MaxMessageLength,
52+
};
53+
54+
pub use crate::{
55+
bdebug as debug, berror as error, binfo as info, blog as log, btrace as trace, bwarn as warn,
56+
};
57+
58+
// Provide a prelude for easy imports
59+
pub mod prelude {
60+
pub use crate::{init, set_color_mode, set_json_mode, set_log_level, Level, LogError};
61+
}

0 commit comments

Comments
 (0)