logger_linux.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // +build linux
  2. // Package logger is a wrapper around UNIX syslog, so that it also may
  3. // be wrapped with something else for Windows (Sadly, the stdlib log/syslog
  4. // is frozen, and there is no Windows implementation.)
  5. package logger
  6. import (
  7. sl "log/syslog"
  8. )
  9. // Priority is the logger priority
  10. type Priority = sl.Priority
  11. // Writer is a syslog Writer
  12. type Writer = sl.Writer
  13. // nolint: golint
  14. const (
  15. // Severity.
  16. // From /usr/include/sys/syslog.h.
  17. // These are the same on Linux, BSD, and OS X.
  18. LOG_EMERG Priority = iota
  19. LOG_ALERT
  20. LOG_CRIT
  21. LOG_ERR
  22. LOG_WARNING
  23. LOG_NOTICE
  24. LOG_INFO
  25. LOG_DEBUG
  26. )
  27. // nolint: golint
  28. const (
  29. // Facility.
  30. // From /usr/include/sys/syslog.h.
  31. // These are the same up to LOG_FTP on Linux, BSD, and OS X.
  32. LOG_KERN Priority = iota << 3
  33. LOG_USER
  34. LOG_MAIL
  35. LOG_DAEMON
  36. LOG_AUTH
  37. LOG_SYSLOG
  38. LOG_LPR
  39. LOG_NEWS
  40. LOG_UUCP
  41. LOG_CRON
  42. LOG_AUTHPRIV
  43. LOG_FTP
  44. _ // unused
  45. _ // unused
  46. _ // unused
  47. _ // unused
  48. LOG_LOCAL0
  49. LOG_LOCAL1
  50. LOG_LOCAL2
  51. LOG_LOCAL3
  52. LOG_LOCAL4
  53. LOG_LOCAL5
  54. LOG_LOCAL6
  55. LOG_LOCAL7
  56. )
  57. var (
  58. l *sl.Writer
  59. )
  60. // New returns a new log Writer.
  61. func New(flags Priority, tag string) (w *Writer, e error) {
  62. w, e = sl.New(flags, tag)
  63. l = w
  64. return w, e
  65. }
  66. // Alert returns a log Alert error
  67. func Alert(s string) error {
  68. if l != nil {
  69. return l.Alert(s)
  70. }
  71. return nil
  72. }
  73. // LogClose closes the log Writer.
  74. func LogClose() error {
  75. if l != nil {
  76. return l.Close()
  77. }
  78. return nil
  79. }
  80. // LogCrit returns a log Alert error
  81. func LogCrit(s string) error {
  82. if l != nil {
  83. return l.Crit(s)
  84. }
  85. return nil
  86. }
  87. // LogDebug returns a log Debug error
  88. func LogDebug(s string) error {
  89. if l != nil {
  90. return l.Debug(s)
  91. }
  92. return nil
  93. }
  94. // LogEmerg returns a log Emerg error
  95. func LogEmerg(s string) error {
  96. if l != nil {
  97. return l.Emerg(s)
  98. }
  99. return nil
  100. }
  101. // LogErr returns a log Err error
  102. func LogErr(s string) error {
  103. if l != nil {
  104. return l.Err(s)
  105. }
  106. return nil
  107. }
  108. // LogInfo returns a log Info error
  109. func LogInfo(s string) error {
  110. if l != nil {
  111. return l.Info(s)
  112. }
  113. return nil
  114. }
  115. // LogNotice returns a log Notice error
  116. func LogNotice(s string) error {
  117. if l != nil {
  118. return l.Notice(s)
  119. }
  120. return nil
  121. }
  122. // LogWarning returns a log Warning error
  123. func LogWarning(s string) error {
  124. if l != nil {
  125. return l.Warning(s)
  126. }
  127. return nil
  128. }
  129. // LogWrite writes to the logger at default level
  130. func LogWrite(b []byte) (int, error) {
  131. if l != nil {
  132. return l.Write(b)
  133. }
  134. return len(b),nil
  135. }