NewStructuredLogger creates a Logger that logs messages using JSON.
(quiet bool)
| 79 | |
| 80 | // NewStructuredLogger creates a Logger that logs messages using JSON. |
| 81 | func NewStructuredLogger(quiet bool) cloudsql.Logger { |
| 82 | var infoHandler, errorHandler slog.Handler |
| 83 | if quiet { |
| 84 | infoHandler = slog.DiscardHandler |
| 85 | } else { |
| 86 | infoHandler = slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ |
| 87 | Level: slog.LevelDebug, |
| 88 | ReplaceAttr: replaceAttr, |
| 89 | }) |
| 90 | } |
| 91 | errorHandler = slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{ |
| 92 | Level: slog.LevelError, |
| 93 | ReplaceAttr: replaceAttr, |
| 94 | }) |
| 95 | |
| 96 | l := &StructuredLogger{ |
| 97 | stdLog: slog.New(infoHandler), |
| 98 | errLog: slog.New(errorHandler), |
| 99 | } |
| 100 | return l |
| 101 | } |
| 102 | |
| 103 | // replaceAttr remaps default Go logging keys to adhere to LogEntry format |
| 104 | // https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry |