Skip to content

Commit a881e84

Browse files
committed
Enhance logging configuration and command handling in mcpproxy
- Updated default logging settings to disable file logging and enable console output. - Modified command logger setup to use appropriate log levels based on command type. - Improved logging in search-servers command to provide detailed information on server searches and registry loading. - Adjusted autostart configurations to include logging options for better traceability.
1 parent 79a4ed1 commit a881e84

5 files changed

Lines changed: 80 additions & 11 deletions

File tree

cmd/mcpproxy/main.go

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ func main() {
6060
// Add global flags
6161
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "Configuration file path")
6262
rootCmd.PersistentFlags().StringVarP(&dataDir, "data-dir", "d", "", "Data directory path (default: ~/.mcpproxy)")
63-
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Log level (debug, info, warn, error)")
64-
rootCmd.PersistentFlags().BoolVar(&logToFile, "log-to-file", true, "Enable logging to file in standard OS location")
63+
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "", "Log level (debug, info, warn, error) - defaults: server=info, other commands=warn")
64+
rootCmd.PersistentFlags().BoolVar(&logToFile, "log-to-file", false, "Enable logging to file in standard OS location (default: console only)")
6565
rootCmd.PersistentFlags().StringVar(&logDir, "log-dir", "", "Custom log directory path (overrides standard OS location)")
6666

6767
// Add server command
@@ -121,9 +121,22 @@ Examples:
121121
122122
# Search for servers tagged as "finance" in the Pulse registry
123123
mcpproxy search-servers --registry pulse --tag finance`,
124-
RunE: func(_ *cobra.Command, _ []string) error {
124+
RunE: func(cmd *cobra.Command, _ []string) error {
125+
// Setup logger for search command (non-server command = WARN level by default)
126+
cmdLogLevel, _ := cmd.Flags().GetString("log-level")
127+
cmdLogToFile, _ := cmd.Flags().GetBool("log-to-file")
128+
cmdLogDir, _ := cmd.Flags().GetString("log-dir")
129+
130+
logger, err := logs.SetupCommandLogger(false, cmdLogLevel, cmdLogToFile, cmdLogDir)
131+
if err != nil {
132+
return fmt.Errorf("failed to setup logger: %w", err)
133+
}
134+
defer func() {
135+
_ = logger.Sync()
136+
}()
137+
125138
if listRegistries {
126-
return listAllRegistries()
139+
return listAllRegistries(logger)
127140
}
128141

129142
if registryFlag == "" {
@@ -145,16 +158,24 @@ Examples:
145158
// Create experiments guesser if repository checking is enabled
146159
var guesser *experiments.Guesser
147160
if cfg.CheckServerRepo {
148-
// For CLI, we don't have cache manager, so pass nil
149-
logger := zap.NewNop() // Use no-op logger for CLI
161+
// Use the proper logger instead of no-op
150162
guesser = experiments.NewGuesser(nil, logger)
151163
}
152164

165+
logger.Info("Searching servers",
166+
zap.String("registry", registryFlag),
167+
zap.String("search", searchFlag),
168+
zap.String("tag", tagFlag),
169+
zap.Int("limit", limitFlag))
170+
153171
servers, err := registries.SearchServers(ctx, registryFlag, tagFlag, searchFlag, limitFlag, guesser)
154172
if err != nil {
173+
logger.Error("Search failed", zap.Error(err))
155174
return fmt.Errorf("search failed: %w", err)
156175
}
157176

177+
logger.Info("Search completed", zap.Int("results_count", len(servers)))
178+
158179
// Print results as JSON
159180
output, err := json.MarshalIndent(servers, "", " ")
160181
if err != nil {
@@ -175,19 +196,23 @@ Examples:
175196
return cmd
176197
}
177198

178-
func listAllRegistries() error {
199+
func listAllRegistries(logger *zap.Logger) error {
179200
// Load config and initialize registries
180201
cfg, err := config.LoadFromFile("")
181202
if err != nil {
182203
// Use default config if loading fails
183204
cfg = config.DefaultConfig()
184205
}
185206

207+
logger.Info("Loading registries configuration")
208+
186209
// Initialize registries from config
187210
registries.SetRegistriesFromConfig(cfg)
188211

189212
registryList := registries.ListRegistries()
190213

214+
logger.Info("Found registries", zap.Int("count", len(registryList)))
215+
191216
// Format as a simple table for CLI display
192217
fmt.Printf("%-20s %-30s %s\n", "ID", "NAME", "DESCRIPTION")
193218
fmt.Printf("%-20s %-30s %s\n", "==", "====", "===========")
@@ -223,8 +248,14 @@ func runServer(cmd *cobra.Command, _ []string) error {
223248

224249
// Override logging settings from command line
225250
if cfg.Logging == nil {
251+
// Use command-specific default level (INFO for server command)
252+
defaultLevel := cmdLogLevel
253+
if defaultLevel == "" {
254+
defaultLevel = "info" // Server command defaults to INFO
255+
}
256+
226257
cfg.Logging = &config.LogConfig{
227-
Level: cmdLogLevel,
258+
Level: defaultLevel,
228259
EnableFile: cmdLogToFile,
229260
EnableConsole: true,
230261
Filename: "main.log",
@@ -236,7 +267,11 @@ func runServer(cmd *cobra.Command, _ []string) error {
236267
}
237268
} else {
238269
// Override specific fields from command line
239-
cfg.Logging.Level = cmdLogLevel
270+
if cmdLogLevel != "" {
271+
cfg.Logging.Level = cmdLogLevel
272+
} else if cfg.Logging.Level == "" {
273+
cfg.Logging.Level = "info" // Server command defaults to INFO
274+
}
240275
cfg.Logging.EnableFile = cmdLogToFile
241276
if cfg.Logging.Filename == "" || cfg.Logging.Filename == "mcpproxy.log" {
242277
cfg.Logging.Filename = "main.log"

internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func DefaultConfig() *Config {
192192
// Default logging configuration
193193
Logging: &LogConfig{
194194
Level: "info",
195-
EnableFile: true,
195+
EnableFile: false, // Changed: Console by default
196196
EnableConsole: true,
197197
Filename: "main.log",
198198
MaxSize: 10, // 10MB

internal/logs/logger.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const (
2525
func DefaultLogConfig() *config.LogConfig {
2626
return &config.LogConfig{
2727
Level: LogLevelInfo,
28-
EnableFile: true,
28+
EnableFile: false, // Changed: Console by default, not file
2929
EnableConsole: true,
3030
Filename: "main.log",
3131
MaxSize: 10, // 10MB
@@ -92,6 +92,38 @@ func SetupLogger(config *config.LogConfig) (*zap.Logger, error) {
9292
return logger, nil
9393
}
9494

95+
// SetupCommandLogger creates a logger for console commands with appropriate default levels
96+
// serverCommand: if true, uses INFO level by default; if false, uses WARN level by default
97+
func SetupCommandLogger(serverCommand bool, logLevel string, logToFile bool, logDir string) (*zap.Logger, error) {
98+
// Determine default log level based on command type
99+
defaultLevel := LogLevelWarn // Other commands default to WARN
100+
if serverCommand {
101+
defaultLevel = LogLevelInfo // Server command defaults to INFO
102+
}
103+
104+
// Use provided level or fall back to command-specific default
105+
level := defaultLevel
106+
if logLevel != "" {
107+
level = logLevel
108+
}
109+
110+
// Create config for command logger
111+
config := &config.LogConfig{
112+
Level: level,
113+
EnableFile: logToFile,
114+
EnableConsole: true, // Console always enabled for commands
115+
Filename: "main.log",
116+
LogDir: logDir,
117+
MaxSize: 10,
118+
MaxBackups: 5,
119+
MaxAge: 30,
120+
Compress: true,
121+
JSONFormat: false,
122+
}
123+
124+
return SetupLogger(config)
125+
}
126+
95127
// createFileCore creates a file-based logging core
96128
func createFileCore(config *config.LogConfig, level zapcore.Level) (zapcore.Core, error) {
97129
// Get log file path with custom directory support

internal/tray/autostart.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
<array>
2323
<string>%s</string>
2424
<string>--tray=true</string>
25+
<string>--log-to-file=true</string>
2526
</array>
2627
<key>RunAtLoad</key>
2728
<true/>

scripts/com.smartmcpproxy.mcpproxy.plist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<string>/Applications/mcpproxy</string>
1111
<string>serve</string>
1212
<string>--tray=true</string>
13+
<string>--log-to-file=true</string>
1314
</array>
1415

1516
<key>RunAtLoad</key>

0 commit comments

Comments
 (0)