Skip to content

Commit

Permalink
modify logger for enable prefix (#97)
Browse files Browse the repository at this point in the history
* modify logger for enable prefix

* fix bug
  • Loading branch information
louishlz authored and defool committed Jan 16, 2019
1 parent 91161a6 commit 447cc0f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
7 changes: 6 additions & 1 deletion tars/remotelogger.go
Expand Up @@ -13,6 +13,7 @@ type RemoteTimeWriter struct {
logPtr *logf.Log
reportSuccPtr *PropertyReport
reportFailPtr *PropertyReport
hasPrefix bool
}

//NewRemoteTimeWriter new and init RemoteTimeWriter
Expand Down Expand Up @@ -135,7 +136,11 @@ func (rw *RemoteTimeWriter) InitFormat(s string) {

//NeedPrefix return if need prefix for the logger.
func (rw *RemoteTimeWriter) NeedPrefix() bool {
return false
return rw.hasPrefix
}

func (rw *RemoteTimeWriter) SetPrefix(enable bool) {
rw.hasPrefix = enable
}

//Write Writes the logs to the buffer.
Expand Down
2 changes: 1 addition & 1 deletion tars/servant.go
Expand Up @@ -71,7 +71,7 @@ func (s *ServantProxy) Tars_invoke(ctx context.Context, ctype byte,
err = s.obj.Invoke(ctx, msg, time.Duration(s.timeout)*time.Millisecond)
}
if err != nil {
TLOG.Error("Invoke error:", s.name, sFuncName, err.Error())
TLOG.Errorf("Invoke Obj:%s,fun:%s,error:%s", s.name, sFuncName, err.Error())
if msg.Resp == nil {
ReportStat(msg, 0, 0, 1)
} else if msg.Status == basef.TARSINVOKETIMEOUT {
Expand Down
2 changes: 1 addition & 1 deletion tars/statf.go
Expand Up @@ -163,7 +163,7 @@ func ReportStatFromClient(msg *Message, succ int32, timeout int32, exec int32) {
head.InterfaceName = msg.Req.SFuncName
sNames := strings.Split(msg.Req.SServantName, ".")
if len(sNames) < 2 {
TLOG.Debug("report err:servant name (%s) format error", msg.Req.SServantName)
TLOG.Debugf("report err:servant name (%s) format error", msg.Req.SServantName)
return
}
head.SlaveName = fmt.Sprintf("%s.%s", sNames[0], sNames[1])
Expand Down
5 changes: 3 additions & 2 deletions tars/util/rogger/logger.go
Expand Up @@ -234,9 +234,10 @@ func (l *Logger) writef(level LogLevel, format string, v []interface{}) {
}
fmt.Fprintf(buf, "%s:%d|", file, line)
}

buf.WriteString(level.String())
buf.WriteByte('|')
}
buf.WriteString(level.String())
buf.WriteByte('|')

if format == "" {
fmt.Fprint(buf, v...)
Expand Down
30 changes: 18 additions & 12 deletions tars/util/rogger/logwriter.go
Expand Up @@ -37,13 +37,14 @@ type RollFileWriter struct {

//DateWriter rotate logs by date.
type DateWriter struct {
logpath string
name string
dateType DateType
num int
currDate string
currFile *os.File
openTime int64
logpath string
name string
dateType DateType
num int
currDate string
currFile *os.File
openTime int64
hasPrefix bool
}

//HourWriter for rotate logs by hour
Expand Down Expand Up @@ -147,16 +148,21 @@ func (w *DateWriter) Write(v []byte) {

//NeedPrefix shows whether needs prefix info for DateWriter or not.
func (w *DateWriter) NeedPrefix() bool {
return true
return w.hasPrefix
}

func (w *DateWriter) SetPrefix(enable bool) {
w.hasPrefix = enable
}

//NewDateWriter returns a writer which keeps logs in hours or day format.
func NewDateWriter(logpath, name string, dateType DateType, num int) *DateWriter {
w := &DateWriter{
logpath: logpath,
name: name,
num: num,
dateType: dateType,
logpath: logpath,
name: name,
num: num,
dateType: dateType,
hasPrefix: true,
}
w.currDate = w.getCurrDate()
return w
Expand Down

0 comments on commit 447cc0f

Please sign in to comment.