Skip to content

Commit 633e359

Browse files
authored
feat: adds structured and unstructured logging (#93)
Signed-off-by: ChrisJBurns <29541485+ChrisJBurns@users.noreply.github.com>
1 parent 40711f1 commit 633e359

File tree

31 files changed

+635
-267
lines changed

31 files changed

+635
-267
lines changed

cmd/thv/config.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/spf13/cobra"
77

88
"github.com/stacklok/toolhive/pkg/config"
9+
"github.com/stacklok/toolhive/pkg/logger"
910
"github.com/stacklok/toolhive/pkg/secrets"
1011
)
1112

@@ -79,7 +80,7 @@ func init() {
7980
configCmd.AddCommand(listRegisteredClientsCmd)
8081
}
8182

82-
func secretsProviderCmdFunc(cmd *cobra.Command, args []string) error {
83+
func secretsProviderCmdFunc(_ *cobra.Command, args []string) error {
8384
provider := args[0]
8485

8586
// Validate the provider type
@@ -101,11 +102,11 @@ func secretsProviderCmdFunc(cmd *cobra.Command, args []string) error {
101102
return fmt.Errorf("failed to save configuration: %w", err)
102103
}
103104

104-
cmd.Printf("Secrets provider type updated to: %s\n", provider)
105+
logger.Log.Info(fmt.Sprintf("Secrets provider type updated to: %s", provider))
105106
return nil
106107
}
107108

108-
func autoDiscoveryCmdFunc(cmd *cobra.Command, args []string) error {
109+
func autoDiscoveryCmdFunc(_ *cobra.Command, args []string) error {
109110
value := args[0]
110111

111112
// Validate the boolean value
@@ -130,11 +131,11 @@ func autoDiscoveryCmdFunc(cmd *cobra.Command, args []string) error {
130131
return fmt.Errorf("failed to save configuration: %w", err)
131132
}
132133

133-
cmd.Printf("Auto-discovery of MCP clients %s\n", map[bool]string{true: "enabled", false: "disabled"}[enabled])
134+
logger.Log.Info(fmt.Sprintf("Auto-discovery of MCP clients %s", map[bool]string{true: "enabled", false: "disabled"}[enabled]))
134135
return nil
135136
}
136137

137-
func registerClientCmdFunc(cmd *cobra.Command, args []string) error {
138+
func registerClientCmdFunc(_ *cobra.Command, args []string) error {
138139
client := args[0]
139140

140141
// Validate the client type
@@ -163,11 +164,11 @@ func registerClientCmdFunc(cmd *cobra.Command, args []string) error {
163164
return fmt.Errorf("failed to save configuration: %w", err)
164165
}
165166

166-
cmd.Printf("Successfully registered client: %s\n", client)
167+
logger.Log.Info(fmt.Sprintf("Successfully registered client: %s", client))
167168
return nil
168169
}
169170

170-
func removeClientCmdFunc(cmd *cobra.Command, args []string) error {
171+
func removeClientCmdFunc(_ *cobra.Command, args []string) error {
171172
client := args[0]
172173

173174
// Validate the client type
@@ -201,24 +202,24 @@ func removeClientCmdFunc(cmd *cobra.Command, args []string) error {
201202
return fmt.Errorf("failed to save configuration: %w", err)
202203
}
203204

204-
cmd.Printf("Successfully removed client: %s\n", client)
205+
logger.Log.Info(fmt.Sprintf("Successfully removed client: %s", client))
205206
return nil
206207
}
207208

208-
func listRegisteredClientsCmdFunc(cmd *cobra.Command, _ []string) error {
209+
func listRegisteredClientsCmdFunc(_ *cobra.Command, _ []string) error {
209210
// Get the current config
210211
cfg := config.GetConfig()
211212

212213
// Check if there are any registered clients
213214
if len(cfg.Clients.RegisteredClients) == 0 {
214-
cmd.Println("No clients are currently registered.")
215+
logger.Log.Info("No clients are currently registered.")
215216
return nil
216217
}
217218

218219
// Print the list of registered clients
219-
cmd.Println("Registered clients:")
220+
logger.Log.Info("Registered clients:")
220221
for _, client := range cfg.Clients.RegisteredClients {
221-
cmd.Printf(" - %s\n", client)
222+
logger.Log.Info(fmt.Sprintf(" - %s", client))
222223
}
223224

224225
return nil

cmd/thv/list.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/stacklok/toolhive/pkg/container"
1616
rt "github.com/stacklok/toolhive/pkg/container/runtime"
1717
"github.com/stacklok/toolhive/pkg/labels"
18+
"github.com/stacklok/toolhive/pkg/logger"
1819
)
1920

2021
var listCmd = &cobra.Command{
@@ -88,7 +89,7 @@ func listCmdFunc(_ *cobra.Command, _ []string) error {
8889
}
8990

9091
if len(toolHiveContainers) == 0 {
91-
fmt.Println("No MCP servers found")
92+
logger.Log.Info("No MCP servers found")
9293
return nil
9394
}
9495

@@ -162,7 +163,7 @@ func printJSONOutput(containers []rt.ContainerInfo) error {
162163
}
163164

164165
// Print JSON
165-
fmt.Println(string(jsonData))
166+
logger.Log.Info(string(jsonData))
166167
return nil
167168
}
168169

@@ -214,7 +215,7 @@ func printMCPServersOutput(containers []rt.ContainerInfo) error {
214215
}
215216

216217
// Print JSON
217-
fmt.Println(string(jsonData))
218+
logger.Log.Info(string(jsonData))
218219
return nil
219220
}
220221

@@ -270,6 +271,6 @@ func printTextOutput(containers []rt.ContainerInfo) {
270271

271272
// Flush the tabwriter
272273
if err := w.Flush(); err != nil {
273-
fmt.Printf("Warning: Failed to flush tabwriter: %v\n", err)
274+
logger.Log.Info(fmt.Sprintf("Warning: Failed to flush tabwriter: %v", err))
274275
}
275276
}

cmd/thv/logs.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package main
22

33
import (
44
"context"
5+
"fmt"
56
"strings"
67

78
"github.com/spf13/cobra"
89

910
"github.com/stacklok/toolhive/pkg/container"
1011
"github.com/stacklok/toolhive/pkg/labels"
12+
"github.com/stacklok/toolhive/pkg/logger"
1113
)
1214

1315
func newLogsCommand() *cobra.Command {
@@ -16,7 +18,7 @@ func newLogsCommand() *cobra.Command {
1618
Short: "Output the logs of an MCP server",
1719
Long: `Output the logs of an MCP server managed by Vibe Tool.`,
1820
Args: cobra.ExactArgs(1),
19-
Run: func(cmd *cobra.Command, args []string) {
21+
Run: func(_ *cobra.Command, args []string) {
2022
// Get container name
2123
containerName := args[0]
2224

@@ -27,14 +29,14 @@ func newLogsCommand() *cobra.Command {
2729
// Create container runtime
2830
runtime, err := container.NewFactory().Create(ctx)
2931
if err != nil {
30-
cmd.Printf("failed to create container runtime: %v", err)
32+
logger.Log.Error(fmt.Sprintf("failed to create container runtime: %v", err))
3133
return
3234
}
3335

3436
// List containers to find the one with the given name
3537
containers, err := runtime.ListContainers(ctx)
3638
if err != nil {
37-
cmd.Printf("failed to list containers: %v", err)
39+
logger.Log.Error(fmt.Sprintf("failed to list containers: %v", err))
3840
return
3941
}
4042

@@ -60,16 +62,17 @@ func newLogsCommand() *cobra.Command {
6062
}
6163

6264
if containerID == "" {
63-
cmd.Printf("container %s not found", containerName)
65+
logger.Log.Info(fmt.Sprintf("container %s not found", containerName))
6466
return
6567
}
6668

6769
logs, err := runtime.ContainerLogs(ctx, containerID)
6870
if err != nil {
69-
cmd.Printf("failed to get container logs: %v", err)
71+
logger.Log.Error(fmt.Sprintf("failed to get container logs: %v", err))
7072
return
7173
}
72-
cmd.Println(logs)
74+
logger.Log.Info(logs)
75+
7376
},
7477
}
7578
}

cmd/thv/main.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"os"
66

77
"github.com/spf13/cobra"
8+
9+
"github.com/stacklok/toolhive/pkg/logger"
810
)
911

1012
var rootCmd = &cobra.Command{
@@ -19,7 +21,7 @@ container-based isolation for running MCP servers.`,
1921
Run: func(cmd *cobra.Command, _ []string) {
2022
// If no subcommand is provided, print help
2123
if err := cmd.Help(); err != nil {
22-
fmt.Printf("Error displaying help: %v\n", err)
24+
logger.Log.Error(fmt.Sprintf("Error displaying help: %v", err))
2325
}
2426
},
2527
}
@@ -28,11 +30,14 @@ var versionCmd = &cobra.Command{
2830
Use: "version",
2931
Short: "Show the version of ToolHive",
3032
Run: func(_ *cobra.Command, _ []string) {
31-
fmt.Println("ToolHive v0.1.0")
33+
logger.Log.Info("ToolHive v0.1.0")
3234
},
3335
}
3436

3537
func init() {
38+
// Initialize the logger system
39+
logger.Initialize()
40+
3641
// Add persistent flags
3742
rootCmd.PersistentFlags().Bool("debug", false, "Enable debug mode")
3843

@@ -50,7 +55,7 @@ func init() {
5055

5156
func main() {
5257
if err := rootCmd.Execute(); err != nil {
53-
fmt.Fprintln(os.Stderr, err)
58+
logger.Log.Error("%v, %v", os.Stderr, err)
5459
os.Exit(1)
5560
}
5661
}

cmd/thv/proxy.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/spf13/cobra"
1111

1212
"github.com/stacklok/toolhive/pkg/auth"
13+
"github.com/stacklok/toolhive/pkg/logger"
1314
"github.com/stacklok/toolhive/pkg/networking"
1415
"github.com/stacklok/toolhive/pkg/transport/proxy/transparent"
1516
"github.com/stacklok/toolhive/pkg/transport/types"
@@ -43,7 +44,7 @@ func init() {
4344

4445
// Mark target-uri as required
4546
if err := proxyCmd.MarkFlagRequired("target-uri"); err != nil {
46-
fmt.Printf("Warning: Failed to mark flag as required: %v\n", err)
47+
logger.Log.Warn(fmt.Sprintf("Warning: Failed to mark flag as required: %v", err))
4748
}
4849
}
4950

@@ -56,7 +57,7 @@ func proxyCmdFunc(cmd *cobra.Command, args []string) error {
5657
if err != nil {
5758
return err
5859
}
59-
fmt.Printf("Using host port: %d\n", port)
60+
logger.Log.Info(fmt.Sprintf("Using host port: %d", port))
6061

6162
// Create context
6263
ctx, cancel := context.WithCancel(context.Background())
@@ -67,7 +68,7 @@ func proxyCmdFunc(cmd *cobra.Command, args []string) error {
6768

6869
// Create JWT validator if OIDC flags are provided
6970
if IsOIDCEnabled(cmd) {
70-
fmt.Println("OIDC validation enabled")
71+
logger.Log.Info("OIDC validation enabled")
7172

7273
// Get OIDC flag values
7374
issuer := GetStringFlagOrEmpty(cmd, "oidc-issuer")
@@ -89,36 +90,36 @@ func proxyCmdFunc(cmd *cobra.Command, args []string) error {
8990
// Add JWT validation middleware
9091
middlewares = append(middlewares, jwtValidator.Middleware)
9192
} else {
92-
fmt.Println("OIDC validation disabled")
93+
logger.Log.Info("OIDC validation disabled")
9394
}
9495

9596
// Create the transparent proxy
96-
fmt.Printf("Setting up transparent proxy to forward from host port %d to %s\n",
97-
port, proxyTargetURI)
97+
logger.Log.Info(fmt.Sprintf("Setting up transparent proxy to forward from host port %d to %s",
98+
port, proxyTargetURI))
9899

99100
// Create the transparent proxy with middlewares
100101
proxy := transparent.NewTransparentProxy(port, serverName, proxyTargetURI, middlewares...)
101102
if err := proxy.Start(ctx); err != nil {
102103
return fmt.Errorf("failed to start proxy: %v", err)
103104
}
104105

105-
fmt.Printf("Transparent proxy started for server %s on port %d -> %s\n",
106-
serverName, port, proxyTargetURI)
107-
fmt.Println("Press Ctrl+C to stop")
106+
logger.Log.Info(fmt.Sprintf("Transparent proxy started for server %s on port %d -> %s",
107+
serverName, port, proxyTargetURI))
108+
logger.Log.Info("Press Ctrl+C to stop")
108109

109110
// Set up signal handling
110111
sigCh := make(chan os.Signal, 1)
111112
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
112113

113114
// Wait for signal
114115
sig := <-sigCh
115-
fmt.Printf("Received signal %s, stopping proxy...\n", sig)
116+
logger.Log.Info(fmt.Sprintf("Received signal %s, stopping proxy...", sig))
116117

117118
// Stop the proxy
118119
if err := proxy.Stop(ctx); err != nil {
119-
fmt.Printf("Warning: Failed to stop proxy: %v\n", err)
120+
logger.Log.Warn(fmt.Sprintf("Warning: Failed to stop proxy: %v", err))
120121
}
121122

122-
fmt.Printf("Proxy for server %s stopped\n", serverName)
123+
logger.Log.Info(fmt.Sprintf("Proxy for server %s stopped", serverName))
123124
return nil
124125
}

0 commit comments

Comments
 (0)