You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.2 KiB
62 lines
1.2 KiB
package log
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/sirupsen/logrus"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
type LoggerItf interface {
|
|
Errorf(format string, args ...interface{})
|
|
Infof(format string, args ...interface{})
|
|
}
|
|
|
|
type LoggerS struct {
|
|
LoggerItf
|
|
}
|
|
|
|
var (
|
|
Logger LoggerS
|
|
enableLogger bool
|
|
)
|
|
|
|
func init() {
|
|
Logger.LoggerItf = logrus.New()
|
|
enableLogger = true
|
|
}
|
|
|
|
// SetLogger todo: replace the log plugin
|
|
func (l *LoggerS) SetLogger(logger LoggerItf) {
|
|
if logger == nil {
|
|
return
|
|
}
|
|
|
|
l.LoggerItf = logger
|
|
}
|
|
|
|
func (l *LoggerS) DisableLogger() {
|
|
enableLogger = false
|
|
}
|
|
|
|
func (l *LoggerS) TraceErrorf(traceId, format string, args ...interface{}) {
|
|
if !enableLogger {
|
|
return
|
|
}
|
|
|
|
_, file, line, _ := runtime.Caller(1)
|
|
split := strings.Split(file, "/")
|
|
file = strings.Join(split[len(split)-2:], "/")
|
|
l.LoggerItf.Errorf(fmt.Sprintf("cloud-trace-id:[%s]:::[%s]:::line[%d]:::%s", traceId, file, line, format), args...)
|
|
}
|
|
|
|
func (l *LoggerS) TraceInfof(traceId, format string, args ...interface{}) {
|
|
if !enableLogger {
|
|
return
|
|
}
|
|
|
|
_, file, line, _ := runtime.Caller(1)
|
|
split := strings.Split(file, "/")
|
|
file = strings.Join(split[len(split)-2:], "/")
|
|
l.LoggerItf.Infof(fmt.Sprintf("cloud-trace-id:[%s]:::[%s]:::line[%d]:::%s", traceId, file, line, format), args...)
|
|
}
|
|
|