loglevel

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2017 License: Apache-2.0 Imports: 5 Imported by: 2

README

loglevel

A simple standard out logger and formatter that supports log levels, namespaces and coloured output.

Get started

Download the package

go get -t github.com/amimof/loglevel-go

Import it in your code

package main

import "github.com/amimof/loglevel-go"

func main() {

    // Create an instance of loglevel
    loglevel := loglevel.New()
    
    // Set loglevel. 3=Debug, 2=Info, 1=Warn, 0=Error. Defaults to Info.
    loglevel.SetLevel(3)
    
    // Log away
    loglevel.Debug("Debug message")
    loglevel.Info("Info message")
    loglevel.Warn("Warn message")
    loglevel.Error("Error message")

}

Result

2017-01-21T13:56:28+01:00 DEBUG Debug message
2017-01-21T13:56:28+01:00 INFO Info message
2017-01-21T13:56:28+01:00 WARN Warn message
2017-01-21T13:56:28+01:00 ERROR Error message

Formatting

loglevel supports both non-format and format print methods. Formatting is as good as in the native fmt package but with the additional log level functionality.

package main

import "github.com/amimof/loglevel-go"

func main() {
    // Create an instance of loglevel
    loglevel := loglevel.New()
    
    // Set loglevel. 3=Debug, 2=Info, 1=Warn, 0=Error. Defaults to Info.
    loglevel.SetLevel(3)
        
    fname := "Luke"
    lname := "Skywalker"
    loglevel.Infof("The Jedi %s, %s", fname, lname)
}

Result:

2017-01-21T13:53:54+01:00 INFO The Jedi Luke, Skywalker

Namespaces

You can created multiple loglevel instances in your app. Assign different names to them, to make debugging app packages an modules easy.

package main

import "github.com/amimof/loglevel-go"

func main() {
    
    // Create an instance of loglevel
    loglevel := loglevel.New().SetLevel(3)
    
    // Set the namespace of this loglevel instance
    loglevel.Name = "StarWarsNames"    
    loglevel.PrintName =  true

    fname := "Luke"
    lname := "Skywalker"
    loglevel.Infof("The Jedi %s, %s", fname, lname)
}

Result:

2017-01-21T13:53:54+01:00 INFO StarWarsNames The Jedi Luke, Skywalker

Options

If you like vanilla, fmt.Println(), formatting. Disable those fiels.

package main

import "github.com/amimof/loglevel-go"

func main() {
    // Create an instance of loglevel
    loglevel := loglevel.New()
    loglevel.PrintTime = false
    loglevel.PrintLevel = false
    loglevel.PrintName = false
    loglevel.Warn("Achtung, Achtung!")
}

Result

Achtung, Achtung!

Documentation

Index

Constants

View Source
const (
	CLR_0 = "\x1b[30;1m"
	CLR_R = "\x1b[31;1m"
	CLR_G = "\x1b[32;1m"
	CLR_Y = "\x1b[33;1m"
	CLR_B = "\x1b[34;1m"
	CLR_M = "\x1b[35;1m"
	CLR_C = "\x1b[36;1m"
	CLR_W = "\x1b[37;1m"
	CLR_N = "\x1b[0m"
)
View Source
const (
	DEBUG = 3
	INFO  = 2
	WARN  = 1
	ERROR = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Logger

type Logger struct {
	Name       string
	Level      int
	TimeFormat string
	PrintTime  bool
	PrintName  bool
	PrintLevel bool
	UseColors  bool
	// contains filtered or unexported fields
}

func New added in v1.1.0

func New() *Logger

Create return logger

func (*Logger) Debug

func (l *Logger) Debug(message ...interface{})

Prints a debug message on a new line

func (*Logger) Debugf

func (l *Logger) Debugf(format string, message ...interface{})

Prints according to fmt.Sprintf format specifier and returns the resulting string

func (*Logger) Error

func (l *Logger) Error(message ...interface{})

Prints an error message on a new line followed by Exit

func (*Logger) Errorf

func (l *Logger) Errorf(format string, message ...interface{})

Prints according to fmt.Sprintf format specifier and returns the resulting string

func (*Logger) Info

func (l *Logger) Info(message ...interface{})

Prints n info message on a new line

func (*Logger) Infof

func (l *Logger) Infof(format string, message ...interface{})

Prints according to fmt.Sprintf format specifier and returns the resulting string

func (*Logger) Out added in v1.1.0

func (l *Logger) Out(str string) error

Writes str to stdout

func (*Logger) Output added in v1.1.0

func (l *Logger) Output(color, level, str string) (n int, err error)

Writes the specified string to std and colors it accordingly

func (*Logger) Panic added in v1.1.0

func (l *Logger) Panic(message ...interface{})

Same as fmt.Sprint followed by panic

func (*Logger) Panicf added in v1.1.0

func (l *Logger) Panicf(format string, message ...interface{})

Same as fmt.Sprintf followed by panic

func (*Logger) Panicln added in v1.1.0

func (l *Logger) Panicln(message ...interface{})

Same as fmt.Sprintln followed by panic

func (*Logger) Print added in v1.1.0

func (l *Logger) Print(message ...interface{})

Same as fmt.Sprint

func (*Logger) Printf added in v1.1.0

func (l *Logger) Printf(format string, message ...interface{})

Same as fmt.Sprintf

func (*Logger) Println added in v1.1.0

func (l *Logger) Println(message ...interface{})

Same as fmt.Sprintln

func (*Logger) SetLevel added in v1.1.0

func (l *Logger) SetLevel(level int) *Logger

func (*Logger) Warn

func (l *Logger) Warn(message ...interface{})

Prints a warning message on a new line

func (*Logger) Warnf

func (l *Logger) Warnf(format string, message ...interface{})

Prints according to fmt.Sprintf format specifier and returns the resulting string

Jump to

Keyboard shortcuts

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