log.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. "log/syslog"
  23. "os"
  24. "github.com/Psiphon-Inc/logrus"
  25. logrus_syslog "github.com/Psiphon-Inc/logrus/hooks/syslog"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  27. )
  28. // ContextLogger adds context logging functionality to the
  29. // underlying logging packages.
  30. type ContextLogger struct {
  31. *logrus.Logger
  32. }
  33. // LogFields is an alias for the field struct in the
  34. // underlying logging package.
  35. type LogFields logrus.Fields
  36. // WithContext adds a "context" field containing the caller's
  37. // function name and source file line number. Use this function
  38. // when the log has no fields.
  39. func (logger *ContextLogger) WithContext() *logrus.Entry {
  40. return log.WithFields(
  41. logrus.Fields{
  42. "context": psiphon.GetParentContext(),
  43. })
  44. }
  45. // WithContextFields adds a "context" field containing the caller's
  46. // function name and source file line number. Use this function
  47. // when the log has fields. Note that any existing "context" field
  48. // will be renamed to "field.context".
  49. func (logger *ContextLogger) WithContextFields(fields LogFields) *logrus.Entry {
  50. _, ok := fields["context"]
  51. if ok {
  52. fields["fields.context"] = fields["context"]
  53. }
  54. fields["context"] = psiphon.GetParentContext()
  55. return log.WithFields(logrus.Fields(fields))
  56. }
  57. // NewLogWriter returns an io.PipeWriter that can be used to write
  58. // to the global logger. Caller must Close() the writer.
  59. func NewLogWriter() *io.PipeWriter {
  60. return log.Writer()
  61. }
  62. var log *ContextLogger
  63. // InitLogging configures a logger according to the specified
  64. // config params. If not called, the default logger set by the
  65. // package init() is used.
  66. // When configured, InitLogging also establishes a local syslog
  67. // logger specifically for fail2ban integration.
  68. // Concurrenty note: should only be called from the main
  69. // goroutine.
  70. func InitLogging(config *Config) error {
  71. level, err := logrus.ParseLevel(config.LogLevel)
  72. if err != nil {
  73. return psiphon.ContextError(err)
  74. }
  75. hooks := make(logrus.LevelHooks)
  76. var syslogHook *logrus_syslog.SyslogHook
  77. if config.SyslogFacility != "" {
  78. syslogHook, err = logrus_syslog.NewSyslogHook(
  79. "", "", getSyslogPriority(config), config.SyslogTag)
  80. if err != nil {
  81. return psiphon.ContextError(err)
  82. }
  83. hooks.Add(syslogHook)
  84. }
  85. log = &ContextLogger{
  86. &logrus.Logger{
  87. Out: os.Stderr,
  88. Formatter: new(logrus.JSONFormatter),
  89. Hooks: hooks,
  90. Level: level,
  91. },
  92. }
  93. return nil
  94. }
  95. // getSyslogPriority determines golang's syslog "priority" value
  96. // based on the provided config.
  97. func getSyslogPriority(config *Config) syslog.Priority {
  98. // TODO: assumes log.Level filter applies?
  99. severity := syslog.LOG_DEBUG
  100. facilityCodes := map[string]syslog.Priority{
  101. "kern": syslog.LOG_KERN,
  102. "user": syslog.LOG_USER,
  103. "mail": syslog.LOG_MAIL,
  104. "daemon": syslog.LOG_DAEMON,
  105. "auth": syslog.LOG_AUTH,
  106. "syslog": syslog.LOG_SYSLOG,
  107. "lpr": syslog.LOG_LPR,
  108. "news": syslog.LOG_NEWS,
  109. "uucp": syslog.LOG_UUCP,
  110. "cron": syslog.LOG_CRON,
  111. "authpriv": syslog.LOG_AUTHPRIV,
  112. "ftp": syslog.LOG_FTP,
  113. "local0": syslog.LOG_LOCAL0,
  114. "local1": syslog.LOG_LOCAL1,
  115. "local2": syslog.LOG_LOCAL2,
  116. "local3": syslog.LOG_LOCAL3,
  117. "local4": syslog.LOG_LOCAL4,
  118. "local5": syslog.LOG_LOCAL5,
  119. "local6": syslog.LOG_LOCAL6,
  120. "local7": syslog.LOG_LOCAL7,
  121. }
  122. facility, ok := facilityCodes[config.SyslogFacility]
  123. if !ok {
  124. facility = syslog.LOG_USER
  125. }
  126. return severity | facility
  127. }
  128. func init() {
  129. log = &ContextLogger{
  130. &logrus.Logger{
  131. Out: os.Stderr,
  132. Formatter: new(logrus.JSONFormatter),
  133. Hooks: make(logrus.LevelHooks),
  134. Level: logrus.DebugLevel,
  135. },
  136. }
  137. }