log, swarm/api: introduce log.Output, so that we have correct line numbers in logs

This commit is contained in:
Anton Evangelatov 2018-02-09 18:15:17 +01:00
parent e55559ee45
commit 6f686cb9ea
3 changed files with 21 additions and 15 deletions

View file

@ -126,13 +126,13 @@ type logger struct {
h *swapHandler h *swapHandler
} }
func (l *logger) write(msg string, lvl Lvl, ctx []interface{}) { func (l *logger) write(msg string, lvl Lvl, ctx []interface{}, skip int) {
l.h.Log(&Record{ l.h.Log(&Record{
Time: time.Now(), Time: time.Now(),
Lvl: lvl, Lvl: lvl,
Msg: msg, Msg: msg,
Ctx: newContext(l.ctx, ctx), Ctx: newContext(l.ctx, ctx),
Call: stack.Caller(2), Call: stack.Caller(skip),
KeyNames: RecordKeyNames{ KeyNames: RecordKeyNames{
Time: timeKey, Time: timeKey,
Msg: msgKey, Msg: msgKey,
@ -156,27 +156,27 @@ func newContext(prefix []interface{}, suffix []interface{}) []interface{} {
} }
func (l *logger) Trace(msg string, ctx ...interface{}) { func (l *logger) Trace(msg string, ctx ...interface{}) {
l.write(msg, LvlTrace, ctx) l.write(msg, LvlTrace, ctx, 2)
} }
func (l *logger) Debug(msg string, ctx ...interface{}) { func (l *logger) Debug(msg string, ctx ...interface{}) {
l.write(msg, LvlDebug, ctx) l.write(msg, LvlDebug, ctx, 2)
} }
func (l *logger) Info(msg string, ctx ...interface{}) { func (l *logger) Info(msg string, ctx ...interface{}) {
l.write(msg, LvlInfo, ctx) l.write(msg, LvlInfo, ctx, 2)
} }
func (l *logger) Warn(msg string, ctx ...interface{}) { func (l *logger) Warn(msg string, ctx ...interface{}) {
l.write(msg, LvlWarn, ctx) l.write(msg, LvlWarn, ctx, 2)
} }
func (l *logger) Error(msg string, ctx ...interface{}) { func (l *logger) Error(msg string, ctx ...interface{}) {
l.write(msg, LvlError, ctx) l.write(msg, LvlError, ctx, 2)
} }
func (l *logger) Crit(msg string, ctx ...interface{}) { func (l *logger) Crit(msg string, ctx ...interface{}) {
l.write(msg, LvlCrit, ctx) l.write(msg, LvlCrit, ctx, 2)
os.Exit(1) os.Exit(1)
} }

View file

@ -31,31 +31,36 @@ func Root() Logger {
// Trace is a convenient alias for Root().Trace // Trace is a convenient alias for Root().Trace
func Trace(msg string, ctx ...interface{}) { func Trace(msg string, ctx ...interface{}) {
root.write(msg, LvlTrace, ctx) root.write(msg, LvlTrace, ctx, 2)
} }
// Debug is a convenient alias for Root().Debug // Debug is a convenient alias for Root().Debug
func Debug(msg string, ctx ...interface{}) { func Debug(msg string, ctx ...interface{}) {
root.write(msg, LvlDebug, ctx) root.write(msg, LvlDebug, ctx, 2)
} }
// Info is a convenient alias for Root().Info // Info is a convenient alias for Root().Info
func Info(msg string, ctx ...interface{}) { func Info(msg string, ctx ...interface{}) {
root.write(msg, LvlInfo, ctx) root.write(msg, LvlInfo, ctx, 2)
} }
// Warn is a convenient alias for Root().Warn // Warn is a convenient alias for Root().Warn
func Warn(msg string, ctx ...interface{}) { func Warn(msg string, ctx ...interface{}) {
root.write(msg, LvlWarn, ctx) root.write(msg, LvlWarn, ctx, 2)
} }
// Error is a convenient alias for Root().Error // Error is a convenient alias for Root().Error
func Error(msg string, ctx ...interface{}) { func Error(msg string, ctx ...interface{}) {
root.write(msg, LvlError, ctx) root.write(msg, LvlError, ctx, 2)
} }
// Crit is a convenient alias for Root().Crit // Crit is a convenient alias for Root().Crit
func Crit(msg string, ctx ...interface{}) { func Crit(msg string, ctx ...interface{}) {
root.write(msg, LvlCrit, ctx) root.write(msg, LvlCrit, ctx, 2)
os.Exit(1) os.Exit(1)
} }
// Output is a convenient alias for write
func Output(msg string, lvl Lvl, skip int, ctx ...interface{}) {
root.write(msg, lvl, ctx, skip)
}

View file

@ -110,7 +110,8 @@ func ShowMultipleChoices(w http.ResponseWriter, r *http.Request, list api.Manife
//(and return the correct HTTP status code) //(and return the correct HTTP status code)
func ShowError(w http.ResponseWriter, r *http.Request, msg string, code int) { func ShowError(w http.ResponseWriter, r *http.Request, msg string, code int) {
if code == http.StatusInternalServerError { if code == http.StatusInternalServerError {
log.Error(msg) //log.Error(msg)
log.Output(msg, log.LvlError, 3)
} }
respond(w, r, &ErrorParams{ respond(w, r, &ErrorParams{
Code: code, Code: code,