Skip to content

Commit dccb7f1

Browse files
explodedclaude
andcommitted
Replace log.Printf with slog methods to fix empty logship source field
The logship handler extracts source file:line from slog.Record.PC, which is only set by slog.Logger methods (slog.Info, slog.Warn, etc.). All logging was using the old log package, so PC was never populated. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b14b0a4 commit dccb7f1

1 file changed

Lines changed: 21 additions & 21 deletions

File tree

moon.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"encoding/json"
2323
"fmt"
2424
"html/template"
25-
"log"
2625
"log/slog"
2726
"net/http"
2827
"os"
@@ -43,15 +42,15 @@ func init() {
4342
var err error
4443
templates, err = template.ParseGlob("*.html")
4544
if err != nil {
46-
log.Printf("Warning: Error parsing templates: %v", err)
45+
slog.Warn("Error parsing templates", "error", err)
4746
}
4847
}
4948

5049
// Get Google Maps API key from environment variable
5150
func getGoogleMapsKey() string {
5251
key := os.Getenv("GOOGLE_MAPS_API_KEY")
5352
if key == "" {
54-
log.Println("WARNING: GOOGLE_MAPS_API_KEY not set in environment")
53+
slog.Warn("GOOGLE_MAPS_API_KEY not set in environment")
5554
}
5655
return key
5756
}
@@ -61,7 +60,7 @@ func requestLogger(next http.Handler) http.Handler {
6160
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6261
start := time.Now()
6362
next.ServeHTTP(w, r)
64-
log.Printf("%s %s %s", r.Method, r.RequestURI, time.Since(start))
63+
slog.Info("request", "method", r.Method, "uri", r.RequestURI, "duration", time.Since(start))
6564
})
6665
}
6766

@@ -108,7 +107,7 @@ func makeHTTPServer(isProd bool) *http.Server {
108107
mux.HandleFunc("/calendar", calendar)
109108
mux.HandleFunc("/favicon.ico", handleFavicon)
110109
path, _ := os.Getwd()
111-
log.Printf("Working directory: %s", path)
110+
slog.Info("Working directory", "path", path)
112111
fileServer := http.FileServer(http.Dir(path + "/static"))
113112
mux.Handle("/static/", http.StripPrefix("/static/", cacheStaticAssets(fileServer)))
114113
// 404 handler for all other routes
@@ -152,53 +151,54 @@ func main() {
152151
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo})))
153152
}
154153

155-
log.Printf("Production: %v", flgProduction)
156-
log.Printf("HTTP Port: %s", httpPort)
154+
slog.Info("Production", "enabled", flgProduction)
155+
slog.Info("HTTP Port", "port", httpPort)
157156

158157
httpSrv := makeHTTPServer(flgProduction)
159158
httpSrv.Addr = httpPort
160159

161160
// Start server in goroutine
162161
go func() {
163-
log.Printf("Starting HTTP server on %s", httpSrv.Addr)
162+
slog.Info("Starting HTTP server", "addr", httpSrv.Addr)
164163
if err := httpSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
165-
log.Fatalf("httpSrv.ListenAndServe() failed: %v", err)
164+
slog.Error("httpSrv.ListenAndServe() failed", "error", err)
165+
os.Exit(1)
166166
}
167167
}()
168168

169169
// Wait for interrupt signal for graceful shutdown
170170
quit := make(chan os.Signal, 1)
171171
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
172172
<-quit
173-
log.Println("Shutting down server...")
173+
slog.Info("Shutting down server...")
174174

175175
// Give outstanding requests 5 seconds to complete
176176
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
177177
defer cancel()
178178
if err := httpSrv.Shutdown(ctx); err != nil {
179-
log.Printf("Server forced to shutdown: %v", err)
179+
slog.Error("Server forced to shutdown", "error", err)
180180
}
181181

182-
log.Println("Server exited")
182+
slog.Info("Server exited")
183183
}
184184

185185
func about(w http.ResponseWriter, r *http.Request) {
186186
w.Header().Set("Content-Type", "text/html; charset=utf-8")
187187
if templates != nil {
188188
if err := templates.ExecuteTemplate(w, "about.html", nil); err != nil {
189-
log.Printf("Error executing about template: %v", err)
189+
slog.Error("Error executing about template", "error", err)
190190
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
191191
}
192192
} else {
193193
// Fallback to parsing on demand
194194
t, err := template.ParseFiles("about.html")
195195
if err != nil {
196196
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
197-
log.Printf("Error parsing about template: %v", err)
197+
slog.Error("Error parsing about template", "error", err)
198198
return
199199
}
200200
if err := t.Execute(w, nil); err != nil {
201-
log.Printf("Error executing about template: %v", err)
201+
slog.Error("Error executing about template", "error", err)
202202
}
203203
}
204204
}
@@ -297,19 +297,19 @@ func calendar(w http.ResponseWriter, r *http.Request) {
297297

298298
if templates != nil {
299299
if err := templates.ExecuteTemplate(w, "calendar.html", &Passme); err != nil {
300-
log.Printf("Error executing calendar template: %v", err)
300+
slog.Error("Error executing calendar template", "error", err)
301301
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
302302
}
303303
} else {
304304
// Fallback to parsing on demand
305305
t, err := template.ParseFiles("calendar.html")
306306
if err != nil {
307307
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
308-
log.Printf("Error parsing calendar template: %v", err)
308+
slog.Error("Error parsing calendar template", "error", err)
309309
return
310310
}
311311
if err := t.Execute(w, &Passme); err != nil {
312-
log.Printf("Error executing calendar template: %v", err)
312+
slog.Error("Error executing calendar template", "error", err)
313313
}
314314
}
315315
}
@@ -327,19 +327,19 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {
327327

328328
if templates != nil {
329329
if err := templates.ExecuteTemplate(w, "index.html", data); err != nil {
330-
log.Printf("Error executing index template: %v", err)
330+
slog.Error("Error executing index template", "error", err)
331331
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
332332
}
333333
} else {
334334
// Fallback to parsing on demand
335335
t, err := template.ParseFiles("index.html")
336336
if err != nil {
337337
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
338-
log.Printf("Error parsing index template: %v", err)
338+
slog.Error("Error parsing index template", "error", err)
339339
return
340340
}
341341
if err := t.Execute(w, data); err != nil {
342-
log.Printf("Error executing index template: %v", err)
342+
slog.Error("Error executing index template", "error", err)
343343
}
344344
}
345345
}

0 commit comments

Comments
 (0)