Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Commit d0e5d78

Browse files
authored
log into files in addition to stderr (#802)
* log into files in addition to stderr this changeset introduces persistent logging through the `tracing-appender` crate. logs are written into `<index_dir>/logs/bloop.log.YYYY-MM-DD-HH` and rotated hourly. logs written to files are limited to the `bleep` crate. * disable log write on cloud instances * improve log filters * rotate logs daily instead of hourly, tweak log filters
1 parent aa360a5 commit d0e5d78

File tree

10 files changed

+69
-38
lines changed

10 files changed

+69
-38
lines changed

Cargo.lock

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

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ COPY model /model
4040
COPY --from=builder /bleep /
4141
COPY --from=builder /dylib /dylib
4242
COPY --from=frontend /build/client/dist /frontend
43-
ENTRYPOINT ["/bleep", "--host=0.0.0.0", "--source-dir=/repos", "--index-dir=/data", "--model-dir=/model", "--dylib-dir=/dylib"]
43+
ENTRYPOINT ["/bleep", "--host=0.0.0.0", "--source-dir=/repos", "--index-dir=/data", "--model-dir=/model", "--dylib-dir=/dylib", "--disable-log-write"]

apps/desktop/src-tauri/src/backend.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ where
6969
)
7070
};
7171

72+
Application::install_logging(&configuration);
73+
7274
if let Some(dsn) = &configuration.sentry_dsn {
7375
initialize_sentry(dsn);
7476
}

apps/desktop/src-tauri/src/main.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
windows_subsystem = "windows"
44
)]
55

6-
use bleep::Application;
7-
86
mod backend;
97
mod qdrant;
108

@@ -36,8 +34,6 @@ fn relative_command_path(command: impl AsRef<str>) -> Option<PathBuf> {
3634

3735
#[tokio::main]
3836
async fn main() {
39-
Application::install_logging();
40-
4137
tauri::Builder::default()
4238
.plugin(qdrant::QdrantSupervisor::default())
4339
.setup(backend::bleep)

helm/bloop/templates/bloop-deployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ spec:
7272
- --sentry-dsn={{ .Values.bloop.sentryDsn }}
7373
- --sentry-dsn-fe={{ .Values.bloop.sentryDsnFE }}
7474
- --analytics-key-fe={{ .Values.bloop.analyticsKeyFE }}
75+
- --disable-log-write
7576
env:
7677
- name: BLOOP_LOG
7778
value: {{ .Values.bloop.logLevel }}

server/bleep/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ rayon = "1.7.0"
3636
clap = { version = "4.3.11", features = ["derive"] }
3737
tracing = "0.1.37"
3838
tracing-subscriber = { version = "0.3.17", features = ["env-filter", "registry"] }
39+
tracing-appender = "0.2.2"
3940
color-eyre = "0.6.2"
4041
sqlx = { version = "0.6.3", features = ["sqlite", "migrate", "offline", "runtime-tokio-rustls", "chrono"] }
4142

server/bleep/src/bin/bleep.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@ use bleep::{Application, Configuration, Environment};
33

44
#[tokio::main]
55
async fn main() -> Result<()> {
6-
Application::install_logging();
7-
let app = Application::initialize(
8-
Environment::server(),
9-
Configuration::cli_overriding_config_file()?,
10-
None,
11-
None,
12-
)
13-
.await?;
6+
let config = Configuration::cli_overriding_config_file()?;
7+
8+
Application::install_logging(&config);
9+
let app = Application::initialize(Environment::server(), config, None, None).await?;
1410

1511
app.initialize_sentry();
1612
app.run().await

server/bleep/src/config.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ pub struct Configuration {
4141
/// Disable system-native notification backends to detect new git commits immediately.
4242
pub disable_fsevents: bool,
4343

44+
#[clap(long, default_value_t = false)]
45+
#[serde(default)]
46+
/// Avoid writing logs to files.
47+
///
48+
/// If this flag is not set to `true`, logs are written to <index_dir>/logs/bloop.log.YYYY-MM-DD-HH
49+
pub disable_log_write: bool,
50+
4451
#[clap(short, long, default_value_t = default_buffer_size())]
4552
#[serde(default = "default_buffer_size")]
4653
/// Size of memory to use for file indexes
@@ -226,6 +233,8 @@ impl Configuration {
226233

227234
disable_fsevents: b.disable_fsevents | a.disable_fsevents,
228235

236+
disable_log_write: b.disable_log_write | a.disable_log_write,
237+
229238
buffer_size: right_if_default!(b.buffer_size, a.buffer_size, default_buffer_size()),
230239

231240
repo_buffer_size: right_if_default!(

server/bleep/src/indexes/file.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,10 +603,7 @@ impl RepoFile {
603603
// we have a graph, use that
604604
Ok(graph) => SymbolLocations::TreeSitter(graph),
605605
// no graph, it's empty
606-
Err(err) => {
607-
warn!(?err, %lang_str, "failed to build scope graph");
608-
SymbolLocations::Empty
609-
}
606+
Err(_) => SymbolLocations::Empty,
610607
}
611608
};
612609

server/bleep/src/lib.rs

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ use once_cell::sync::OnceCell;
3434
use sentry_tracing::{EventFilter, SentryLayer};
3535
use std::{path::Path, sync::Arc};
3636
use tracing::{debug, error, info, warn, Level};
37-
use tracing_subscriber::EnvFilter;
37+
use tracing_subscriber::{
38+
filter::{LevelFilter, Targets},
39+
fmt,
40+
prelude::*,
41+
EnvFilter,
42+
};
3843

3944
mod background;
4045
mod cache;
@@ -67,6 +72,7 @@ pub use env::Environment;
6772
const LOG_ENV_VAR: &str = "BLOOP_LOG";
6873
static LOGGER_INSTALLED: OnceCell<bool> = OnceCell::new();
6974
static SENTRY_GUARD: OnceCell<sentry::ClientInitGuard> = OnceCell::new();
75+
static LOGGER_GUARD: OnceCell<tracing_appender::non_blocking::WorkerGuard> = OnceCell::new();
7076

7177
/// The global state
7278
#[derive(Clone)]
@@ -218,12 +224,12 @@ impl Application {
218224
_ = SENTRY_GUARD.set(guard);
219225
}
220226

221-
pub fn install_logging() {
227+
pub fn install_logging(config: &Configuration) {
222228
if let Some(true) = LOGGER_INSTALLED.get() {
223229
return;
224230
}
225231

226-
if !tracing_subscribe() {
232+
if !tracing_subscribe(config) {
227233
warn!("Failed to install tracing_subscriber. There's probably one already...");
228234
};
229235

@@ -239,7 +245,7 @@ impl Application {
239245
}
240246

241247
pub async fn run(self) -> Result<()> {
242-
Self::install_logging();
248+
Self::install_logging(&self.config);
243249

244250
let mut joins = tokio::task::JoinSet::new();
245251

@@ -338,25 +344,36 @@ impl FromRef<Application> for axum_extra::extract::cookie::Key {
338344
}
339345
}
340346

341-
#[cfg(all(tokio_unstable, feature = "debug"))]
342-
fn tracing_subscribe() -> bool {
343-
use tracing_subscriber::{fmt, prelude::*};
344-
let env_filter = fmt::layer().with_filter(EnvFilter::from_env(LOG_ENV_VAR));
345-
tracing_subscriber::registry()
346-
.with(env_filter)
347-
.with(sentry_layer())
348-
.with(console_subscriber::spawn())
349-
.try_init()
350-
.is_ok()
351-
}
347+
fn tracing_subscribe(config: &Configuration) -> bool {
348+
let env_filter_layer = fmt::layer().with_filter(EnvFilter::from_env(LOG_ENV_VAR));
349+
let sentry_layer = sentry_layer();
350+
let log_writer_layer = (!config.disable_log_write).then(|| {
351+
let log_dir = config.index_dir.join("logs");
352+
let file_appender = tracing_appender::rolling::daily(log_dir, "bloop.log");
353+
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
354+
_ = LOGGER_GUARD.set(guard);
355+
fmt::layer()
356+
.with_writer(non_blocking)
357+
.with_ansi(false)
358+
.with_filter(
359+
Targets::new()
360+
.with_target("bleep", LevelFilter::DEBUG)
361+
.with_target("bleep::indexes::file", LevelFilter::WARN)
362+
.with_target("bleep::semantic", LevelFilter::WARN),
363+
)
364+
});
365+
366+
#[cfg(all(tokio_unstable, feature = "debug"))]
367+
let console_subscriber_layer = Some(console_subscriber::spawn());
368+
#[cfg(not(all(tokio_unstable, feature = "debug")))]
369+
let console_subscriber_layer: Option<Box<dyn tracing_subscriber::Layer<_> + Send + Sync>> =
370+
None;
352371

353-
#[cfg(not(all(tokio_unstable, feature = "debug")))]
354-
fn tracing_subscribe() -> bool {
355-
use tracing_subscriber::{fmt, prelude::*};
356-
let env_filter = fmt::layer().with_filter(EnvFilter::from_env(LOG_ENV_VAR));
357372
tracing_subscriber::registry()
358-
.with(env_filter)
359-
.with(sentry_layer())
373+
.with(log_writer_layer)
374+
.with(env_filter_layer)
375+
.with(sentry_layer)
376+
.with(console_subscriber_layer)
360377
.try_init()
361378
.is_ok()
362379
}

0 commit comments

Comments
 (0)