-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
697b1e2
commit 926e412
Showing
11 changed files
with
224 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use anyhow::{Context, Result}; | ||
use std::fs; | ||
use std::path::PathBuf; | ||
use tracing_appender::rolling::Rotation; | ||
use tracing_subscriber::{ | ||
fmt, | ||
Layer, | ||
layer::SubscriberExt, | ||
util::SubscriberInitExt, | ||
EnvFilter, | ||
Registry, | ||
filter::LevelFilter, | ||
}; | ||
|
||
use goose::tracing::langfuse_layer; | ||
|
||
/// Returns the directory where log files should be stored. | ||
/// Creates the directory structure if it doesn't exist. | ||
fn get_log_directory() -> Result<PathBuf> { | ||
let home = std::env::var("HOME").context("HOME environment variable not set")?; | ||
let base_log_dir = PathBuf::from(home) | ||
.join(".config") | ||
.join("goose") | ||
.join("logs") | ||
.join("cli"); // Add cli-specific subdirectory | ||
|
||
// Create date-based subdirectory | ||
let now = chrono::Local::now(); | ||
let date_dir = base_log_dir.join(now.format("%Y-%m-%d").to_string()); | ||
|
||
// Ensure log directory exists | ||
fs::create_dir_all(&date_dir).context("Failed to create log directory")?; | ||
|
||
Ok(date_dir) | ||
} | ||
|
||
/// Sets up the logging infrastructure for the application. | ||
/// This includes: | ||
/// - File-based logging with JSON formatting (DEBUG level) | ||
/// - Console output for development (INFO level) | ||
/// - Optional Langfuse integration (DEBUG level) | ||
pub fn setup_logging() -> Result<()> { | ||
// Set up file appender for goose module logs | ||
let log_dir = get_log_directory()?; | ||
let timestamp = chrono::Local::now().format("%Y%m%d_%H%M%S").to_string(); | ||
|
||
// Create non-rolling file appender for detailed logs | ||
let file_appender = tracing_appender::rolling::RollingFileAppender::new( | ||
Rotation::NEVER, | ||
log_dir, | ||
&format!("goose_{}.log", timestamp), | ||
); | ||
|
||
// Create JSON file logging layer with all logs (DEBUG and above) | ||
let file_layer = fmt::layer() | ||
.with_target(true) | ||
.with_level(true) | ||
.with_writer(file_appender) | ||
.with_ansi(false) | ||
.with_file(true) | ||
.pretty(); | ||
|
||
// Create console logging layer for development - INFO and above only | ||
let console_layer = fmt::layer() | ||
.with_target(true) | ||
.with_level(true) | ||
.with_ansi(true) | ||
.with_file(true) | ||
.with_line_number(true) | ||
.pretty(); | ||
|
||
// Base filter | ||
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| { | ||
// Set default levels for different modules | ||
EnvFilter::new("") | ||
// Set goose module to INFO only | ||
.add_directive("goose=debug".parse().unwrap()) | ||
// Set goose-cli to INFO | ||
.add_directive("goose_cli=info".parse().unwrap()) | ||
// Set everything else to WARN | ||
.add_directive(LevelFilter::WARN.into()) | ||
}); | ||
|
||
// Build the subscriber with required layers | ||
let subscriber = Registry::default() | ||
.with(file_layer.with_filter(env_filter)) // Gets all logs | ||
.with(console_layer.with_filter(LevelFilter::INFO)); // Controls log levels | ||
|
||
// Initialize with Langfuse if available | ||
if let Some(langfuse) = langfuse_layer::create_langfuse_observer() { | ||
subscriber | ||
.with(langfuse.with_filter(LevelFilter::DEBUG)) | ||
.try_init() | ||
.context("Failed to set global subscriber")?; | ||
} else { | ||
subscriber | ||
.try_init() | ||
.context("Failed to set global subscriber")?; | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use anyhow::{Context, Result}; | ||
use std::fs; | ||
use std::path::PathBuf; | ||
use tracing_appender::rolling::Rotation; | ||
use tracing_subscriber::{ | ||
fmt, | ||
Layer, | ||
layer::SubscriberExt, | ||
util::SubscriberInitExt, | ||
EnvFilter, | ||
Registry, | ||
filter::LevelFilter, | ||
}; | ||
|
||
use goose::tracing::langfuse_layer; | ||
|
||
/// Returns the directory where log files should be stored. | ||
/// Creates the directory structure if it doesn't exist. | ||
fn get_log_directory() -> Result<PathBuf> { | ||
let home = std::env::var("HOME").context("HOME environment variable not set")?; | ||
let base_log_dir = PathBuf::from(home) | ||
.join(".config") | ||
.join("goose") | ||
.join("logs") | ||
.join("server"); // Add server-specific subdirectory | ||
|
||
// Create date-based subdirectory | ||
let now = chrono::Local::now(); | ||
let date_dir = base_log_dir.join(now.format("%Y-%m-%d").to_string()); | ||
|
||
// Ensure log directory exists | ||
fs::create_dir_all(&date_dir).context("Failed to create log directory")?; | ||
|
||
Ok(date_dir) | ||
} | ||
|
||
/// Sets up the logging infrastructure for the application. | ||
/// This includes: | ||
/// - File-based logging with JSON formatting (DEBUG level) | ||
/// - Console output for development (INFO level) | ||
/// - Optional Langfuse integration (DEBUG level) | ||
pub fn setup_logging() -> Result<()> { | ||
// Set up file appender for goose module logs | ||
let log_dir = get_log_directory()?; | ||
let timestamp = chrono::Local::now().format("%Y%m%d_%H%M%S").to_string(); | ||
|
||
// Create non-rolling file appender for detailed logs | ||
let file_appender = tracing_appender::rolling::RollingFileAppender::new( | ||
Rotation::NEVER, | ||
log_dir, | ||
&format!("goosed_{}.log", timestamp), | ||
); | ||
|
||
// Create JSON file logging layer | ||
let file_layer = fmt::layer() | ||
.with_target(true) | ||
.with_level(true) | ||
.with_writer(file_appender) | ||
.with_ansi(false) | ||
.with_file(true); | ||
|
||
// Create console logging layer for development - INFO and above only | ||
let console_layer = fmt::layer() | ||
.with_target(true) | ||
.with_level(true) | ||
.with_ansi(true) | ||
.with_file(true) | ||
.with_line_number(true) | ||
.pretty(); | ||
|
||
// Base filter for all logging | ||
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| { | ||
// Set default levels for different modules | ||
EnvFilter::new("") | ||
// Set goose module to INFO only | ||
.add_directive("goose=info".parse().unwrap()) | ||
// Set goose-server to INFO | ||
.add_directive("goose_server=info".parse().unwrap()) | ||
// Set tower-http to INFO for request logging | ||
.add_directive("tower_http=info".parse().unwrap()) | ||
// Set everything else to WARN | ||
.add_directive(LevelFilter::WARN.into()) | ||
}); | ||
|
||
// Build the subscriber with required layers | ||
let subscriber = Registry::default() | ||
.with(file_layer) | ||
.with(console_layer.with_filter(env_filter)); | ||
|
||
// Initialize with Langfuse if available | ||
if let Some(langfuse) = langfuse_layer::create_langfuse_observer() { | ||
subscriber | ||
.with(langfuse.with_filter(LevelFilter::DEBUG)) | ||
.try_init() | ||
.context("Failed to set global subscriber")?; | ||
} else { | ||
subscriber | ||
.try_init() | ||
.context("Failed to set global subscriber")?; | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,3 @@ pub mod providers; | |
pub mod systems; | ||
pub mod token_counter; | ||
pub mod tracing; | ||
pub mod logging; |
Oops, something went wrong.