Skip to content

Commit

Permalink
修复由于使用 fmt 包存在的性能问题
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Mar 7, 2020
1 parent 52beb97 commit 55020fb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
package logit

import (
"fmt"
"io"
"runtime"
"strconv"
"sync"
"time"
)
Expand Down Expand Up @@ -223,15 +223,15 @@ func wrapMessageWithFileInfo(callDepth int, msg string) string {
// 这个 callDepth 是 runtime.Caller 方法的参数,表示上面第几层调用者信息
_, file, line, ok := runtime.Caller(callDepth)
if !ok {
return fmt.Sprintf("[unknown file:unknown line] %s", msg)
return "[unknown file:unknown line] " + msg
}

return fmt.Sprintf("[%s:%d] %s", file, line, msg)
return "[" + file + ":" + strconv.Itoa(line) + "] " + msg
}

// formatMessage returns the formatted message with given args
func formatMessage(msg string, args ...interface{}) string {
return fmt.Sprintf(msg, args...)
return msg
}

// Debug will output msg as a debug message.
Expand Down
3 changes: 1 addition & 2 deletions logger_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package logit

import (
"fmt"
"time"
)

Expand All @@ -40,6 +39,6 @@ func (lh LoggerHandler) handle(logger *Logger, level LoggerLevel, now time.Time,
// The log handled by this handler will be like "[Info] [2020-03-06 16:10:44] msg".
// If you want to customize, just code your own handler, then replace it!
func DefaultLoggerHandler(logger *Logger, level LoggerLevel, now time.Time, msg string) bool {
fmt.Fprintf(logger.Writer(), "[%s] [%s] %s\n", prefixOf(level), now.Format(logger.formatOfTime), msg)
logger.Writer().Write([]byte("[" + prefixOf(level) + "] [" + now.Format(logger.formatOfTime) + "] " + msg + "\n"))
return true
}
4 changes: 2 additions & 2 deletions wrapper/duration_rolling_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package wrapper

import (
"fmt"
"errors"
"os"
"sync"
"time"
Expand Down Expand Up @@ -72,7 +72,7 @@ func NewDurationRollingFile(duration time.Duration, nextFilename func(now time.T

// 防止时间间隔太小导致滚动文件时 IO 的疯狂蠕动
if duration < minDuration {
panic(fmt.Errorf("Duration is smaller than %v!\n", minDuration))
panic(errors.New("Duration is smaller than " + minDuration.String() + "\n"))
}

// 获取当前时间,并生成第一个文件
Expand Down
5 changes: 3 additions & 2 deletions wrapper/size_rolling_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
package wrapper

import (
"fmt"
"errors"
"os"
"strconv"
"sync"
"time"
)
Expand Down Expand Up @@ -85,7 +86,7 @@ func NewSizeRollingFile(limitedSize int64, nextFilename func(now time.Time) stri

// 防止文件限制尺寸太小导致滚动文件时 IO 的疯狂蠕动
if limitedSize < minLimitedSize {
panic(fmt.Errorf("LimitedSize is smaller than %v KB!\n", uint64(minLimitedSize)>>10))
panic(errors.New("LimitedSize is smaller than " + strconv.FormatUint(uint64(minLimitedSize)>>10, 10) + " KB!\n"))
}

// 获取当前时间,并生成第一个文件
Expand Down

0 comments on commit 55020fb

Please sign in to comment.