Skip to content

Commit

Permalink
🕜 perf: Update logs and performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
sohaha committed Apr 1, 2024
1 parent 048484e commit 4f570b2
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 78 deletions.
2 changes: 1 addition & 1 deletion zlog/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (log *Logger) setLogfile(filepath string, archive bool) {
log.fileDir = fileDir
log.fileName = fileName
if log.fileAndStdout {
log.out = io.MultiWriter(log.file, os.Stdout)
log.out = io.MultiWriter(log.file, log.out)
} else {
log.out = fileObj
}
Expand Down
106 changes: 81 additions & 25 deletions zlog/zlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@ func (log *Logger) OpTextWrap(color Op, text string) string {
}

func (log *Logger) formatHeader(buf *bytes.Buffer, t time.Time, file string, line int, level int) {
if level == LogNot {
return
}

if log.flag&(BitDate|BitTime|BitMicroSeconds|BitLevel) != 0 {

if log.flag&BitDate != 0 {
Expand Down Expand Up @@ -204,10 +200,6 @@ func (log *Logger) formatHeader(buf *bytes.Buffer, t time.Time, file string, lin
buf.WriteString(": ")
}
}

if log.prefix != "" {
buf.WriteString(log.prefix)
}
}

// outPut Output log
Expand Down Expand Up @@ -242,28 +234,22 @@ func (log *Logger) outPut(level int, s string, isWrap bool, fn func() func(),
return nil
}

isNotLevel := level == LogNot
if log.level < level {
return nil
}

if len(prefixText) > 0 {
s = prefixText[0] + s
}
now := time.Now()
var file string
var line int
if !isNotLevel && (log.flag&(BitShortFile|BitLongFile) != 0) {
var ok bool
_, file, line, ok = runtime.Caller(log.calldDepth)
if !ok {
file = "unknown-file"
line = 0
}
}

log.buf.Reset()
log.formatHeader(&log.buf, now, file, line, level)

now := ztime.Time()
if level != LogNot {
file, line := log.fileLocation(level)
log.formatHeader(&log.buf, now, file, line, level)
}

if log.prefix != "" {
log.buf.WriteString(log.prefix)
}

log.buf.WriteString(s)
if isWrap && len(s) > 0 && s[len(s)-1] != '\n' {
log.buf.WriteByte('\n')
Expand All @@ -284,11 +270,17 @@ func (log *Logger) Println(v ...interface{}) {

// Debugf Debugf
func (log *Logger) Debugf(format string, v ...interface{}) {
if log.level < LogDebug {
return
}
_ = log.outPut(LogDebug, fmt.Sprintf(format, v...), false, nil)
}

// Debug Debug
func (log *Logger) Debug(v ...interface{}) {
if log.level < LogDebug {
return
}
_ = log.outPut(LogDebug, fmt.Sprintln(v...), true, nil)
}

Expand All @@ -311,82 +303,127 @@ func (log *Logger) Dump(v ...interface{}) {

// Successf output Success
func (log *Logger) Successf(format string, v ...interface{}) {
if log.level < LogSuccess {
return
}
_ = log.outPut(LogSuccess, fmt.Sprintf(format, v...), false, nil)
}

// Success output Success
func (log *Logger) Success(v ...interface{}) {
if log.level < LogSuccess {
return
}
_ = log.outPut(LogSuccess, fmt.Sprintln(v...), true, nil)
}

// Infof output Info
func (log *Logger) Infof(format string, v ...interface{}) {
if log.level < LogInfo {
return
}
_ = log.outPut(LogInfo, fmt.Sprintf(format, v...), false, nil)
}

// Info output Info
func (log *Logger) Info(v ...interface{}) {
if log.level < LogInfo {
return
}
_ = log.outPut(LogInfo, fmt.Sprintln(v...), true, nil)
}

// Tipsf output Tips
func (log *Logger) Tipsf(format string, v ...interface{}) {
if log.level < LogTips {
return
}
_ = log.outPut(LogTips, fmt.Sprintf(format, v...), false, nil)
}

// Tips output Tips
func (log *Logger) Tips(v ...interface{}) {
if log.level < LogTips {
return
}
_ = log.outPut(LogTips, fmt.Sprintln(v...), true, nil)
}

// Warnf output Warn
func (log *Logger) Warnf(format string, v ...interface{}) {
if log.level < LogWarn {
return
}
_ = log.outPut(LogWarn, fmt.Sprintf(format, v...), false, nil)
}

// Warn output Warn
func (log *Logger) Warn(v ...interface{}) {
if log.level < LogWarn {
return
}
_ = log.outPut(LogWarn, fmt.Sprintln(v...), true, nil)
}

// Errorf output Error
func (log *Logger) Errorf(format string, v ...interface{}) {
if log.level < LogError {
return
}
_ = log.outPut(LogError, fmt.Sprintf(format, v...), false, nil)
}

// Error output Error
func (log *Logger) Error(v ...interface{}) {
if log.level < LogError {
return
}
_ = log.outPut(LogError, fmt.Sprintln(v...), true, nil)
}

// Fatalf output Fatal
func (log *Logger) Fatalf(format string, v ...interface{}) {
if log.level < LogFatal {
return
}
_ = log.outPut(LogFatal, fmt.Sprintf(format, v...), false, nil)
osExit(1)
}

// Fatal output Fatal
func (log *Logger) Fatal(v ...interface{}) {
if log.level < LogFatal {
return
}
_ = log.outPut(LogFatal, fmt.Sprintln(v...), true, nil)
osExit(1)
}

// Panicf output Panic
func (log *Logger) Panicf(format string, v ...interface{}) {
if log.level < LogPanic {
return
}
s := fmt.Sprintf(format, v...)
_ = log.outPut(LogPanic, fmt.Sprintf(format, s), false, nil)
panic(s)
}

// Panic output panic
func (log *Logger) Panic(v ...interface{}) {
if log.level < LogPanic {
return
}
s := fmt.Sprintln(v...)
_ = log.outPut(LogPanic, s, true, nil)
panic(s)
}

// Stack output Stack
func (log *Logger) Stack(v interface{}) {
if log.level < LogTrack {
return
}
var s string
switch e := v.(type) {
case error:
Expand All @@ -401,6 +438,9 @@ func (log *Logger) Stack(v interface{}) {

// Track output Track
func (log *Logger) Track(v string, i ...int) {
if log.level < LogTrack {
return
}
b, skip, max, index := zutil.GetBuff(), 4, 1, 1
il := len(i)
if il > 0 {
Expand Down Expand Up @@ -514,6 +554,10 @@ func itoa(buf *bytes.Buffer, i int, wid int) {
}
}

func (log *Logger) ResetWriter(w io.Writer) {
log.out = w
}

func formatArgs(args ...interface{}) []interface{} {
formatted := make([]interface{}, 0, len(args))
for _, a := range args {
Expand Down Expand Up @@ -558,3 +602,15 @@ func prependArgName(names []string, values []interface{}) []interface{} {
}
return prepended
}

func (log *Logger) fileLocation(level int) (file string, line int) {
if log.flag&(BitShortFile|BitLongFile) != 0 {
var ok bool
_, file, line, ok = runtime.Caller(log.calldDepth)
if !ok {
file = "unknown-file"
line = 0
}
}
return
}
36 changes: 36 additions & 0 deletions zlog/zlogger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package zlog_test

import (
"fmt"
"io"
"os"
"testing"

"github.com/sohaha/zlsgo/zlog"
)

type Report struct {
Writer io.Writer
}

func (w *Report) Write(p []byte) (n int, err error) {
// Here you can initiate an HTTP request to report an error
fmt.Println("Report: ", string(p))

return w.Writer.Write(p)
}

func TestCustomWriter(t *testing.T) {
w := &Report{
Writer: os.Stderr,
}

l1 := zlog.NewZLog(w, "Custom1", zlog.BitDefault|zlog.BitLongFile, zlog.LogDump, true, 2)
l1.Info("Test")

// or

l2 := zlog.New("Custom2")
l2.ResetWriter(w)
l2.Info("Test")
}
1 change: 1 addition & 0 deletions znet/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (e *Engine) StaticFS(relativePath string, fs http.FileSystem, moreHandler .
}
e.GET(urlPattern, handler, moreHandler...)
e.HEAD(urlPattern, handler, moreHandler...)
e.OPTIONS(urlPattern, handler, moreHandler...)
log()
}

Expand Down
Loading

0 comments on commit 4f570b2

Please sign in to comment.