log

package module
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2022 License: MIT Imports: 8 Imported by: 18

README

Log

Test Go Reference

Simple level logging

Levels

Levels can be:

  1. TRACE
  2. VERBOSE
  3. DEBUG
  4. INFO
  5. WARN
  6. ERROR
  7. FATAL
Usage
package main

import (
  "os"
  "github.com/taybart/log"
)


// All levels
func main() {
  log.SetLevel(log.TRACE)
  log.SetTimeFmt("2006-01-02 15:04:05") // Default time format

  // output defaults to stdout
  log.SetOutput("./logfile.log") // set output file
  log.SetOutputWriter(os.Stdout) // can also be set with an io.Writer

  log.UseColors(true) // defaults to true

  amount := 1
  thingy := "thingy"

  log.Trace(amount, thingy) // alias for "ln" version
  log.Traceln(amount, thingy)
  log.Tracef("test %s", thingy)

  log.Verbose(amount, thingy) // alias for "ln" version
  log.Verboseln(amount, thingy)
  log.Verbosef("test %s", thingy)

  log.Debug(amount) // alias for "ln" version
  log.Debugln(amount)
  log.Debugf("test %s", thingy)

  log.Info(amount, thingy) // alias for "ln" version
  log.Infoln(amount, thingy)
  log.Infof("test %s", thingy)

  log.Warn(amount, thingy) // alias for "ln" version
  log.Warnln(amount, thingy)
  log.Warnf("test %s", thingy)

  log.Error(amount, thingy) // alias for "ln" version
  log.Errorln(amount, thingy)
  log.Errorf("test %s", thingy)
}

HTTP logging

package main

import (
  "fmt"

  "github.com/taybart/log"
)

type server struct {
  router *http.ServeMux
}

func (s *server) routes() {
  // Add logging to a route
  s.router.HandleFunc("/ping", log.Middleware(func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "pong")
  }))
}

func main() {
  srv := &http.Server{
    Handler:      s.router,
    Addr:         c.addr,
    WriteTimeout: 15 * time.Second,
    ReadTimeout:  15 * time.Second,
  }

  log.Infof("Serving at %s\n", c.addr)
  log.Fatal(srv.ListenAndServe())
}

Templates

package main

import (
	"bytes"
	"text/template"

	"github.com/taybart/log"
)

type Block struct {
	Text string
}

func main() {
	log.SetPlain()
	b := Block{Text: "hello world!"}
	tmpl := `{{green "~~~~~START~~~~~" }}
{{red .Text}}
{{ green "~~~~~~END~~~~~~" }}`

	t := template.Must(template.New("block").Funcs(log.TmplFuncs).Parse(tmpl))

	var buf bytes.Buffer
	t.Execute(&buf, b)
	log.Info(buf.String())
}
Control via environment variables
LOG_NO_COLOR=true go run .
LOG_PLAIN=true go run .
LOG_LEVEL=verbose go run .

Documentation

Index

Constants

View Source
const (

	// Normal Colors
	Gray   = ce + "37m"
	Purple = ce + "35m"
	Blue   = ce + "34m"
	Yellow = ce + "33m"
	Green  = ce + "32m"
	Red    = ce + "31m"

	// Bold Colors
	BoldGray   = ceBold + "37m"
	BoldPurple = ceBold + "35m"
	BoldBlue   = ceBold + "34m"
	BoldYellow = ceBold + "33m"
	BoldGreen  = ceBold + "32m"
	BoldRed    = ceBold + "31m"

	// Italic Colors
	ItalicGray   = ceItalic + "37m"
	ItalicPurple = ceItalic + "35m"
	ItalicBlue   = ceItalic + "34m"
	ItalicYellow = ceItalic + "33m"
	ItalicGreen  = ceItalic + "32m"
	ItalicRed    = ceItalic + "31m"

	// Underlined Colors
	UnderlinedGray   = ceUnderlined + "37m"
	UnderlinedPurple = ceUnderlined + "35m"
	UnderlinedBlue   = ceUnderlined + "34m"
	UnderlinedYellow = ceUnderlined + "33m"
	UnderlinedGreen  = ceUnderlined + "32m"
	UnderlinedRed    = ceUnderlined + "31m"

	// Blinking Colors
	BlinkingGray   = ceBlinking + "37m"
	BlinkingPurple = ceBlinking + "35m"
	BlinkingBlue   = ceBlinking + "34m"
	BlinkingYellow = ceBlinking + "33m"
	BlinkingGreen  = ceBlinking + "32m"
	BlinkingRed    = ceBlinking + "31m"

	Bold       = "\033[1;3m"
	Italic     = "\033[3;3m"
	Underlined = "\033[4;1m"
	Blinking   = "\033[5;1m"
	// Return to default
	Rtd   = ce + "0m"
	Reset = ce + "0m"
)

Colors

Variables

View Source
var (

	// Output writer for log
	Output io.Writer = os.Stdout
)
View Source
var TmplFuncs = map[string]interface{}{
	"red":    red,
	"gray":   gray,
	"purple": purple,
	"blue":   blue,
	"yellow": yellow,
	"green":  green,
}

Functions

func Debug

func Debug(v ...interface{})

Debug : print var with no format

func Debugf

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

Debugf : logging

func Debugln

func Debugln(v ...interface{})

Debugln : print var with no format

func Error

func Error(v ...interface{})

Error : error var with no format

func Errorf

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

Errorf : logging

func Errorln

func Errorln(v ...interface{})

Errorln : error var with no format

func Fatal

func Fatal(v ...interface{})

Fatal : error var with no format

func Fatalf

func Fatalf(format string, v ...interface{})

Fatalf : logging

func Fatalln

func Fatalln(v ...interface{})

Fatalln : error var with no format

func Info

func Info(v ...interface{})

Info : info with no format

func Infof

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

Infof : logging

func Infoln

func Infoln(v ...interface{})

Infoln : info with no format

func Middleware

func Middleware(next http.HandlerFunc) http.HandlerFunc

func Print

func Print(v ...interface{})

Print : print var with no format

func Printf

func Printf(format string, v ...interface{})

Printf : logging

func Println

func Println(v ...interface{})

Println : print var with no format

func SetFancy added in v1.6.2

func SetFancy()

SetFancy : output, will print time and level

func SetLevel

func SetLevel(l Level)

SetLevel : used to set logging level

func SetNoTime

func SetNoTime()

SetTimeOnly : output, will not print time or level

func SetOutput

func SetOutput(filename string) error

SetOutput : set log output to a specific file, default is stdout

func SetOutputWriter

func SetOutputWriter(w io.Writer)

SetOutputWriter : set log io.Writer, if logs should be streamed, the io.Writer can be passed here

func SetPlain

func SetPlain()

SetPlain : output, will not print time or level

func SetTimeFmt

func SetTimeFmt(f string)

SetTimeFmt : used to adjust time format for logs

func SetTimeOnly

func SetTimeOnly()

SetTimeOnly : output, will not print time or level

func Test

func Test(v ...interface{})

Test : test with no format

func Testf

func Testf(format string, v ...interface{})

Testf : logging

func Testln

func Testln(v ...interface{})

Testln : test with no format

func Trace

func Trace(v ...interface{})

Trace : print var with no format

func Tracef

func Tracef(format string, v ...interface{})

Tracef : logging

func Traceln

func Traceln(v ...interface{})

Traceln : print var with no format

func UseColors

func UseColors(use bool)

UseColors : used to set colors

func Verbose

func Verbose(v ...interface{})

Verbose : print var with no format

func Verbosef

func Verbosef(format string, v ...interface{})

Verbosef : logging

func Verboseln

func Verboseln(v ...interface{})

Verboseln : print var with no format

func Warn

func Warn(v ...interface{})

Warn : error var with no format

func Warnf

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

Warnf : logging

func Warnln

func Warnln(v ...interface{})

Warnln : error var with no format

Types

type Level

type Level int

Level type for level logging

const (
	// TRACE lowest = most verbose
	TRACE Level = iota + 1
	// DEBUG level
	DEBUG
	// VERBOSE level
	VERBOSE
	// TEST special level for testing
	TEST
	// INFO level
	INFO
	// WARN level
	WARN
	// ERROR level
	ERROR
	// FATAL level
	FATAL
)

Jump to

Keyboard shortcuts

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