- Implemented new gRPC service `AIService` in `proto/ai/v1/ai.proto` for handling natural language queries. - Generated Go code for the gRPC service and messages in `gen/ai/v1/`. - Created `services/ai-gateway/` directory structure with necessary files for the service. - Added configuration loading and structured logging. - Implemented domain logic for intent parsing and interaction with Home Assistant. - Established outbound adapters for Ollama and Home Assistant with mTLS support. - Updated `go.work` to include the new service and maintain existing dependencies. - Modified `discord-bot` to use the new `ai-gateway` for AI interactions. - Added deployment manifest for Kubernetes and CI/CD configuration for building and deploying the service.
39 lines
1013 B
Go
39 lines
1013 B
Go
package logger
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
)
|
|
|
|
type contextKey struct{}
|
|
|
|
// New constructs a root logger for the configured format and level.
|
|
func New(format, level string) *slog.Logger {
|
|
var parsed slog.Level
|
|
if err := parsed.UnmarshalText([]byte(level)); err != nil {
|
|
_, _ = fmt.Fprintf(os.Stderr, "invalid log level %q, falling back to info\n", level)
|
|
parsed = slog.LevelInfo
|
|
}
|
|
|
|
opts := &slog.HandlerOptions{Level: parsed}
|
|
if format == "json" {
|
|
return slog.New(slog.NewJSONHandler(os.Stdout, opts))
|
|
}
|
|
return slog.New(slog.NewTextHandler(os.Stdout, opts))
|
|
}
|
|
|
|
// WithLogger attaches a logger to the provided context.
|
|
func WithLogger(ctx context.Context, l *slog.Logger) context.Context {
|
|
return context.WithValue(ctx, contextKey{}, l)
|
|
}
|
|
|
|
// FromContext retrieves a logger from context and falls back to slog.Default().
|
|
func FromContext(ctx context.Context) *slog.Logger {
|
|
if l, ok := ctx.Value(contextKey{}).(*slog.Logger); ok && l != nil {
|
|
return l
|
|
}
|
|
return slog.Default()
|
|
}
|