{"record":{"id":"5f71f242eef13956","repo":"owasp-amass/amass","slug":"failed-to-cast-the-level-value","errorCode":null,"errorMessage":"failed to cast the level value","messagePattern":"failed to cast the level value","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/afmt/slog.go","lineNumber":38,"sourceCode":"\t\treturn slog.Record{}, errors.New(\"failed to unmarchal the JSON\")\n\t}\n\n\tltime := time.Now()\n\tif timeVal, found := j[slog.TimeKey]; found {\n\t\tif timestr, valid := timeVal.(string); valid {\n\t\t\tif t, err := time.Parse(\"2006-01-02T15:04:05.000000000Z\", timestr); err == nil {\n\t\t\t\tltime = t\n\t\t\t}\n\t\t}\n\t}\n\tdelete(j, slog.TimeKey)\n\n\tvar level slog.Level\n\t// extract the log level for the new record\n\tif val, found := j[slog.LevelKey]; !found {\n\t\treturn slog.Record{}, errors.New(\"failed to find the level key\")\n\t} else if str, ok := val.(string); !ok {\n\t\treturn slog.Record{}, errors.New(\"failed to cast the level value\")\n\t} else if level.UnmarshalText([]byte(str)) != nil {\n\t\treturn slog.Record{}, errors.New(\"failed to unmarshal the level text\")\n\t}\n\tdelete(j, slog.LevelKey)\n\n\tvar msg string\n\t// extract the log message for the new record\n\tif val, found := j[slog.MessageKey]; !found {\n\t\treturn slog.Record{}, errors.New(\"failed to find the msg key\")\n\t} else if str, ok := val.(string); !ok {\n\t\treturn slog.Record{}, errors.New(\"failed to cast the msg value\")\n\t} else {\n\t\tmsg = str\n\t}\n\tdelete(j, slog.MessageKey)\n\n\tvar pc uintptr\n\trecord := slog.NewRecord(ltime, level, msg, pc)","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/owasp-amass/amass/blob/79299dce87b0085db0f2f4ef3e9c52cccb49f514/internal/afmt/slog.go#L20-L56","documentation":"When the \"level\" key is present in the parsed JSON but its value is not a JSON string (e.g. a number like 3 or null), the type assertion val.(string) fails and this error is returned. slog.Level.UnmarshalText expects textual level names (DEBUG, INFO, WARN, ERROR), so a non-string level cannot be converted.","triggerScenarios":"JSONLogToRecord receives a log line where j[\"level\"] is an int, float, bool, null, or nested object instead of a string.","commonSituations":"A producer serializing numeric log levels (0-4) while the consumer expects text; hand-written or tool-generated log lines; a schema mismatch between two library versions.","solutions":["Produce log levels as strings (\"DEBUG\",\"INFO\",\"WARN\",\"ERROR\") in the engine's JSON output","If you control the producer, convert numeric levels to text before writing the line","If you cannot change the producer, pre-process the JSON map and stringify numeric levels before calling JSONLogToRecord","Check producer/consumer library versions for schema agreement"],"exampleFix":"// before\n{\"time\":\"...\",\"level\":3,\"msg\":\"started\"}\n// after\n{\"time\":\"...\",\"level\":\"INFO\",\"msg\":\"started\"}","handlingStrategy":"type-guard","validationCode":"if v, ok := j[slog.LevelKey]; ok {\n    if _, isStr := v.(string); !isStr {\n        return errors.New(\"level value must be a JSON string\")\n    }\n}","typeGuard":"func levelIsString(j map[string]any) bool {\n    v, ok := j[slog.LevelKey]\n    if !ok {\n        return false\n    }\n    _, isStr := v.(string)\n    return isStr\n}","tryCatchPattern":"rec, err := afmt.JSONLogToRecord(line)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to cast the level value\") {\n        rawLog.Printf(\"non-string level in log line: %q\", line)\n        return nil\n    }\n    return err\n}","preventionTips":["Serialize log levels as strings, never numeric codes","Normalize producer output so level is always a quoted level name","Add a schema check on the producer side before writing log lines","Keep producer and consumer on schema-compatible versions"],"tags":["logging","json","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"79299dce87b0085db0f2f4ef3e9c52cccb49f514","analyzedAt":"2026-09-06T08:22:48.198Z","contentChangedAt":"2026-09-06T08:22:48.198Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}