gol

package module
v0.0.0-...-b3641da Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2018 License: MIT Imports: 11 Imported by: 0

README

gol: a high performance async log kit for golang

Golang Build Status Coverage Status codebeat badge Go Report Card GoDoc license

gol is a high performance async log infrastructure for golang, which include several useful log backend adapters, include file/file rotate/stmp/slack/elasticsearch etc...

Introduce

Level

gol support various log levels, you can set the logger's level to disable some lower level output

const (
    ALL LogLevel = iota
    DEBUG
    INFO
    WARN
    ERROR
    CRITICAL
)
Built in adapters

gol has several built in adapters

  • Console adapter support write log to stderr, and this is the default adapter
  • File adapter support write log to file
  • File rotate adapter support write log to rotate files
  • Smtp adapter support write log to email
  • Slack adapter support write log to given slack channel
  • ES adapter support write log to elastic search (under development)
Customize backend adapters

You can create any backend adapter which implement the Adapter interface.

Actually Adapter is a alias of io.Writer

type Adapter interface {
    io.WriteCloser
}
Color

gol also include a colorful output

Colorful output

Usage

Log to console
import (
    "github.com/philchia/gol"
    "runtime"
)

defer gol.Flush()
gol.Debug("Hello, gol!!!")
gol.Criticalf("Hello from %s", runtime.GOOS)

Not log to console
import (
    "github.com/philchia/gol"
    "runtime"
)
gol.RemoveAdapter(gol.CONSOLELOGGER)
Log to file
import (
    "github.com/philchia/gol"
    "runtime"
)

defer gol.Flush()
gol.AddLogAdapter("file", file.NewAdapter("/var/log/tmp.log"))
gol.Debug("Hello, gol!!!")
gol.Criticalf("Hello from %s", runtime.GOOS)

Rotate log to file
import (
    "github.com/philchia/gol"
    "runtime"
)

defer gol.Flush()
gol.AddLogAdapter("rotate file", rotatefile.NewAdapter("./temp.log", 6, rotatefile.KB*1))
gol.Debug("Hello, gol!!!")
gol.Criticalf("Hello from %s", runtime.GOOS)

Set level
import (
    "github.com/philchia/gol"
    "runtime"
)

defer gol.Flush()
gol.SetLevel(gol.ERROR)
gol.Debug("Hello, gol!!!") // this will not print
gol.Criticalf("Hello from %s", runtime.GOOS)

Set options
import (
    "github.com/philchia/gol"
    "runtime"
)

defer gol.Flush()
gol.SetOption(gol.Llongfile | gol.Ldate | gol.Ltime | gol.Lmicroseconds)
gol.Debug("Hello, gol!!!")
gol.Criticalf("Hello from %s", runtime.GOOS)

Add adapters

You can implement you own custom adapters which implement the Adapter interface.

import (
    "github.com/philchia/gol"
    "runtime"
)

defer gol.Flush()
gol.SetOption(gol.Llongfile | gol.Ldate | gol.Ltime | gol.Lmicroseconds)
gol.AddLogAdapter("anonymous", a)
gol.Debug("Hello, gol!!!")
gol.Criticalf("Hello from %s", runtime.GOOS)

Installation

$go get github.com/philchia/gol

or you can use go get -u to update the package

Documentation

For docs, see Documentation or run:

$godoc github.com/philchia/gol

Benchmark

gol include a benchmark against the builtin log package, run $go test ./... -bench . -benchmem in your terminal to run the bench

Benchmark

Features

  • Log level support
  • Customizable log option support
  • Async write
  • Colorful output
  • Flush buffered log
  • Toggle console adapter
  • Logrotate
  • Mail adapter
  • Slack adapter
  • Level support for single adapter
  • Elastic Search adapter for ELK stack
  • 100% coverage
  • Customizable msg buffer size

License

gol code is published under the MIT license

Documentation

Overview

Package gol is a high performance go log tool

Index

Constants

View Source
const CONSOLELOGGER = "console"

CONSOLELOGGER represent the given console adapter name

Variables

This section is empty.

Functions

func AddLogAdapter

func AddLogAdapter(name string, adapter adapter.Adapter) error

AddLogAdapter add a log adapter which implement the adapter.Adapter interface with give name key, return error if name already exists

func Critical

func Critical(i ...interface{})

Critical will prinnt log as CRITICAL level

func Criticalf

func Criticalf(format string, i ...interface{})

Criticalf will prinnt log as CRITICAL level

func Debug

func Debug(i ...interface{})

Debug will prinnt log as DEBUG level

func Debugf

func Debugf(format string, i ...interface{})

Debugf will prinnt log as DEBUG level

func Error

func Error(i ...interface{})

Error will prinnt log as ERROR level

func Errorf

func Errorf(format string, i ...interface{})

Errorf will prinnt log as ERROR level

func Flush

func Flush()

Flush flush all buffered log and call Close() on all adapters

func Info

func Info(i ...interface{})

Info will prinnt log as INFO level

func Infof

func Infof(format string, i ...interface{})

Infof will prinnt log as INFO level

func RemoveAdapter

func RemoveAdapter(name string) error

RemoveAdapter remove a log adapter with give name key, return error in name not exists

func SetLevel

func SetLevel(level level.LogLevel)

SetLevel set the shared logger's log level

func SetOption

func SetOption(option LogOption)

SetOption set the shared logger's log options used to format log headerr

func Warn

func Warn(i ...interface{})

Warn will prinnt log as WARN level

func Warnf

func Warnf(format string, i ...interface{})

Warnf will prinnt log as WARN level

Types

type LogOption

type LogOption int8

LogOption define which text to prefix to each log entry generated by the Logger.

const (
	// Ldate print date
	// Bits or'ed together to control what's printed.
	// There is no control over the order they appear (the order listed
	// here) or the format they present (as described in the comments).
	// The prefix is followed by a colon only when Llongfile or Lshortfile
	// is specified.
	// For example, flags Ldate | Ltime (or LstdFlags) produce,
	//	2009/01/23 01:23:23 message
	// while flags Ldate | Ltime | Lmicroseconds | Llongfile produce,
	//	2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message
	Ldate LogOption = 1 << iota // the date in the local time zone: 2009/01/23
	// Ltime print time
	Ltime // the time in the local time zone: 01:23:23
	// Lmicroseconds print microsecond
	Lmicroseconds // microsecond resolution: 01:23:23.123123.  assumes Ltime.
	// Llongfile print long format file name
	Llongfile // full file name and line number: /a/b/c/d.go:23
	// Lshortfile print short format file name
	Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
	// LUTC print time as UTC format
	LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
	// LstdFlags is the default header format
	LstdFlags = Ldate | Ltime | Lshortfile // initial values for the standard logger
)

type Logger

type Logger interface {
	Debug(i ...interface{})
	Debugf(format string, i ...interface{})

	Info(i ...interface{})
	Infof(format string, i ...interface{})

	Warn(i ...interface{})
	Warnf(format string, i ...interface{})

	Error(i ...interface{})
	Errorf(format string, i ...interface{})

	Critical(i ...interface{})
	Criticalf(format string, i ...interface{})

	SetLevel(level.LogLevel)
	SetOption(LogOption)

	AddLogAdapter(name string, adapter adapter.Adapter) error
	RemoveAdapter(name string) error
	Flush()
}

Logger ...

func NewLogger

func NewLogger(level level.LogLevel) Logger

NewLogger create a Logger with given log level

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL