2016-03-14 11:18:24 +08:00
|
|
|
// Copyright 2016 fatedier, fatedier@gmail.com
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-01-27 21:24:36 +08:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2024-04-11 20:19:08 +08:00
|
|
|
"bytes"
|
2024-03-12 13:58:53 +08:00
|
|
|
"os"
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2024-03-12 13:58:53 +08:00
|
|
|
"github.com/fatedier/golib/log"
|
2016-01-27 21:24:36 +08:00
|
|
|
)
|
|
|
|
|
2024-04-11 20:19:08 +08:00
|
|
|
var (
|
|
|
|
TraceLevel = log.TraceLevel
|
|
|
|
DebugLevel = log.DebugLevel
|
|
|
|
InfoLevel = log.InfoLevel
|
|
|
|
WarnLevel = log.WarnLevel
|
|
|
|
ErrorLevel = log.ErrorLevel
|
|
|
|
)
|
|
|
|
|
2024-03-12 13:58:53 +08:00
|
|
|
var Logger *log.Logger
|
2016-01-27 21:24:36 +08:00
|
|
|
|
|
|
|
func init() {
|
2024-03-12 13:58:53 +08:00
|
|
|
Logger = log.New(
|
|
|
|
log.WithCaller(true),
|
|
|
|
log.AddCallerSkip(1),
|
|
|
|
log.WithLevel(log.InfoLevel),
|
|
|
|
)
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
2024-03-12 13:58:53 +08:00
|
|
|
func InitLogger(logPath string, levelStr string, maxDays int, disableLogColor bool) {
|
|
|
|
options := []log.Option{}
|
|
|
|
if logPath == "console" {
|
|
|
|
if !disableLogColor {
|
|
|
|
options = append(options,
|
|
|
|
log.WithOutput(log.NewConsoleWriter(log.ConsoleConfig{
|
|
|
|
Colorful: true,
|
|
|
|
}, os.Stdout)),
|
|
|
|
)
|
2019-08-12 00:47:35 +08:00
|
|
|
}
|
2016-01-27 21:24:36 +08:00
|
|
|
} else {
|
2024-03-12 13:58:53 +08:00
|
|
|
writer := log.NewRotateFileWriter(log.RotateFileConfig{
|
|
|
|
FileName: logPath,
|
|
|
|
Mode: log.RotateFileModeDaily,
|
|
|
|
MaxDays: maxDays,
|
|
|
|
})
|
|
|
|
writer.Init()
|
|
|
|
options = append(options, log.WithOutput(writer))
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
2024-03-12 13:58:53 +08:00
|
|
|
level, err := log.ParseLevel(levelStr)
|
|
|
|
if err != nil {
|
|
|
|
level = log.InfoLevel
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
2024-03-12 13:58:53 +08:00
|
|
|
options = append(options, log.WithLevel(level))
|
|
|
|
Logger = Logger.WithOptions(options...)
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
2024-03-12 13:58:53 +08:00
|
|
|
func Errorf(format string, v ...interface{}) {
|
|
|
|
Logger.Errorf(format, v...)
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
2024-03-12 13:58:53 +08:00
|
|
|
func Warnf(format string, v ...interface{}) {
|
|
|
|
Logger.Warnf(format, v...)
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
2024-03-12 13:58:53 +08:00
|
|
|
func Infof(format string, v ...interface{}) {
|
|
|
|
Logger.Infof(format, v...)
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
|
|
|
|
2024-03-12 13:58:53 +08:00
|
|
|
func Debugf(format string, v ...interface{}) {
|
|
|
|
Logger.Debugf(format, v...)
|
2016-01-27 21:24:36 +08:00
|
|
|
}
|
2017-03-09 02:03:47 +08:00
|
|
|
|
2024-03-12 13:58:53 +08:00
|
|
|
func Tracef(format string, v ...interface{}) {
|
|
|
|
Logger.Tracef(format, v...)
|
2017-04-25 00:34:14 +08:00
|
|
|
}
|
2024-04-11 20:19:08 +08:00
|
|
|
|
|
|
|
func Logf(level log.Level, offset int, format string, v ...interface{}) {
|
|
|
|
Logger.Logf(level, offset, format, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
type WriteLogger struct {
|
|
|
|
level log.Level
|
|
|
|
offset int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWriteLogger(level log.Level, offset int) *WriteLogger {
|
|
|
|
return &WriteLogger{
|
|
|
|
level: level,
|
|
|
|
offset: offset,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WriteLogger) Write(p []byte) (n int, err error) {
|
|
|
|
Logger.Log(w.level, w.offset, string(bytes.TrimRight(p, "\n")))
|
|
|
|
return len(p), nil
|
|
|
|
}
|