refactor(log): split location into package/file/class/method, auto-derived
Per the log schema: 'package' is the Go package (absent on Python-emitted lines), 'file' the Go/Python source basename, 'class' the Go receiver or Python class (omitted when there is none -- free functions no longer masquerade under a package-as-class), 'method' the emitting function. applog.App() now takes no arguments and derives all of it from runtime.Caller, so labels can never drift from the code; the manual http/wrapper emitters and forwarded wrapper.py lines (file= wrapper.py, no package) carry the same fields. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -227,7 +227,9 @@ func (c *subprocessClient) execute(ctx context.Context, cmd string, params any)
|
||||
defer func() {
|
||||
attrs := []slog.Attr{
|
||||
slog.String("type", "wrapper"),
|
||||
slog.String("class", "garmin.subprocessClient"),
|
||||
slog.String("package", "garmin"),
|
||||
slog.String("file", "client.go"),
|
||||
slog.String("class", "subprocessClient"),
|
||||
slog.String("method", "execute"),
|
||||
slog.String("cmd", cmd),
|
||||
slog.Any("params", params),
|
||||
@@ -384,17 +386,18 @@ func forwardWrapperStderr(r io.Reader) {
|
||||
line := scanner.Text()
|
||||
var entry map[string]any
|
||||
if err := json.Unmarshal([]byte(line), &entry); err != nil || entry["msg"] == nil {
|
||||
slog.Warn("", "type", "wrapper", "class", "garmin", "method", "forwardWrapperStderr", "line", line)
|
||||
slog.Warn("", "type", "wrapper", "package", "garmin", "file", "client.go", "method", "forwardWrapperStderr", "line", line)
|
||||
continue
|
||||
}
|
||||
msg, _ := entry["msg"].(string)
|
||||
levelName, _ := entry["level"].(string)
|
||||
delete(entry, "msg")
|
||||
delete(entry, "level")
|
||||
// class is the Python module; method arrives in the entry itself
|
||||
// Python-emitted lines carry no "package" (a Go-only field) and no
|
||||
// "class" (wrapper.py has none); method arrives in the entry itself
|
||||
// (wrapper.py's _log stamps the emitting function).
|
||||
attrs := make([]slog.Attr, 0, len(entry)+2)
|
||||
attrs = append(attrs, slog.String("type", "wrapper"), slog.String("class", "wrapper"))
|
||||
attrs = append(attrs, slog.String("type", "wrapper"), slog.String("file", "wrapper.py"))
|
||||
for k, v := range entry {
|
||||
attrs = append(attrs, slog.Any(k, v))
|
||||
}
|
||||
|
||||
@@ -448,8 +448,8 @@ func TestSubprocessClient_RoundTrip_LogsCallWithResultPreview(t *testing.T) {
|
||||
if err := json.Unmarshal(buf.Bytes(), &entry); err != nil {
|
||||
t.Fatalf("log output is not valid JSON: %v (%q)", err, buf.String())
|
||||
}
|
||||
if entry["type"] != "wrapper" || entry["class"] != "garmin.subprocessClient" || entry["method"] != "execute" {
|
||||
t.Errorf("schema fields = %v, want type=wrapper class=garmin.subprocessClient method=execute", entry)
|
||||
if entry["type"] != "wrapper" || entry["package"] != "garmin" || entry["file"] != "client.go" || entry["class"] != "subprocessClient" || entry["method"] != "execute" {
|
||||
t.Errorf("schema fields = %v, want type=wrapper package=garmin file=client.go class=subprocessClient method=execute", entry)
|
||||
}
|
||||
if _, hasMsg := entry["msg"]; hasMsg {
|
||||
t.Errorf("expected no msg on the wrapper-call record, got %v", entry["msg"])
|
||||
@@ -516,8 +516,8 @@ func TestForwardWrapperStderr_ReemitsJSONAndWrapsFreeText(t *testing.T) {
|
||||
if first["level"] != "WARN" || first["msg"] != "startup tokenstore login failed" {
|
||||
t.Errorf("first = %v, want the wrapper's level and msg preserved", first)
|
||||
}
|
||||
if first["error"] != "boom" || first["type"] != "wrapper" || first["class"] != "wrapper" {
|
||||
t.Errorf("first = %v, want error attr preserved and type=wrapper class=wrapper", first)
|
||||
if first["error"] != "boom" || first["type"] != "wrapper" || first["file"] != "wrapper.py" {
|
||||
t.Errorf("first = %v, want error attr preserved and type=wrapper file=wrapper.py", first)
|
||||
}
|
||||
|
||||
var second map[string]any
|
||||
|
||||
@@ -368,12 +368,12 @@ func (s *Sync) fillPendingWorkouts(ctx context.Context, limit int) error {
|
||||
// after being linked to this activity) will never succeed on
|
||||
// retry -- mark it so ActivitiesMissingWorkout stops
|
||||
// surfacing it, instead of retrying forever.
|
||||
applog.App("garmin.Sync", "fillPendingWorkouts").Warn("workout not found on Garmin, marking as such (will not retry)", "garmin_activity_id", a.GarminActivityID, "error", err)
|
||||
applog.App().Warn("workout not found on Garmin, marking as such (will not retry)", "garmin_activity_id", a.GarminActivityID, "error", err)
|
||||
if serr := s.db.SetActivityWorkoutNotFound(ctx, s.userID, a.ID); serr != nil {
|
||||
return fmt.Errorf("mark activity %d workout not found: %w", a.GarminActivityID, serr)
|
||||
}
|
||||
} else {
|
||||
applog.App("garmin.Sync", "fillPendingWorkouts").Warn("fill workout failed, will retry next sync", "garmin_activity_id", a.GarminActivityID, "error", err)
|
||||
applog.App().Warn("fill workout failed, will retry next sync", "garmin_activity_id", a.GarminActivityID, "error", err)
|
||||
}
|
||||
}
|
||||
s.setProgress(PhaseWorkouts, i+1, len(pending))
|
||||
|
||||
@@ -31,7 +31,7 @@ def _log(level, msg, **attrs):
|
||||
never print free text to stderr or stdout from this process (stdout is
|
||||
reserved for the request/response protocol). The emitting function is
|
||||
stamped as "method" automatically; the Go side adds the rest of the
|
||||
log schema (type=wrapper, class=wrapper)."""
|
||||
log schema (type=wrapper, file=wrapper.py)."""
|
||||
entry = {"level": level, "msg": msg, "method": sys._getframe(1).f_code.co_name}
|
||||
entry.update(attrs)
|
||||
print(json.dumps(entry), file=sys.stderr, flush=True)
|
||||
|
||||
Reference in New Issue
Block a user