1
0

log.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2016 fatedier, fatedier@gmail.com
  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 log
  15. import (
  16. "fmt"
  17. "github.com/fatedier/beego/logs"
  18. )
  19. var Log *logs.BeeLogger
  20. func init() {
  21. Log = logs.NewLogger(200)
  22. Log.EnableFuncCallDepth(true)
  23. Log.SetLogFuncCallDepth(Log.GetLogFuncCallDepth() + 1)
  24. }
  25. func InitLog(logWay string, logFile string, logLevel string, maxdays int64) {
  26. SetLogFile(logWay, logFile, maxdays)
  27. SetLogLevel(logLevel)
  28. }
  29. // logWay: file or console
  30. func SetLogFile(logWay string, logFile string, maxdays int64) {
  31. if logWay == "console" {
  32. Log.SetLogger("console", "")
  33. } else {
  34. params := fmt.Sprintf(`{"filename": "%s", "maxdays": %d}`, logFile, maxdays)
  35. Log.SetLogger("file", params)
  36. }
  37. }
  38. // value: error, warning, info, debug, trace
  39. func SetLogLevel(logLevel string) {
  40. level := 4 // warning
  41. switch logLevel {
  42. case "error":
  43. level = 3
  44. case "warn":
  45. level = 4
  46. case "info":
  47. level = 6
  48. case "debug":
  49. level = 7
  50. case "trace":
  51. level = 8
  52. default:
  53. level = 4
  54. }
  55. Log.SetLevel(level)
  56. }
  57. // wrap log
  58. func Error(format string, v ...interface{}) {
  59. Log.Error(format, v...)
  60. }
  61. func Warn(format string, v ...interface{}) {
  62. Log.Warn(format, v...)
  63. }
  64. func Info(format string, v ...interface{}) {
  65. Log.Info(format, v...)
  66. }
  67. func Debug(format string, v ...interface{}) {
  68. Log.Debug(format, v...)
  69. }
  70. func Trace(format string, v ...interface{}) {
  71. Log.Trace(format, v...)
  72. }
  73. // Logger
  74. type Logger interface {
  75. AddLogPrefix(string)
  76. GetPrefixStr() string
  77. GetAllPrefix() []string
  78. ClearLogPrefix()
  79. Error(string, ...interface{})
  80. Warn(string, ...interface{})
  81. Info(string, ...interface{})
  82. Debug(string, ...interface{})
  83. Trace(string, ...interface{})
  84. }
  85. type PrefixLogger struct {
  86. prefix string
  87. allPrefix []string
  88. }
  89. func NewPrefixLogger(prefix string) *PrefixLogger {
  90. logger := &PrefixLogger{
  91. allPrefix: make([]string, 0),
  92. }
  93. logger.AddLogPrefix(prefix)
  94. return logger
  95. }
  96. func (pl *PrefixLogger) AddLogPrefix(prefix string) {
  97. if len(prefix) == 0 {
  98. return
  99. }
  100. pl.prefix += "[" + prefix + "] "
  101. pl.allPrefix = append(pl.allPrefix, prefix)
  102. }
  103. func (pl *PrefixLogger) GetPrefixStr() string {
  104. return pl.prefix
  105. }
  106. func (pl *PrefixLogger) GetAllPrefix() []string {
  107. return pl.allPrefix
  108. }
  109. func (pl *PrefixLogger) ClearLogPrefix() {
  110. pl.prefix = ""
  111. pl.allPrefix = make([]string, 0)
  112. }
  113. func (pl *PrefixLogger) Error(format string, v ...interface{}) {
  114. Log.Error(pl.prefix+format, v...)
  115. }
  116. func (pl *PrefixLogger) Warn(format string, v ...interface{}) {
  117. Log.Warn(pl.prefix+format, v...)
  118. }
  119. func (pl *PrefixLogger) Info(format string, v ...interface{}) {
  120. Log.Info(pl.prefix+format, v...)
  121. }
  122. func (pl *PrefixLogger) Debug(format string, v ...interface{}) {
  123. Log.Debug(pl.prefix+format, v...)
  124. }
  125. func (pl *PrefixLogger) Trace(format string, v ...interface{}) {
  126. Log.Trace(pl.prefix+format, v...)
  127. }