syslog.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package extern
  15. import (
  16. "fmt"
  17. "log/syslog"
  18. "strings"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/onecloud/pkg/appsrv"
  22. )
  23. var (
  24. sysLog ISyslogWriter
  25. syslogWorkerMan *appsrv.SWorkerManager
  26. )
  27. func init() {
  28. syslogWorkerMan = appsrv.NewWorkerManager("syslogSenderWorkerManager", 1, 50, false)
  29. }
  30. func InitSyslog(url string) error {
  31. proto := "tcp"
  32. addr := url
  33. tag := "cloudaudit"
  34. if strings.HasPrefix(url, "tcp://") {
  35. proto = "tcp"
  36. addr = url[6:]
  37. } else if strings.HasPrefix(url, "udp://") {
  38. proto = "udp"
  39. addr = url[6:]
  40. }
  41. if strings.Contains(addr, "@") {
  42. parts := strings.Split(addr, "@")
  43. addr = parts[0]
  44. tag = parts[1]
  45. }
  46. var err error
  47. sysLog, err = syslog.Dial(proto, addr, 0, tag)
  48. if err != nil {
  49. return errors.Wrap(err, "syslog.Dial")
  50. }
  51. log.Infof("Start syslog to %s://%s@%s", proto, addr, tag)
  52. return nil
  53. }
  54. func Emergency(msg string) {
  55. if sysLog == nil {
  56. return
  57. }
  58. sendSyslog("emerg", msg)
  59. }
  60. func Alert(msg string) {
  61. if sysLog == nil {
  62. return
  63. }
  64. sendSyslog("alert", msg)
  65. }
  66. func Critical(msg string) {
  67. if sysLog == nil {
  68. return
  69. }
  70. sendSyslog("crit", msg)
  71. }
  72. func Error(msg string) {
  73. if sysLog == nil {
  74. return
  75. }
  76. sendSyslog("err", msg)
  77. }
  78. func Warning(msg string) {
  79. if sysLog == nil {
  80. return
  81. }
  82. sendSyslog("warning", msg)
  83. }
  84. func Notice(msg string) {
  85. if sysLog == nil {
  86. return
  87. }
  88. sendSyslog("notice", msg)
  89. }
  90. func Info(msg string) {
  91. if sysLog == nil {
  92. return
  93. }
  94. sendSyslog("info", msg)
  95. }
  96. func Debug(msg string) {
  97. if sysLog == nil {
  98. return
  99. }
  100. sendSyslog("debug", msg)
  101. }
  102. func sendSyslog(facility string, msg string) {
  103. log.Infof("%s", msg)
  104. syslogWorkerMan.Run(&syslogTask{
  105. facility: facility,
  106. msg: msg,
  107. }, nil, nil)
  108. }
  109. type syslogTask struct {
  110. facility string
  111. msg string
  112. }
  113. func (t *syslogTask) Run() {
  114. if sysLog == nil {
  115. return
  116. }
  117. switch t.facility {
  118. case "info":
  119. sysLog.Info(t.msg)
  120. case "debug":
  121. sysLog.Debug(t.msg)
  122. case "emerg":
  123. sysLog.Emerg(t.msg)
  124. case "warning":
  125. sysLog.Warning(t.msg)
  126. case "notice":
  127. sysLog.Notice(t.msg)
  128. case "err":
  129. sysLog.Err(t.msg)
  130. case "crit":
  131. sysLog.Crit(t.msg)
  132. case "Alert":
  133. sysLog.Alert(t.msg)
  134. default:
  135. sysLog.Info(t.msg)
  136. }
  137. }
  138. func (t *syslogTask) Dump() string {
  139. return fmt.Sprintf("syslogTask %v %s", t.facility, t.msg)
  140. }