log.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "bytes"
  17. "os"
  18. "github.com/fatedier/golib/log"
  19. )
  20. var (
  21. TraceLevel = log.TraceLevel
  22. DebugLevel = log.DebugLevel
  23. InfoLevel = log.InfoLevel
  24. WarnLevel = log.WarnLevel
  25. ErrorLevel = log.ErrorLevel
  26. )
  27. var Logger *log.Logger
  28. func init() {
  29. Logger = log.New(
  30. log.WithCaller(true),
  31. log.AddCallerSkip(1),
  32. log.WithLevel(log.InfoLevel),
  33. )
  34. }
  35. func InitLogger(logPath string, levelStr string, maxDays int, disableLogColor bool) {
  36. options := []log.Option{}
  37. if logPath == "console" {
  38. if !disableLogColor {
  39. options = append(options,
  40. log.WithOutput(log.NewConsoleWriter(log.ConsoleConfig{
  41. Colorful: true,
  42. }, os.Stdout)),
  43. )
  44. }
  45. } else {
  46. writer := log.NewRotateFileWriter(log.RotateFileConfig{
  47. FileName: logPath,
  48. Mode: log.RotateFileModeDaily,
  49. MaxDays: maxDays,
  50. })
  51. writer.Init()
  52. options = append(options, log.WithOutput(writer))
  53. }
  54. level, err := log.ParseLevel(levelStr)
  55. if err != nil {
  56. level = log.InfoLevel
  57. }
  58. options = append(options, log.WithLevel(level))
  59. Logger = Logger.WithOptions(options...)
  60. }
  61. func Errorf(format string, v ...interface{}) {
  62. Logger.Errorf(format, v...)
  63. }
  64. func Warnf(format string, v ...interface{}) {
  65. Logger.Warnf(format, v...)
  66. }
  67. func Infof(format string, v ...interface{}) {
  68. Logger.Infof(format, v...)
  69. }
  70. func Debugf(format string, v ...interface{}) {
  71. Logger.Debugf(format, v...)
  72. }
  73. func Tracef(format string, v ...interface{}) {
  74. Logger.Tracef(format, v...)
  75. }
  76. func Logf(level log.Level, offset int, format string, v ...interface{}) {
  77. Logger.Logf(level, offset, format, v...)
  78. }
  79. type WriteLogger struct {
  80. level log.Level
  81. offset int
  82. }
  83. func NewWriteLogger(level log.Level, offset int) *WriteLogger {
  84. return &WriteLogger{
  85. level: level,
  86. offset: offset,
  87. }
  88. }
  89. func (w *WriteLogger) Write(p []byte) (n int, err error) {
  90. Logger.Log(w.level, w.offset, string(bytes.TrimRight(p, "\n")))
  91. return len(p), nil
  92. }