log.go 2.1 KB

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