logging.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. Copyright 2021 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "github.com/coreos/pkg/capnslog"
  16. )
  17. const DefaultLogLevel = capnslog.INFO
  18. func SetGlobalLogLevel(userLogLevelSelection string, logger *capnslog.PackageLogger) {
  19. // capnslog supports trace level logging, but in Rook we want to treat trace logging as insecure
  20. // and block users from finding the value in most circumstances. If they request "TRACE" level
  21. // logging, just output debug logs.
  22. if userLogLevelSelection == "TRACE" {
  23. userLogLevelSelection = "DEBUG"
  24. }
  25. // only if users give the super secret "TRACE_INSECURE" log level will they get real trace
  26. // logging, which might leak credentials and other insecure nasties into their logs.
  27. if userLogLevelSelection == "TRACE_INSECURE" {
  28. userLogLevelSelection = "TRACE"
  29. }
  30. // parse given log level string then set up corresponding global logging level
  31. logLevel, err := capnslog.ParseLevel(userLogLevelSelection)
  32. if err != nil {
  33. logger.Errorf("failed to parse log level %q. defaulting to %q. %v", userLogLevelSelection, DefaultLogLevel.String(), err)
  34. logLevel = DefaultLogLevel
  35. }
  36. // If capnslog changes in the future to allow a more verbose level than TRACE and a user somehow
  37. // enters it, then reject that log level, and revert to default. This can't be unit tested, but
  38. // it'll probably never happen in the wild anyway, just here for safety.
  39. if logLevel > capnslog.TRACE {
  40. logger.Infof("not setting log level %q more verbose than TRACE. reverting to default %q", logLevel.String(), DefaultLogLevel.String())
  41. logLevel = DefaultLogLevel
  42. }
  43. capnslog.SetGlobalLogLevel(logLevel)
  44. }