{"record":{"id":"6ed7f38863dc18fb","repo":"grpc/grpc-go","slug":"cannot-use-component-logger-as-grpclog-logger","errorCode":null,"errorMessage":"cannot use component logger as grpclog logger","messagePattern":"cannot use component logger as grpclog logger","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"grpclog/loggerv2.go","lineNumber":37,"sourceCode":"package grpclog\n\nimport (\n\t\"io\"\n\t\"os\"\n\t\"strconv\"\n\t\"strings\"\n\n\t\"google.golang.org/grpc/grpclog/internal\"\n)\n\n// LoggerV2 does underlying logging work for grpclog.\ntype LoggerV2 internal.LoggerV2\n\n// SetLoggerV2 sets logger that is used in grpc to a V2 logger.\n// Not mutex-protected, should be called before any gRPC functions.\nfunc SetLoggerV2(l LoggerV2) {\n\tif _, ok := l.(*componentData); ok {\n\t\tpanic(\"cannot use component logger as grpclog logger\")\n\t}\n\tinternal.LoggerV2Impl = l\n\tinternal.DepthLoggerV2Impl, _ = l.(internal.DepthLoggerV2)\n}\n\n// NewLoggerV2 creates a loggerV2 with the provided writers.\n// Fatal logs will be written to errorW, warningW, infoW, followed by exit(1).\n// Error logs will be written to errorW, warningW and infoW.\n// Warning logs will be written to warningW and infoW.\n// Info logs will be written to infoW.\nfunc NewLoggerV2(infoW, warningW, errorW io.Writer) LoggerV2 {\n\treturn internal.NewLoggerV2(infoW, warningW, errorW, internal.LoggerV2Config{})\n}\n\n// NewLoggerV2WithVerbosity creates a loggerV2 with the provided writers and\n// verbosity level.\nfunc NewLoggerV2WithVerbosity(infoW, warningW, errorW io.Writer, v int) LoggerV2 {\n\treturn internal.NewLoggerV2(infoW, warningW, errorW, internal.LoggerV2Config{Verbosity: v})","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/grpc/grpc-go/blob/03255a9237b6eb32710f6bc4f2de9a675b99fe36/grpclog/loggerv2.go#L19-L55","documentation":"grpclog.SetLoggerV2 (loggerv2.go:35) sets the global gRPC logger and panics if you pass it a *componentData (the type returned by grpclog.Component(...)). A component logger is scoped to a sub-component and delegates back to the global logger, so making it the global logger would cause infinite recursion; gRPC refuses this to prevent a stack overflow at first log.","triggerScenarios":"Calling grpclog.SetLoggerV2(grpclog.Component(...)) directly, or assigning a component logger (obtained via Component(packageName)) into the variable you later pass to SetLoggerV2.","commonSituations":"Copy-pasting logger-setup code; confusing the global logger with a per-component logger; refactor that swapped in a component logger where a real logger was expected.","solutions":["Pass a real logger built with grpclog.NewLoggerV2(...) (or your own LoggerV2 impl), not a value from grpclog.Component(...).","If you intended to set a component-specific logger, that is not what SetLoggerV2 does; set the global logger and let components inherit it.","Rename variables so a component logger is never accidentally passed as the global one."],"exampleFix":"// before\ncompLog := grpclog.Component(\"myapp\")\ngrpclog.SetLoggerV2(compLog) // panics\n\n// after\ngrpclog.SetLoggerV2(grpclog.NewLoggerV2(os.Stdout, os.Stderr, os.Stderr))","handlingStrategy":"validation","validationCode":"// Never pass a component logger to SetLoggerV2\nfunc setGlobalLogger(l grpclog.LoggerV2) {\n    if l == nil { log.Fatal(\"logger nil\") }\n    // component loggers are *componentData (unexported); the safe rule:\n    // only pass loggers from NewLoggerV2 or your own LoggerV2 impl.\n    grpclog.SetLoggerV2(l)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep a clear type distinction: component loggers (from Component()) are NEVER global loggers.","Pass only grpclog.NewLoggerV2(...) or a custom LoggerV2 implementation to SetLoggerV2.","Call SetLoggerV2 once at startup before any gRPC activity."],"tags":["logging","grpclog","panic","grpc-go","config"],"analyzedSha":"03255a9237b6eb32710f6bc4f2de9a675b99fe36","analyzedAt":"2026-08-07T00:29:34.215Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}