Coder Social home page Coder Social logo

gogger's Introduction

gogger

CI CodeQL Go Report Card Go Reference

A Go library for logging.

Overview

gogger is a logging library, providing just one Log struct and Logger interface. This is to simplify instance creation and to ensure that the exact same approach can always be taken.
Log struct is designed to be highly extensible, using the LogWriter interface for log output and the LogFormatter interface for log formatting, which can be freely replaced. Also, multiple LogWriters can be configured for Log, so that the same log can be written to both stdout and a file, for example.
Since LogWriter and LogFormatter are exported interfaces, users are free to define and use them if they are dissatisfied with the built-in ones. If they are useful, please give us pull request.

Quickstart

Here is an example of using the built-in LogStreamWriter and LogSimpleFormatter. Those details are described after this section.
Remember that Log generation is done here by passing only LogConfig to its constructor, NewLog. Then, configure LogWriter and LogFormatter in LogConfig.

package main

import (
	"github.com/yackrru/gogger"
	"os"
	"time"
)

func main() {
	writer := gogger.NewLogStreamWriter(gogger.LogStreamWriterOption{
		Output: os.Stderr,
	})
	writer.Open()
	defer writer.Close()
	
	logger := gogger.NewLog(&gogger.LogConfig{
		Writers: []gogger.LogWriter{writer},
		Formatter: gogger.NewLogSimpleFormatter(gogger.DefaultLogSimpleFormatterTmpl),
	})
	
	// logging
	logger.Info("log string")
}

LogConfig

It is a centralized setting for Log.

option default description
Writers - Set a LogWriter for each output destination.
Formatter - Only one LogFormatter can be set.
TimeFormat "2006-01-02 15:04:05.000" Time stamp format. Specify a time format string for the golang.
LogMinLevel INFO Specify the log level you want to output. Log levels are ERROR, WARN, INFO, DEBUG, and OFF.

Writer

LogStreamWriter

It is an asynchronous log writer with an internal buffer of log strings and a log output goroutine.

gogger.NewLogStreamWriter(gogger.LogStreamWriterOption{
	Output: os.Stderr,
	SyncIntervalMills: 100,
})
option default description
Output os.Stderr Specify the *os.File to which the logs will be output.
SyncIntervalMills 100 The log output interval. (milliseconds)

Formatter

LogSimpleFormatter

It is a LogFormatter that formats a predefined parameter and log string in a format set as a template. The struct itself has only one parameter, a template string called tmpl, which is passed to the constructor when the instance is created.

gogger.NewLogSimpleFormatter(gogger.DefaultLogSimpleFormatterTmpl)
option default description
tmpl "%timestamp% %level% --- [%pkg%] %args%" The template string. Each parameter is enclosed in %. %args% is the string passed to Log.

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.