diff --git a/zlog/file.go b/zlog/file.go index 2992e8e..22d7c5a 100644 --- a/zlog/file.go +++ b/zlog/file.go @@ -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 } diff --git a/zlog/zlogger.go b/zlog/zlogger.go index 3606286..1d9cfa3 100644 --- a/zlog/zlogger.go +++ b/zlog/zlogger.go @@ -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 { @@ -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 @@ -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') @@ -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) } @@ -311,68 +303,107 @@ 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) @@ -380,6 +411,9 @@ func (log *Logger) Panicf(format string, v ...interface{}) { // 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) @@ -387,6 +421,9 @@ func (log *Logger) Panic(v ...interface{}) { // Stack output Stack func (log *Logger) Stack(v interface{}) { + if log.level < LogTrack { + return + } var s string switch e := v.(type) { case error: @@ -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 { @@ -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 { @@ -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 +} diff --git a/zlog/zlogger_test.go b/zlog/zlogger_test.go new file mode 100644 index 0000000..c5228b1 --- /dev/null +++ b/zlog/zlogger_test.go @@ -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") +} diff --git a/znet/router.go b/znet/router.go index e09c906..b68edcc 100644 --- a/znet/router.go +++ b/znet/router.go @@ -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() } diff --git a/ztype/map.go b/ztype/map.go index c66386b..e75a295 100644 --- a/ztype/map.go +++ b/ztype/map.go @@ -165,7 +165,7 @@ func ToMaps(value interface{}) Maps { } } -func toMapString(value interface{}, tags ...string) map[string]interface{} { +func toMapString(value interface{}) map[string]interface{} { if value == nil { return map[string]interface{}{} } @@ -223,62 +223,66 @@ func toMapString(value interface{}, tags ...string) map[string]interface{} { m[ToString(k)] = v } default: - rv := zreflect.ValueOf(val) - kind := rv.Kind() - if kind == reflect.Ptr { - rv = rv.Elem() - kind = rv.Kind() + toMapStringReflect(&m, value) + } + return m +} + +func toMapStringReflect(m *map[string]interface{}, val interface{}) { + rv := zreflect.ValueOf(val) + kind := rv.Kind() + if kind == reflect.Ptr { + rv = rv.Elem() + kind = rv.Kind() + } + switch kind { + case reflect.Map: + ks := rv.MapKeys() + for _, k := range ks { + (*m)[ToString(k.Interface())] = rv.MapIndex(k).Interface() } - switch kind { - case reflect.Map: - ks := rv.MapKeys() - for _, k := range ks { - m[ToString(k.Interface())] = rv.MapIndex(k).Interface() + case reflect.Struct: + rt := rv.Type() + ol: + for i := 0; i < rv.NumField(); i++ { + field := rt.Field(i) + fieldName := field.Name + if !zstring.IsUcfirst(fieldName) { + continue } - case reflect.Struct: - rt := rv.Type() - ol: - for i := 0; i < rv.NumField(); i++ { - field := rt.Field(i) - fieldName := field.Name - if !zstring.IsUcfirst(fieldName) { - continue - } - name, opt := zreflect.GetStructTag(field, tagName, tagNameLesser) - if name == "" { - continue - } - array := strings.Split(opt, ",") - v := rv.Field(i) - for i := range array { - switch strings.TrimSpace(array[i]) { - case "omitempty": - if IsEmpty(v.Interface()) { - continue ol - } + name, opt := zreflect.GetStructTag(field, tagName, tagNameLesser) + if name == "" { + continue + } + array := strings.Split(opt, ",") + v := rv.Field(i) + for i := range array { + switch strings.TrimSpace(array[i]) { + case "omitempty": + if IsEmpty(v.Interface()) { + continue ol } } - fv := reflect.Indirect(v) - switch fv.Kind() { - case reflect.Struct: - m[name] = toMapString(v.Interface()) - continue - case reflect.Slice: - if field.Type.Elem().Kind() == reflect.Struct { - mc := make([]map[string]interface{}, v.Len()) - for i := 0; i < v.Len(); i++ { - mc[i] = toMapString(v.Index(i).Interface()) - } - m[name] = mc - continue + } + fv := reflect.Indirect(v) + switch fv.Kind() { + case reflect.Struct: + (*m)[name] = toMapString(v.Interface()) + continue + case reflect.Slice: + if field.Type.Elem().Kind() == reflect.Struct { + mc := make([]map[string]interface{}, v.Len()) + for i := 0; i < v.Len(); i++ { + mc[i] = toMapString(v.Index(i).Interface()) } + (*m)[name] = mc + continue } - m[name] = v.Interface() } - default: - m["0"] = val + (*m)[name] = v.Interface() } + default: + (*m)["0"] = val } - return m } diff --git a/ztype/to.go b/ztype/to.go index efa5460..a811563 100644 --- a/ztype/to.go +++ b/ztype/to.go @@ -66,12 +66,16 @@ func ToString(i interface{}) string { if f, ok := value.(appString); ok { return f.String() } - jsonContent, _ := json.Marshal(value) - jsonContent = bytes.Trim(jsonContent, `"`) - return zstring.Bytes2String(jsonContent) + return toJsonString(value) } } +func toJsonString(value interface{}) string { + jsonContent, _ := json.Marshal(value) + jsonContent = bytes.Trim(jsonContent, `"`) + return zstring.Bytes2String(jsonContent) +} + // ToBool To Bool func ToBool(i interface{}) bool { if v, ok := i.(bool); ok {