diff --git a/type.go b/type.go index fafeecfe..bd316000 100644 --- a/type.go +++ b/type.go @@ -808,6 +808,9 @@ func (m *Message) Logs(level string, arg ...interface{}) *Message { return m } func (m *Message) Log(level string, str string, arg ...interface{}) *Message { + return m.log(level, str, arg...) +} +func (m *Message) log(level string, str string, arg ...interface{}) *Message { if str = strings.TrimSpace(fmt.Sprintf(str, arg...)); Log != nil { // 日志模块 Log(m, level, str) @@ -830,34 +833,42 @@ func (m *Message) Log(level string, str string, arg ...interface{}) *Message { prefix, suffix = "\033[31m", "\033[0m" } + _, file, line, _ := runtime.Caller(2) + ls := strings.Split(file, "/") + if len(ls) > 2 { + ls = ls[len(ls)-2:] + } + if os.Getenv("ctx_mod") != "" && m != nil { // 输出日志 - fmt.Fprintf(os.Stderr, "%s %02d %9s %s%s %s%s\n", + fmt.Fprintf(os.Stderr, "%s %02d %9s %s%s %s%s %s\n", m.time.Format(ICE_TIME), m.code, fmt.Sprintf("%4s->%-4s", m.source.Name, m.target.Name), - prefix, level, str, suffix) + prefix, level, str, suffix, + fmt.Sprintf("%s:%d", strings.Join(ls, "/"), line), + ) } return m } func (m *Message) Cost(str string, arg ...interface{}) *Message { - return m.Log(LOG_COST, "%s: %s", m.Format("cost"), kit.Format(str, arg...)) + return m.log(LOG_COST, "%s: %s", m.Format("cost"), kit.Format(str, arg...)) } func (m *Message) Info(str string, arg ...interface{}) *Message { - return m.Log(LOG_INFO, str, arg...) + return m.log(LOG_INFO, str, arg...) } func (m *Message) Warn(err bool, str string, arg ...interface{}) bool { if err { _, file, line, _ := runtime.Caller(1) m.Echo("warn: ").Echo(str, arg...) - return m.Log(LOG_WARN, "%s:%d %s", file, line, fmt.Sprintf(str, arg...)) != nil + return m.log(LOG_WARN, "%s:%d %s", file, line, fmt.Sprintf(str, arg...)) != nil } return false } func (m *Message) Error(err bool, str string, arg ...interface{}) bool { if err { m.Echo("error: ").Echo(str, arg...) - m.Log(LOG_ERROR, m.Format("stack")) - m.Log(LOG_ERROR, str, arg...) - m.Log(LOG_ERROR, m.Format("chain")) + m.log(LOG_ERROR, m.Format("stack")) + m.log(LOG_ERROR, str, arg...) + m.log(LOG_ERROR, m.Format("chain")) return true } return false @@ -865,7 +876,7 @@ func (m *Message) Error(err bool, str string, arg ...interface{}) bool { func (m *Message) Trace(key string, str string, arg ...interface{}) *Message { if m.Options(key) { m.Echo("trace: ").Echo(str, arg...) - return m.Log(LOG_TRACE, str, arg...) + return m.log(LOG_TRACE, str, arg...) } return m }