Giter VIP home page Giter VIP logo

golog's Introduction

Golog GoDoc

Golog is a lightweight and expandable logger for Golang.

Features

  • Compatible with the standard log package
  • Output to console with nicely color
  • Write to file and rotate by date, hour and size
  • Write to multi-output simultaneously
  • Customizable log header
  • Redirect stdout, stderr to log
  • Build-in five log levels

Quick start

Golog has a global Logger object initiated with a global ConsoleWriter object. After import Golog, you could call Debugf(), Infof(), Warnf(), Errorf() and Criticalf() to write logs of different levels with the global logger.

go get -u github.com/thinkphoebe/golog
package main

import log "github.com/thinkphoebe/golog"

func main() {
    log.Infof("Hello world! this is Golog")
}

Usage

Convert codes use the standard log package to Golog

The interface of Golog is compatible with the standard log package. You just need to modify import from

import "log"

to

import log "github.com/thinkphoebe/golog"

Output to console

By default, global Logger output to console with nicely colors. You can call SetBrush() or SetColored() method of ConsoleWriter() to modify colors or disable log color. If you use the global logger, you can get its ConsoleWriter object with log.GConsoleWriter.

log.SetLevel(log.LevelDebug)
log.Debugf("Golog is a lightweight and expandable logger for Golang")
log.Infof("Golog is compatible with the standard log library")
log.Warnf("Golog can output to console with nicely color")
log.Errorf("Golog output to file and rotate by date, hour and size")
log.Criticalf("Golog can redirect stdout, stderr to log")

log.Warnf("test Warning log with default color")
log.GConsoleWriter.SetBrush("\033[34;1m", log.LevelWarn)
log.Warnf("test Warning log with custom color")
log.GConsoleWriter.SetColored(false)
log.Warnf("test Warning log not colored")

Console colored

Output to file

Golog can output to file by add RotateWriter. RotateWrite support rotate log by hour, day and file size.

fmtStr := `%(asctime) [%(levelno)][%(filename):%(lineno)] `
w := log.NewRotateWriter("Rotate.log", log.RotateBySize)
w.SetRotateSize(100 * 1000 * 1000)
log.Init(w, log.LevelDebug, fmtStr)

Write to multi-output

Golog can write to multi-output simultaneously. You can add output by AddOutput() and remove output by RemoveOutput().

log.Infof("write to stderr")
wFile := log.NewRotateWriter("Multi.log", log.RotateByHour)
log.AddOutput(wFile)
log.Infof("both write to both stderr and log file")
log.Infof("remove file output %t", log.RemoveOutput(log.GConsoleWriter))
log.Infof("only write to log file")

Redirect system stdout and stderr to log

Please attention that AddRedirect() will modify the value of **os.File you passed to it. So if you want to output logs to stderr, you must add ConsoleWriter before AddRedirect() called. The following example use the global logger which GConsoleWriter already added at the package initialization.

fmt.Fprint(os.Stdout, "write to stdout before redirect\n")
fmt.Fprint(os.Stderr, "write to stderr before redirect\n")

rstdr := log.AddRedirect(&os.Stderr, log.LevelError, "[stderr]")
rstdo := log.AddRedirect(&os.Stdout, log.LevelInfo, "[stdout]")

log.Infof("write to log 1")
fmt.Fprint(os.Stdout, "write to stdout with recirect\n")
fmt.Fprint(os.Stderr, "write to stderr with recirect\n")
log.Infof("write to log 2")

log.CancelRedirector(rstdr)
log.CancelRedirector(rstdo)

fmt.Fprint(os.Stdout, "write to stdout after redirect\n")
fmt.Fprint(os.Stderr, "write to stderr after redirect\n")

Console colored

Customize log header

To customize log header, you need to pass a format string to log.NewLogger(). For the global logger, you could call log.Init() to re-initialize. Format string supports the following keywords write as %(xxx).

  • asctime - timestamp
  • levelno - log level
  • filename - source file name
  • lineno - line no log in source file
fmtStr := `%(asctime) [%(levelno)][%(filename):%(lineno)] `
log.Init(log.NewConsoleWriter(os.Stderr), log.LevelDebug, fmtStr)

New Logger

The most convenient way is to use the global logger. However, you could create new Logger object in some complicate usage.

fmtStr := `%(asctime) [%(levelno)][%(filename):%(lineno)] `
logger, err := log.NewLogger(log.NewRotateWriter("golog.log", log.RotateByHour), log.LevelDebug, fmtStr)
if err != nil {
    // error handling
}
logger.Infof("Hello world, this is [%s]", "golog")

Customize log level

log.SetLevel() accept LevelDebug, LevelInfo, LevelWarn, LevelError and LevelCritical. If you set log level to LevelWarn, only Warn, Error and Critical logs will be output.

log.SetLevel(LevelInfo)

Use you own writer

You can implement your own writers in addition to ConsoleWriter and RotateWriter. You need to implement IOutput interface defined in log.go and pass it to log.NewLogger() or log.Init().

golog's People

Contributors

thinkphoebe avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.