{"record":{"id":"e8ed6c2d95f2efe7","repo":"googleapis/mcp-toolbox","slug":"unable-to-retrieve-logger-w","errorCode":null,"errorMessage":"unable to retrieve logger: %w","messagePattern":"unable to retrieve logger: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/embeddingmodels/gemini/gemini.go","lineNumber":55,"sourceCode":"\tApiKey    string `yaml:\"apiKey\"`\n\tProject   string `yaml:\"project\"`\n\tLocation  string `yaml:\"location\"`\n\tDimension int32  `yaml:\"dimension\"`\n}\n\n// Returns the embedding model type\nfunc (cfg Config) EmbeddingModelConfigType() string {\n\treturn EmbeddingModelType\n}\n\n// Initialize a Gemini embedding model\nfunc (cfg Config) Initialize(ctx context.Context) (embeddingmodels.EmbeddingModel, error) {\n\tconfigs := &genai.ClientConfig{}\n\n\t// Retrieve logger from context\n\tl, err := util.LoggerFromContext(ctx)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"unable to retrieve logger: %w\", err)\n\t}\n\n\t// Get API Key\n\tapiKey := cfg.ApiKey\n\tif apiKey == \"\" {\n\t\tapiKey = os.Getenv(\"GOOGLE_API_KEY\")\n\t}\n\tif apiKey == \"\" {\n\t\tapiKey = os.Getenv(\"GEMINI_API_KEY\")\n\t}\n\n\t// Try to resolve Project and Location\n\tproject := cfg.Project\n\tif project == \"\" {\n\t\tproject = os.Getenv(\"GOOGLE_CLOUD_PROJECT\")\n\t}\n\n\tlocation := cfg.Location","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/embeddingmodels/gemini/gemini.go#L37-L73","documentation":"The Gemini embedding model's Config.Initialize retrieves a structured logger from the passed context via util.LoggerFromContext and fails fast with 'unable to retrieve logger' if none is present. Like the auth-side equivalent, it guards against running with a nil logger.","triggerScenarios":"Calling Initialize with a context lacking a logger: tests passing context.Background(), or application code not wrapping context with the toolbox logger utility before configuring embedding models.","commonSituations":"Custom embedding-model bootstrap code outside the standard server startup path; unit tests constructing contexts manually; frameworks that rebuild contexts and drop values.","solutions":["Wrap the context before Initialize: ctx = util.NewLoggerContext(ctx, slog.Default())","Use the toolbox's normal config-loading/server startup path, which injects the logger","In tests, add a logger to the context fixture","Check for middleware or helpers that strip context values"],"exampleFix":"// before\nmodel, err := cfg.Initialize(context.Background())\n// after\nctx := util.NewLoggerContext(context.Background(), slog.Default())\nmodel, err := cfg.Initialize(ctx)","handlingStrategy":"validation","validationCode":"if _, err := util.LoggerFromContext(ctx); err != nil {\n    ctx = util.NewLoggerContext(ctx, slog.Default())\n}\nmodel, err := cfg.Initialize(ctx)","typeGuard":"func contextHasLogger(ctx context.Context) bool {\n    _, err := util.LoggerFromContext(ctx)\n    return err == nil\n}","tryCatchPattern":"model, err := cfg.Initialize(ctx)\nif err != nil && strings.Contains(err.Error(), \"unable to retrieve logger\") {\n    ctx = util.NewLoggerContext(ctx, slog.Default())\n    model, err = cfg.Initialize(ctx)\n}","preventionTips":["Always build contexts through the toolbox's logger-injecting helpers","Fix test fixtures to supply a logger-bearing context","Avoid context-rewriting helpers that drop context values","Centralize context creation so logger injection cannot be skipped"],"tags":["go","context","logging","gemini","embedding"],"backgroundTag":"missing-logger-in-context","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}