go>> logo>> 返回
项目作者: mbndr

项目描述 :
Golang logger to different configurable writers.
高级语言: Go
项目地址: git://github.com/mbndr/logo.git
创建时间: 2017-02-07T18:02:55Z
项目社区:https://github.com/mbndr/logo

开源协议:MIT License

下载


Logo

A simple logging library for golang.

banner

I wanted to have a logger that is easy configurable with various io.Writers and a simple active and color on / off switch, so I created it.

Build Status
Go Report Card
Coverage
cover.run go
GoDoc
MIT License

Explanation

A Logger object can hold multiple Receivers. Every Receiver holds an io.Writer object (f.e. os.File, os.Stderr), a Level which is minimal the log level that is logged, a boolean Active which says if the Receiver should log or not, and a boolean Color which prints output colored if turned on. Every Receiver has also its own log format.

Initilization

First you have to install the package.

  1. go get -u github.com/mbndr/logo

After that you can import it.

  1. import "github.com/mbndr/logo"

Simple

It’s possible to create a simple logger with a io.Writer, the log level and a color boolean as parameter.

  1. // Create a simple cli logger with activated colors which logs everything
  2. log := logo.NewSimpleLogger(os.Stderr, logo.DEBUG, "prefix ", true)

Advanced

You can create multiple Receivers and add it to a new Logger.

  1. // Receiver for the terminal which logs everything
  2. cliRec := logo.NewReceiver(os.Stderr, "prefix ")
  3. cliRec.Color = true
  4. cliRec.Level = logo.DEBUG
  5. // Helper function to get a os.File with the correct options
  6. logFile, _ := logo.Open("./example/logo.log")
  7. // Receiver for the log file
  8. // This will log with level INFO (default) and have no colors activated
  9. // Also the log format is simpler (f.e. ERRO: Message)
  10. fileRec := logo.NewReceiver(logFile, "prefix ")
  11. fileRec.Format = "%s: %s"
  12. // Create the logger
  13. log := logo.NewLogger(cliRec, fileRec)

Usage

If you created you logo.Logger object, there are a few methods you can use to log.

  1. // Methods which write like log.Println()
  2. log.Debug("First debug", " and another string to log")
  3. log.Info("Information")
  4. log.Warn("Warning", " message")
  5. log.Error("Error message")
  6. log.Fatal("Fatal error", " because of something")
  7. // Methods which write like log.Printf()
  8. log.Debugf("Debug value %d", 16)
  9. log.Infof("Listening on port %d", 8080)
  10. log.Warnf("Invalid user %s", user.Name)
  11. log.Errorf("Couldn't load config file: %s", path)
  12. log.Fatalf("Fatal error: %s", err.Error())
  13. // Disable the logger
  14. log.Active = false

Tests

There are a few unit tests written for this library. To run them cd into the project directory and run this.

  1. go test -v