log.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2016, 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 server
  20. import (
  21. "io"
  22. "os"
  23. "github.com/Psiphon-Inc/logrus"
  24. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  25. )
  26. // ContextLogger adds context logging functionality to the
  27. // underlying logging packages.
  28. type ContextLogger struct {
  29. *logrus.Logger
  30. }
  31. // LogFields is an alias for the field struct in the
  32. // underlying logging package.
  33. type LogFields logrus.Fields
  34. // WithContext adds a "context" field containing the caller's
  35. // function name and source file line number. Use this function
  36. // when the log has no fields.
  37. func (logger *ContextLogger) WithContext() *logrus.Entry {
  38. return log.WithFields(
  39. logrus.Fields{
  40. "context": psiphon.GetParentContext(),
  41. })
  42. }
  43. // WithContextFields adds a "context" field containing the caller's
  44. // function name and source file line number. Use this function
  45. // when the log has fields. Note that any existing "context" field
  46. // will be renamed to "field.context".
  47. func (logger *ContextLogger) WithContextFields(fields LogFields) *logrus.Entry {
  48. _, ok := fields["context"]
  49. if ok {
  50. fields["fields.context"] = fields["context"]
  51. }
  52. fields["context"] = psiphon.GetParentContext()
  53. return log.WithFields(logrus.Fields(fields))
  54. }
  55. // NewLogWriter returns an io.PipeWriter that can be used to write
  56. // to the global logger. Caller must Close() the writer.
  57. func NewLogWriter() *io.PipeWriter {
  58. return log.Writer()
  59. }
  60. var log *ContextLogger
  61. // InitLogging configures a logger according to the specified
  62. // config params. If not called, the default logger set by the
  63. // package init() is used.
  64. // Concurrenty note: should only be called from the main
  65. // goroutine.
  66. func InitLogging(config *Config) error {
  67. level, err := logrus.ParseLevel(config.LogLevel)
  68. if err != nil {
  69. return psiphon.ContextError(err)
  70. }
  71. logWriter := os.Stderr
  72. if config.LogFilename != "" {
  73. logWriter, err = os.OpenFile(
  74. config.LogFilename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
  75. if err != nil {
  76. return psiphon.ContextError(err)
  77. }
  78. }
  79. log = &ContextLogger{
  80. &logrus.Logger{
  81. Out: logWriter,
  82. Formatter: &logrus.JSONFormatter{},
  83. Level: level,
  84. },
  85. }
  86. return nil
  87. }
  88. func init() {
  89. log = &ContextLogger{
  90. &logrus.Logger{
  91. Out: os.Stderr,
  92. Formatter: &logrus.JSONFormatter{},
  93. Hooks: make(logrus.LevelHooks),
  94. Level: logrus.DebugLevel,
  95. },
  96. }
  97. }