summaryrefslogtreecommitdiffhomepage
path: root/internal/app
diff options
context:
space:
mode:
authoradamdottv <[email protected]>2025-05-09 13:37:13 -0500
committeradamdottv <[email protected]>2025-05-09 13:37:13 -0500
commitf1007771997bd0401516eda87a7e0ac92f269680 (patch)
treed26198d031516eaebcc885870b470925492d8775 /internal/app
parentf41b7bbd0a0cc731fd7c471b7ee8b26f14a21755 (diff)
downloadopencode-f1007771997bd0401516eda87a7e0ac92f269680.tar.gz
opencode-f1007771997bd0401516eda87a7e0ac92f269680.zip
wip: logging improvements
Diffstat (limited to 'internal/app')
-rw-r--r--internal/app/app.go16
-rw-r--r--internal/app/lsp.go34
2 files changed, 30 insertions, 20 deletions
diff --git a/internal/app/app.go b/internal/app/app.go
index b4812fb46..42de24548 100644
--- a/internal/app/app.go
+++ b/internal/app/app.go
@@ -7,6 +7,8 @@ import (
"sync"
"time"
+ "log/slog"
+
"github.com/opencode-ai/opencode/internal/config"
"github.com/opencode-ai/opencode/internal/db"
"github.com/opencode-ai/opencode/internal/history"
@@ -21,6 +23,7 @@ import (
)
type App struct {
+ Logs logging.Service
Sessions session.Service
Messages message.Service
History history.Service
@@ -40,12 +43,16 @@ type App struct {
func New(ctx context.Context, conn *sql.DB) (*App, error) {
q := db.New(conn)
+ loggingService := logging.NewService(q)
sessionService := session.NewService(q)
messageService := message.NewService(q)
historyService := history.NewService(q, conn)
permissionService := permission.NewPermissionService()
statusService := status.NewService()
+ // Initialize logging service
+ logging.InitManager(loggingService)
+
// Initialize session manager
session.InitManager(sessionService)
@@ -53,6 +60,7 @@ func New(ctx context.Context, conn *sql.DB) (*App, error) {
status.InitManager(statusService)
app := &App{
+ Logs: loggingService,
Sessions: sessionService,
Messages: messageService,
History: historyService,
@@ -81,7 +89,7 @@ func New(ctx context.Context, conn *sql.DB) (*App, error) {
),
)
if err != nil {
- logging.Error("Failed to create coder agent", err)
+ slog.Error("Failed to create coder agent", err)
return nil, err
}
@@ -98,9 +106,9 @@ func (app *App) initTheme() {
// Try to set the theme from config
err := theme.SetTheme(cfg.TUI.Theme)
if err != nil {
- logging.Warn("Failed to set theme from config, using default theme", "theme", cfg.TUI.Theme, "error", err)
+ slog.Warn("Failed to set theme from config, using default theme", "theme", cfg.TUI.Theme, "error", err)
} else {
- logging.Debug("Set theme from config", "theme", cfg.TUI.Theme)
+ slog.Debug("Set theme from config", "theme", cfg.TUI.Theme)
}
}
@@ -123,7 +131,7 @@ func (app *App) Shutdown() {
for name, client := range clients {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if err := client.Shutdown(shutdownCtx); err != nil {
- logging.Error("Failed to shutdown LSP client", "name", name, "error", err)
+ slog.Error("Failed to shutdown LSP client", "name", name, "error", err)
}
cancel()
}
diff --git a/internal/app/lsp.go b/internal/app/lsp.go
index 934bd1a89..b3763fba6 100644
--- a/internal/app/lsp.go
+++ b/internal/app/lsp.go
@@ -4,6 +4,8 @@ import (
"context"
"time"
+ "log/slog"
+
"github.com/opencode-ai/opencode/internal/config"
"github.com/opencode-ai/opencode/internal/logging"
"github.com/opencode-ai/opencode/internal/lsp"
@@ -18,29 +20,29 @@ func (app *App) initLSPClients(ctx context.Context) {
// Start each client initialization in its own goroutine
go app.createAndStartLSPClient(ctx, name, clientConfig.Command, clientConfig.Args...)
}
- logging.Info("LSP clients initialization started in background")
+ slog.Info("LSP clients initialization started in background")
}
// createAndStartLSPClient creates a new LSP client, initializes it, and starts its workspace watcher
func (app *App) createAndStartLSPClient(ctx context.Context, name string, command string, args ...string) {
// Create a specific context for initialization with a timeout
- logging.Info("Creating LSP client", "name", name, "command", command, "args", args)
-
+ slog.Info("Creating LSP client", "name", name, "command", command, "args", args)
+
// Create the LSP client
lspClient, err := lsp.NewClient(ctx, command, args...)
if err != nil {
- logging.Error("Failed to create LSP client for", name, err)
+ slog.Error("Failed to create LSP client for", name, err)
return
}
// Create a longer timeout for initialization (some servers take time to start)
initCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
-
+
// Initialize with the initialization context
_, err = lspClient.InitializeLSPClient(initCtx, config.WorkingDirectory())
if err != nil {
- logging.Error("Initialize failed", "name", name, "error", err)
+ slog.Error("Initialize failed", "name", name, "error", err)
// Clean up the client to prevent resource leaks
lspClient.Close()
return
@@ -48,22 +50,22 @@ func (app *App) createAndStartLSPClient(ctx context.Context, name string, comman
// Wait for the server to be ready
if err := lspClient.WaitForServerReady(initCtx); err != nil {
- logging.Error("Server failed to become ready", "name", name, "error", err)
+ slog.Error("Server failed to become ready", "name", name, "error", err)
// We'll continue anyway, as some functionality might still work
lspClient.SetServerState(lsp.StateError)
} else {
- logging.Info("LSP server is ready", "name", name)
+ slog.Info("LSP server is ready", "name", name)
lspClient.SetServerState(lsp.StateReady)
}
- logging.Info("LSP client initialized", "name", name)
-
+ slog.Info("LSP client initialized", "name", name)
+
// Create a child context that can be canceled when the app is shutting down
watchCtx, cancelFunc := context.WithCancel(ctx)
-
+
// Create a context with the server name for better identification
watchCtx = context.WithValue(watchCtx, "serverName", name)
-
+
// Create the workspace watcher
workspaceWatcher := watcher.NewWorkspaceWatcher(lspClient)
@@ -92,7 +94,7 @@ func (app *App) runWorkspaceWatcher(ctx context.Context, name string, workspaceW
})
workspaceWatcher.WatchWorkspace(ctx, config.WorkingDirectory())
- logging.Info("Workspace watcher stopped", "client", name)
+ slog.Info("Workspace watcher stopped", "client", name)
}
// restartLSPClient attempts to restart a crashed or failed LSP client
@@ -101,7 +103,7 @@ func (app *App) restartLSPClient(ctx context.Context, name string) {
cfg := config.Get()
clientConfig, exists := cfg.LSP[name]
if !exists {
- logging.Error("Cannot restart client, configuration not found", "client", name)
+ slog.Error("Cannot restart client, configuration not found", "client", name)
return
}
@@ -118,7 +120,7 @@ func (app *App) restartLSPClient(ctx context.Context, name string) {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
_ = oldClient.Shutdown(shutdownCtx)
cancel()
-
+
// Ensure we close the client to free resources
_ = oldClient.Close()
}
@@ -128,5 +130,5 @@ func (app *App) restartLSPClient(ctx context.Context, name string) {
// Create a new client using the shared function
app.createAndStartLSPClient(ctx, name, clientConfig.Command, clientConfig.Args...)
- logging.Info("Successfully restarted LSP client", "client", name)
+ slog.Info("Successfully restarted LSP client", "client", name)
}