Use of go-logging

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The logging package implements the log-recording infrastructure for Go. Its output format is customizable and supports different log backend, such as syslog, file and memory. You can use multiple back ends, each with a different log level for each back end and logger.

This is an official example:

Package MainImport("OS"    "Github.com/op/go-logging")var Log =Logging.Mustgetlogger ("Example")//Example format string. Everything except the message has a custom color//which is dependent on the log level. Many fields has a custom output//formatting too, eg the time returns the hour down to the Milli second.varFormat=Logging.Muststringformatter ('%{color}%{time:15:04:05.000}%{shortfunc}%{level:.4s}%{id:03x}%{color:reset}%{message} ',)//Password is just an example type implementing the Redactor interface. any//Time This is logged, the redacted () function would be called.typePasswordstringFunc (P Password) redacted () interface{} {returnLogging.Redact (string(p))} Func Main () {//For demo purposes, create the backend for OS. Stderr.Backend1:=Logging.Newlogbackend (OS.Stderr,"",0) Backend2:=Logging.Newlogbackend (OS.Stderr,"",0)//For messages written to Backend2 we want to add some additional    //information to the output, including the used log level and the name of    //the function.Backend2formatter:=Logging.Newbackendformatter (backend2, format)//Only errors and more severe messages should is sent to Backend1Backend1leveled:=Logging.Addmodulelevel (BACKEND1) backend1leveled.SetLevel (Logging.ERROR,"")//Set the backends to be used.Logging.Setbackend (backend1leveled, Backend2formatter)Log.DEBUGF ("Debug%s", Password ("Secret"))Log.Info ("Info")Log.Notice ("notice")Log.Warning ("Warning")Log.Error ("Err")Log.Critical ("Crit")}

Operation Result:

The fabric log system mainly uses third-party package go-logging, and very few use the log in the Go Language standard library. On this basis, the fabric itself encapsulated out the flogging.

In Fabric v1.0, the code is centralized in the fabric/common/flogging directory, and in the fabric v0.6, the code is centrally in the Fabric/flogging directory, and two versions of flogging are available for project global use.

The main functions used are:

1, Mustgetlogger ()

    • function definition
MustGetLogger(module string) *Logger
    • Use to create a logger object for the ACA.
var acaLogger = logging.MustGetLogger("aca")

Mustgetlogger is like GetLogger (GetLogger creates and returns logger objects based on the module name), but if logger cannot be created, an emergency occurs (panics). It simplifies the security initialization of the global logger.

2, Panic ()

    • function definition
func (l *Logger) Panic(args ...interface{})
    • Use
var caLogger = logging.MustGetLogger("ca")...caLogger.Panic(err) // err是调用某个函数的返回值

Panic () is equivalent to L. Critical (FMT. Sprint ()) followed by a call to panic (). Critical () uses Critical as the logging level to log messages.

3. Error (), Errorf ()

    • function definition
func (l *Logger) Error(args ...interface{})funcstring, args ...interface{})
    • Use
var caLogger = logging.MustGetLogger("ca")...caLogger.Error(err)// err是调用某个函数的返回值

Both error () and Errorf () use error as the log level for logging messages.

4, Debug (), DEBUGF ()

    • function definition
func (l *Logger) Debug(args ...interface{})funcstring, args ...interface{})
    • Use
var caLogger = logging.MustGetLogger("ca")...caLogger.Debug("Reading certificate for "".")caLogger.Debugf("readCertificateByKeyUsage() Error: %v", err)

Debug () and DEBUGF () both use debug as the log level for logging messages.

5. Info ()

    • function definition
func (l *Logger) Info(args ...interface{})
    • Use
var caLogger = logging.MustGetLogger("ca")...caLogger.Info("Fresh start; creating databases, key pairs, and certificates.")

Info () logs messages using info as the log level.

For more relevant documentation, see Godoc
Reference article:
http://blog.csdn.net/idsuf698987/article/details/75223986
Https://github.com/op/go-logging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.