log.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // Log is the under log object
  20. var Log *logs.BeeLogger
  21. func init() {
  22. Log = logs.NewLogger(200)
  23. Log.EnableFuncCallDepth(true)
  24. Log.SetLogFuncCallDepth(Log.GetLogFuncCallDepth() + 1)
  25. }
  26. func InitLog(logWay string, logFile string, logLevel string, maxdays int64, disableLogColor bool) {
  27. SetLogFile(logWay, logFile, maxdays, disableLogColor)
  28. SetLogLevel(logLevel)
  29. }
  30. // SetLogFile to configure log params
  31. // logWay: file or console
  32. func SetLogFile(logWay string, logFile string, maxdays int64, disableLogColor bool) {
  33. if logWay == "console" {
  34. params := ""
  35. if disableLogColor {
  36. params = fmt.Sprintf(`{"color": false}`)
  37. }
  38. Log.SetLogger("console", params)
  39. } else {
  40. params := fmt.Sprintf(`{"filename": "%s", "maxdays": %d}`, logFile, maxdays)
  41. Log.SetLogger("file", params)
  42. }
  43. }
  44. // SetLogLevel set log level, default is warning
  45. // value: error, warning, info, debug, trace
  46. func SetLogLevel(logLevel string) {
  47. level := 4 // warning
  48. switch logLevel {
  49. case "error":
  50. level = 3
  51. case "warn":
  52. level = 4
  53. case "info":
  54. level = 6
  55. case "debug":
  56. level = 7
  57. case "trace":
  58. level = 8
  59. default:
  60. level = 4
  61. }
  62. Log.SetLevel(level)
  63. }
  64. // wrap log
  65. func Error(format string, v ...interface{}) {
  66. Log.Error(format, v...)
  67. }
  68. func Warn(format string, v ...interface{}) {
  69. Log.Warn(format, v...)
  70. }
  71. func Info(format string, v ...interface{}) {
  72. Log.Info(format, v...)
  73. }
  74. func Debug(format string, v ...interface{}) {
  75. Log.Debug(format, v...)
  76. }
  77. func Trace(format string, v ...interface{}) {
  78. Log.Trace(format, v...)
  79. }