logger.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package thrift
  20. import (
  21. "log"
  22. "os"
  23. )
  24. // Logger is a simple wrapper of a logging function.
  25. //
  26. // In reality the users might actually use different logging libraries, and they
  27. // are not always compatible with each other.
  28. //
  29. // Logger is meant to be a simple common ground that it's easy to wrap whatever
  30. // logging library they use into.
  31. //
  32. // See https://issues.apache.org/jira/browse/THRIFT-4985 for the design
  33. // discussion behind it.
  34. type Logger func(msg string)
  35. // NopLogger is a Logger implementation that does nothing.
  36. func NopLogger(msg string) {}
  37. // StdLogger wraps stdlib log package into a Logger.
  38. //
  39. // If logger passed in is nil, it will fallback to use stderr and default flags.
  40. func StdLogger(logger *log.Logger) Logger {
  41. if logger == nil {
  42. logger = log.New(os.Stderr, "", log.LstdFlags)
  43. }
  44. return func(msg string) {
  45. logger.Print(msg)
  46. }
  47. }
  48. func fallbackLogger(logger Logger) Logger {
  49. if logger == nil {
  50. return StdLogger(nil)
  51. }
  52. return logger
  53. }