add basic logging using the tracing crate

This commit is contained in:
Josh Thomas 2025-05-22 01:26:27 -05:00
parent 68ea842821
commit e784ffa350
7 changed files with 91 additions and 37 deletions

View file

@ -20,6 +20,7 @@ serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
tower-lsp-server = { workspace = true }
tracing = { workspace = true }
[build-dependencies]
djls-dev = { workspace = true }

View file

@ -98,8 +98,11 @@ impl DjangoLanguageServer {
impl LanguageServer for DjangoLanguageServer {
async fn initialize(&self, params: InitializeParams) -> LspResult<InitializeResult> {
client::log_message(MessageType::INFO, "Initializing server...");
tracing::info!("LSP server initializing");
tracing::debug!("Initialize params: {:?}", params);
let session = Session::new(&params);
tracing::debug!("Session created successfully");
{
let mut session_lock = self.session.write().await;
@ -149,6 +152,7 @@ impl LanguageServer for DjangoLanguageServer {
MessageType::INFO,
"Server received initialized notification.",
);
tracing::info!("LSP server initialized and ready");
self.with_session_task(|session_arc| async move {
let project_path_and_venv = {
@ -237,6 +241,8 @@ impl LanguageServer for DjangoLanguageServer {
MessageType::INFO,
format!("Opened document: {:?}", params.text_document.uri),
);
tracing::info!("Document opened: {:?}", params.text_document.uri);
tracing::debug!("Document language: {}", params.text_document.language_id);
self.with_session_mut(|session| {
let db = session.db();

View file

@ -1,6 +1,5 @@
use djls_conf::Settings;
use djls_project::DjangoProject;
use salsa::StorageHandle;
use tower_lsp_server::lsp_types::ClientCapabilities;
use tower_lsp_server::lsp_types::InitializeParams;
@ -16,35 +15,6 @@ pub struct Session {
#[allow(dead_code)]
client_capabilities: ClientCapabilities,
/// A thread-safe Salsa database handle that can be shared between threads.
///
/// This implements the insight from [this Salsa Zulip discussion](https://salsa.zulipchat.com/#narrow/channel/145099-Using-Salsa/topic/.E2.9C.94.20Advice.20on.20using.20salsa.20from.20Sync.20.2B.20Send.20context/with/495497515)
/// where we're using the `StorageHandle` to create a thread-safe handle that can be
/// shared between threads. When we need to use it, we clone the handle to get a new reference.
///
/// This handle allows us to create database instances as needed.
/// Even though we're using a single-threaded runtime, we still need
/// this to be thread-safe because of LSP trait requirements.
///
/// Usage:
/// ```rust,ignore
/// // Use the StorageHandle in Session
/// let db_handle = StorageHandle::new(None);
///
/// // Clone it to pass to different threads
/// let db_handle_clone = db_handle.clone();
///
/// // Use it in an async context
/// async_fn(move || {
/// // Get a database from the handle
/// let storage = db_handle_clone.into_storage();
/// let db = ServerDatabase::new(storage);
///
/// // Use the database
/// db.some_query(args)
/// });
/// ```
db_handle: StorageHandle<ServerDatabase>,
}
impl Session {
@ -67,7 +37,6 @@ impl Session {
project,
documents: Store::default(),
settings,
db_handle: StorageHandle::new(None),
}
}
@ -97,10 +66,20 @@ impl Session {
/// Get a database instance directly from the session
///
/// This creates a usable database from the handle, which can be used
/// to query and update data in the database.
/// This creates a usable database with Salsa event logging
pub fn db(&self) -> ServerDatabase {
let storage = self.db_handle.clone().into_storage();
let storage = salsa::Storage::new(if tracing::enabled!(tracing::Level::DEBUG) {
Some(Box::new({
move |event: salsa::Event| {
if matches!(event.kind, salsa::EventKind::WillCheckCancellation) {
return;
}
tracing::debug!("Salsa event: {event:?}");
}
}))
} else {
None
});
ServerDatabase::new(storage)
}
}