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

Add support for manually configuring log level via code #1600

Open
wants to merge 8 commits into
base: canary
Choose a base branch
from
Open
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
51 changes: 38 additions & 13 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/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ array-macro = "2"
askama = "0.12.1"
baml-cli = { path = "cli" }
baml-types = { path = "baml-lib/baml-types" }
baml-log = { path = "baml-lib/baml-log" }
base64 = "0.22.1"
bstd = { path = "bstd" }
bytes = "1.6.0"
Expand Down
16 changes: 16 additions & 0 deletions engine/baml-lib/baml-log/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
edition = "2021"
name = "baml-log"
version.workspace = true
authors.workspace = true
license-file.workspace = true
description = "Logging library for BAML for end user logging"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json.workspace = true
anyhow.workspace = true
thiserror = "2.0.12"
colored = "2"
lazy_static = "1.5.0"
chrono = "0.4.40"
140 changes: 140 additions & 0 deletions engine/baml-lib/baml-log/src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
use crate::logger::Loggable;
use crate::Level;
use serde_json::Value;

/// Creates a JSON-serializable event for structured logging
pub struct Event<'a, T>
where
T: Loggable,
{
/// Log level
pub level: Level,
/// Event name
pub name: &'a str,
/// Event payload
pub payload: T,
/// Module path
pub module_path: Option<&'a str>,
/// File
pub file: Option<&'a str>,
/// Line
pub line: Option<u32>,
}

impl<'a, T> Event<'a, T>
where
T: Loggable,
{
pub fn new(
level: Level,
name: &'a str,
payload: T,
module_path: Option<&'a str>,
file: Option<&'a str>,
line: Option<u32>,
) -> Self {
Self {
level,
name,
payload,
module_path,
file,
line,
}
}
}

/// Logs a structured event at the specified level
///
/// This can be used for structured logging that works with both regular and JSON formats.
/// When in JSON mode, the payload is serialized as a JSON object.
/// When in regular mode, the payload is formatted as a string.
///
/// # Example
///
/// ```
/// use baml_log::event;
/// use baml_log::Level;
/// use serde::Serialize;
///
/// #[derive(Serialize)]
/// struct UserEvent {
/// user_id: String,
/// action: String,
/// }
///
/// // Log a structured event
/// event!(
/// Level::Info,
/// "user_action",
/// UserEvent {
/// user_id: "123".to_string(),
/// action: "login".to_string(),
/// }
/// );
/// ```
#[macro_export]
macro_rules! event {
($level:expr, $payload:expr) => {
$crate::log_event_internal(
$level,
&$payload,
Some(module_path!()),
Some(file!()),
Some(line!()),
)
};
}

#[macro_export]
macro_rules! elog {
($level:expr, $payload:expr) => {
$crate::log_event_internal(
$level,
$payload,
Some(module_path!()),
Some(file!()),
Some(line!()),
)
};
}

/// Log an event at the ERROR level
#[macro_export]
macro_rules! eerror {
($payload:expr) => {
$crate::event!($crate::Level::Error, $payload)
};
}

/// Log an event at the WARN level
#[macro_export]
macro_rules! ewarn {
($payload:expr) => {
$crate::event!($crate::Level::Warn, $payload)
};
}

/// Log an event at the INFO level
#[macro_export]
macro_rules! einfo {
($payload:expr) => {
$crate::event!($crate::Level::Info, $payload)
};
}

/// Log an event at the DEBUG level
#[macro_export]
macro_rules! edebug {
($payload:expr) => {
$crate::event!($crate::Level::Debug, $payload)
};
}

/// Log an event at the TRACE level
#[macro_export]
macro_rules! etrace {
($payload:expr) => {
$crate::event!($crate::Level::Trace, $payload)
};
}
61 changes: 61 additions & 0 deletions engine/baml-lib/baml-log/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! A custom logging library for BAML that supports JSON and standard formats.
//!
//! This crate provides a simple logging interface similar to the standard `log` crate
//! but with separate configuration controlled by BAML-specific environment variables.
//!
//! # Features
//!
//! - Controlled by BAML-specific environment variables
//! - Support for both text and JSON log formats
//! - Dynamic configuration changes at runtime
//! - Log level filtering
//! - File, line, and module path information
//!
//! # Environment Variables
//!
//! - `BAML_LOG`: Sets the log level (error, warn, info, debug, trace)
//! - `BAML_LOG_JSON`: Enables JSON formatting when set to "true" or "1"
//! - `BAML_LOG_STYLE`: Controls color output ("auto", "always", "never")
//!
//! # Example
//!
//! ```
//! use baml_log::{binfo, bwarn, berror, bdebug, btrace};
//! use baml_log::{Level, set_log_level};
//!
//! // Initialize the logger (optional)
//! baml_log::init();
//!
//! // Log messages at different levels
//! binfo!("This is an info message");
//! bwarn!("This is a warning: {}", "something went wrong");
//!
//! // Dynamically change the log level
//! set_log_level(Level::Debug);
//! bdebug!("This debug message is now visible");
//! ```

// Export the macros
#[macro_use]
mod macros;

#[macro_use]
mod event;

mod logger;

// Re-export the core types and functions
pub use logger::{
get_log_level, init, log_event_internal, log_internal, reload_from_env, set_color_mode,
set_from_env, set_json_mode, set_log_level, set_max_message_length, Level, LogError, Loggable,
MaxMessageLength,
};

pub use crate::{
bdebug as debug, berror as error, binfo as info, blog as log, btrace as trace, bwarn as warn,
};

// Provide a prelude for easy imports
pub mod prelude {
pub use crate::{init, set_color_mode, set_json_mode, set_log_level, Level, LogError};
}
Loading
Loading