logger.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2017, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package common
  20. // Logger exposes a logging interface that's compatible with
  21. // psiphon/server.TraceLogger. This interface allows packages
  22. // to implement logging that will integrate with psiphon/server
  23. // without importing that package. Other implementations of
  24. // Logger may also be provided.
  25. type Logger interface {
  26. WithTrace() LogTrace
  27. WithTraceFields(fields LogFields) LogTrace
  28. LogMetric(metric string, fields LogFields)
  29. // IsLogLevelDebug is used to skip formatting debug-level log messages in
  30. // cases where performance would be impacted.
  31. IsLogLevelDebug() bool
  32. }
  33. // LogTrace is interface-compatible with the return values from
  34. // psiphon/server.TraceLogger.WitTrace/WithTraceFields.
  35. type LogTrace interface {
  36. Debug(args ...interface{})
  37. Info(args ...interface{})
  38. Warning(args ...interface{})
  39. Error(args ...interface{})
  40. }
  41. // LogFields is type-compatible with psiphon/server.LogFields
  42. // and logrus.LogFields.
  43. type LogFields map[string]interface{}
  44. // Add copies log fields from b to a, skipping fields which already exist,
  45. // regardless of value, in a.
  46. func (a LogFields) Add(b LogFields) {
  47. for name, value := range b {
  48. _, ok := a[name]
  49. if !ok {
  50. a[name] = value
  51. }
  52. }
  53. }
  54. // MetricsSource is an object that provides metrics to be logged.
  55. type MetricsSource interface {
  56. // GetMetrics returns a LogFields populated with metrics from the
  57. // MetricsSource.
  58. GetMetrics() LogFields
  59. }
  60. // NoticeMetricsSource is an object that provides metrics to be logged
  61. // only in notices, for inclusion in diagnostics.
  62. type NoticeMetricsSource interface {
  63. // GetNoticeMetrics returns a LogFields populated with metrics from
  64. // the NoticeMetricsSource.
  65. GetNoticeMetrics() LogFields
  66. }