yfhk / fabric_log

logs
1 stars 0 forks source link

flogging.MustGetLogger("chaincode") #24

Open yfhk opened 5 years ago

yfhk commented 5 years ago

// Log implements the Backend interface. func (b LogBackend) Log(level Level, calldepth int, rec Record) error { // debug.PrintStack() if b.Color { col := colors[level] if len(b.ColorConfig) > int(level) && b.ColorConfig[level] != "" { col = b.ColorConfig[level] }

    buf := &bytes.Buffer{}
    buf.Write([]byte(col))
    buf.Write([]byte(rec.Formatted(calldepth + 1)))
    buf.Write([]byte("\033[0m"))
    // For some reason, the Go logger arbitrarily decided "2" was the correct
    // call depth...
    // return b.Logger.Output(calldepth+2, buf.String())
    fmt.Println(calldepth+2, buf.String())
    return nil
}

// return b.Logger.Output(calldepth+2, rec.Formatted(calldepth+1))
fmt.Println(calldepth+2, rec.Formatted(calldepth+1))
return nil

}

yfhk commented 5 years ago

"github.com/op/go-logging"

// MustGetLogger is used in place of logging.MustGetLogger to allow us to // store a map of all modules and submodules that have loggers in the system. func MustGetLogger(module string) *logging.Logger { l := logging.MustGetLogger(module) lock.Lock() defer lock.Unlock() modules[module] = GetModuleLevel(module) return l }

yfhk commented 5 years ago

// MustGetLogger is like GetLogger but panics if the logger can't be created. // It simplifies safe initialization of a global logger for eg. a package. func MustGetLogger(module string) *Logger { logger, err := GetLogger(module) if err != nil { panic("logger: " + module + ": " + err.Error()) } return logger }

yfhk commented 5 years ago

func (l Logger) log(lvl Level, format string, args ...interface{}) { // if !l.IsEnabledFor(lvl) { // return // }

// Create the logging record and pass it in to the backend
record := &Record{
    ID:     atomic.AddUint64(&sequenceNo, 1),
    Time:   timeNow(),
    Module: l.Module,
    Level:  lvl,
    fmt:    format,
    Args:   args,
}

// TODO use channels to fan out the records to all backends?
// TODO in case of errors, do something (tricky)

// calldepth=2 brings the stack up to the caller of the level
// methods, Info(), Fatal(), etc.
// ExtraCallDepth allows this to be extended further up the stack in case we
// are wrapping these methods, eg. to expose them package level
if l.haveBackend {
    l.backend.Log(lvl, 2+l.ExtraCalldepth, record)
    return
}

defaultBackend.Log(lvl, 2+l.ExtraCalldepth, record)

}

// Fatal is equivalent to l.Critical(fmt.Sprint()) followed by a call to os.Exit(1). func (l *Logger) Fatal(args ...interface{}) { l.log(CRITICAL, nil, args...) os.Exit(1) }

// Fatalf is equivalent to l.Critical followed by a call to os.Exit(1). func (l *Logger) Fatalf(format string, args ...interface{}) { l.log(CRITICAL, &format, args...) os.Exit(1) }

// Panic is equivalent to l.Critical(fmt.Sprint()) followed by a call to panic(). func (l *Logger) Panic(args ...interface{}) { l.log(CRITICAL, nil, args...) panic(fmt.Sprint(args...)) }

// Panicf is equivalent to l.Critical followed by a call to panic(). func (l *Logger) Panicf(format string, args ...interface{}) { l.log(CRITICAL, &format, args...) panic(fmt.Sprintf(format, args...)) }

// Critical logs a message using CRITICAL as log level. func (l *Logger) Critical(args ...interface{}) { l.log(CRITICAL, nil, args...) }

// Criticalf logs a message using CRITICAL as log level. func (l *Logger) Criticalf(format string, args ...interface{}) { l.log(CRITICAL, &format, args...) }

// Error logs a message using ERROR as log level. func (l *Logger) Error(args ...interface{}) { l.log(ERROR, nil, args...) }

// Errorf logs a message using ERROR as log level. func (l *Logger) Errorf(format string, args ...interface{}) { l.log(ERROR, &format, args...) }

// Warning logs a message using WARNING as log level. func (l *Logger) Warning(args ...interface{}) { l.log(WARNING, nil, args...) }

// Warningf logs a message using WARNING as log level. func (l *Logger) Warningf(format string, args ...interface{}) { l.log(WARNING, &format, args...) }

// Notice logs a message using NOTICE as log level. func (l *Logger) Notice(args ...interface{}) { l.log(NOTICE, nil, args...) }

// Noticef logs a message using NOTICE as log level. func (l *Logger) Noticef(format string, args ...interface{}) { l.log(NOTICE, &format, args...) }

// Info logs a message using INFO as log level. func (l *Logger) Info(args ...interface{}) { l.log(INFO, nil, args...) }

// Infof logs a message using INFO as log level. func (l *Logger) Infof(format string, args ...interface{}) { l.log(INFO, &format, args...) }

// Debug logs a message using DEBUG as log level. func (l *Logger) Debug(args ...interface{}) { l.log(DEBUG, nil, args...) }

// Debugf logs a message using DEBUG as log level. func (l *Logger) Debugf(format string, args ...interface{}) { l.log(DEBUG, &format, args...) }

func init() { Reset() }