log.go 5.0 KB

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