Categorygithub.com/mkideal/log
modulepackage
1.1.15
Repository: https://github.com/mkideal/log.git
Documentation: pkg.go.dev

# README

LOG

License Go Report Card Travis branch GoDoc

log package inspired from golang/glog. We have following key features:

  • dependentless - No any dependencies.
  • lightweight - Lightweight and easy to use.
  • highly customizable - You can customize Writer,Printer.
  • fast - Write logs to a buffer queue.

Getting started

package main

import (
	"github.com/mkideal/log"
)

func main() {
	// Start and Shutdown
	log.Start(/* options ... */)
	defer log.Shutdown()

	// Default log level is log.LvINFO, you can change the level as following:
	//
	//	log.SetLevel(log.LvTRACE)
	// 	log.SetLevel(log.LvDEBUG)
	// 	log.SetLevel(log.LvINFO)
	// 	log.SetLevel(log.LvWARN)
	// 	log.SetLevel(log.LvERROR)
	// 	log.SetLevel(log.LvFATAL)

	log.Trace("%s cannot be printed", "TRACE")
	log.Debug("%s cannot be printed", "DEBUG")

	log.Info("%s should be printed", "INFO")
	log.Warn("%s should be printed", "WARN")
	log.Error("%s should be printed", "ERROR")

	log.Fatal("%s should be printed and exit program with status code 1", "FATAL")
	log.Info("You cannot see me")
}

Log level

There are 6 log levels: Fatal,Error,Warn,Info,Debug,Trace

Default log level is Info if log level isn't specified.

Start options

Examples:

// WithConsole
func main() {
	log.Start(log.WithConsole())
	defer log.Shutdown()
	...
}
// WithFile
func main() {
	log.Start(log.WithFile(log.FileOptions{
		Dir: "./log",
		Filename: "app.log",
	}))
	defer log.Shutdown()
	...
}
// WithMultiFile
func main() {
	log.Start(log.WithMultiFile(log.MultiFileOptions{
		RootDir: "./log",
		Filename: "app.log",
	}))
	defer log.Shutdown()
	...
}
// WithWriters

// coloredConsole implements log.Writer interface
type coloredConsole struct {}

// Write implements log.Writer Write method
func (c coloredConsole) Write(level log.Level, data []byte, headerLen int) error {
	// ...
	return nil
}

// Close implements log.Writer Close method
func (c coloredConsole) Close() error{
	return nil
}

func main() {
	log.Start(log.WithWriters(coloredConsole{}))
	// multi-writers supported, e.g.
	//
	// log.Start(log.WithWriters(coloredConsole{}), log.WithFile(...))
	defer log.Shutdown()
	// ...
}
// WithPrinter


// printer implements log.Printer
type printer struct {}

// ... implements log.Printer methods

func main() {
	log.Start(log.WithPrinter(new(printer)))
	// WithPrinter conflicts with WithWriters, and printer should specified once.
	// panics if printer and writers both specified.
	// panics if more than one printer specified.
	defer log.Shutdown()
	// ...
}
// WithHTTPHandler
func main() {
	log.Start(log.WithHTTPHandler(true))
	defer log.Shutdown()
	// ...
}
// WithLevel
func main() {
	log.Start(log.WithLevel(log.LvWARN))
	defer log.Shutdown()
	// ...
}
// WithPrefix
func main() {
	log.Start(log.WithPrefix("name"))
	defer log.Shutdown()
	// ...
}

Print functions

  • Fatal(format string, args ...interface{})
  • Error(format string, args ...interface{})
  • Warn(format string, args ...interface{})
  • Info(format string, args ...interface{})
  • Debug(format string, args ...interface{})
  • Trace(format string, args ...interface{})

# Functions

Debug creates a context fields with level debug.
Error creates a context fields with level error.
Fatal creates a context fields with level fatal.
GetFlags returns the flags.
GetLevel returns current log level.
Info creates a context fields with level info.
Log is a low-level API to print logging.
ParseLevel parses log level from string.
Printf wraps the global printer Printf method.
SetFlags sets the flags.
SetLevel sets current log level.
Shutdown shutdowns global printer.
Start starts logging with options.
Trace creates a context fields with level trace.
Warn creates a context fields with level warn.
WithConsole appends a console writer.
WithFile appends a file writer.
WithFlags enable or disable flags information.
WithHTTPHandler enable or disable http handler for settting level.
WithLevel sets log level.
WithMultiFile appends a multifile writer.
WithOutput appends a console writer with specified io.Writer.
WithPrefix set log prefix.
WithPrinter specify custom printer.
WithSync synchronize outputs log or not.
WithWriters appends the writers.

# Constants

append html header in file.
the datetime in the local time zone: 2001/02/03 01:23:23.
default values for the standard logger.
full file name and line number: /a/b/c/d.go:23.
final file name element and line number: d.go:23.
if Ldatetime is set, use UTC rather than the local time zone.
5.
2.
1.
4.
6.
3.
no header in file.

# Structs

Fields holds context fields.
FileOptions represents options of file writer.
MultiFileOptions represents options for multi file writer.

# Interfaces

File contains the basic writable file operations for logging.
FS wraps the basic fs operations for logging.
Printer represents the printer for logging.
Writer represents a writer for logging.

# Type aliases

FileHeader represents header type of file.
Level represents log level.
Option is option for Start.
Prefix wraps a string as a prefixed logger.