logo

package module
v0.0.0-...-bcfdb5a Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2018 License: BSD-2-Clause Imports: 6 Imported by: 8

README

LoGo is a Golang logging library.

License

LoGo is licensed under the Simplified BSD License.

How to Install

Make Sure You Have golang installed!

$ go get github.com/Nurza/LoGo

How to Upgrade

Make Sure You Have golang installed!

$ go get -u github.com/Nurza/LoGo

How to Use

See the "examples" directory.

Examples

  • Simple console logging
type SimpleStructure struct {
	A string
	B bool
	C int
	D float64
	F byte
}

func main() {
	logo.Log(true)                                                 // bool
	logo.Log(42)                                                   // int
	logo.Log(42.42)                                                // float64
	logo.Log('A')                                                  // byte
	logo.Log(SimpleStructure{"Hello World", true, 42, 42.42, 'A'}) // Structure
	logo.Log("Hello", true, 42, "World")                           // Mixed

	logo.Log("Hello", "World", "!") // Is the same as :
	logo.Log("Hello World !")
}

Simple console logging

  • Console logging
func main() {
	var l logo.Logger            // Create a simple Logger
	l.AddTransport(logo.Console) // Add a transport: Console
	l.EnableAllLevels()          // Enable all logging levels

	l.Log(logo.Info, "Hello World !") // This is the same as:
	l.Info("Hello World !")
}

Console logging

  • File logging
func main() {
	var l logo.Logger                     // Create a simple Logger
	l.AddTransport(logo.File, "test.log") // Add a transport: Console
	l.EnableAllLevels()                   // Enable all logging levels

	l.Log(logo.Info, "Hello World !") // This is the same as:
	l.Info("Hello World !")
}
  • Console logging with color:
func main() {
	var l logo.Logger                 // Create a simple Logger
	t := l.AddTransport(logo.Console) // Add a transport: Console
	t.AddColor(logo.ConsoleColor)     // Add a color: Console color
	l.EnableAllLevels()               // Enable all logging levels

	l.Silly("Silly")
	l.Debug("Debug")
	l.Verbose("Verbose")
	l.Info("Info")
	l.Warn("Warn")
	l.Error("Error")
	l.Critical("Critical")
}

Console logging with color

  • Logging with timer:
func main() {
	var l logo.Logger                  // Create a simple Logger
	t := l.AddTransport(logo.Console)  // Add a transport: Console
	t.AddColor(logo.ConsoleColor)      // Add a color: Console color
	l.AddTime("[2006-01-02 15:04:05]") // Add time template
	l.EnableAllLevels()                // Enable all logging levels

	l.Silly("Silly")
	l.Debug("Debug")
	l.Verbose("Verbose")
	l.Info("Info")
	l.Warn("Warn")
	l.Error("Error")
	l.Critical("Critical")
}

Logging with timer

Documentation

Index

Constants

View Source
const (
	ConsoleColor = iota
	HTMLColor    = iota
)

List of Colors

View Source
const (
	ConsoleColorDefaultText      = "\x1b[39m"
	ConsoleColorBlackText        = "\x1b[30m"
	ConsoleColorRedText          = "\x1b[31m"
	ConsoleColorGreenText        = "\x1b[32m"
	ConsoleColorYellowText       = "\x1b[33m"
	ConsoleColorBlueText         = "\x1b[34m"
	ConsoleColorMagentaText      = "\x1b[35m"
	ConsoleColorCyanText         = "\x1b[36m"
	ConsoleColorLightGrayText    = "\x1b[37m"
	ConsoleColorDarkGrayText     = "\x1b[90m"
	ConsoleColorLightRedText     = "\x1b[91m"
	ConsoleColorLightGreenText   = "\x1b[92m"
	ConsoleColorLightYellowText  = "\x1b[93m"
	ConsoleColorLightBlueText    = "\x1b[94m"
	ConsoleColorLightMagentaText = "\x1b[95m"
	ConsoleColorLightCyanText    = "\x1b[96m"
	ConsoleColorWhiteText        = "\x1b[97m"
)

List of Console Text Colors

View Source
const (
	ConsoleColorDefaultBackground      = "\x1b[49m"
	ConsoleColorBlackBackground        = "\x1b[40m"
	ConsoleColorRedBackground          = "\x1b[41m"
	ConsoleColorGreenBackground        = "\x1b[42m"
	ConsoleColorYellowBackground       = "\x1b[43m"
	ConsoleColorBlueBackground         = "\x1b[44m"
	ConsoleColorMagentaBackground      = "\x1b[45m"
	ConsoleColorCyanBackground         = "\x1b[46m"
	ConsoleColorLightGrayBackground    = "\x1b[47m"
	ConsoleColorDarkGrayBackground     = "\x1b[100m"
	ConsoleColorLightRedBackground     = "\x1b[101m"
	ConsoleColorLightGreenBackground   = "\x1b[102m"
	ConsoleColorLightYellowBackground  = "\x1b[103m"
	ConsoleColorLightBlueBackground    = "\x1b[104m"
	ConsoleColorLightMagentaBackground = "\x1b[105m"
	ConsoleColorLightCyanBackground    = "\x1b[106m"
	ConsoleColorWhiteBackground        = "\x1b[107m"
)

List of Console Background Colors

View Source
const (
	ConsoleColorBold       = "\x1b[1m"
	ConsoleColorDim        = "\x1b[2m"
	ConsoleColorUnderlined = "\x1b[4m"
	ConsoleColorBlink      = "\x1b[5m"
	ConsoleColorReverse    = "\x1b[7m"
	ConsoleColorHidden     = "\x1b[8m"
)

List of Console Text Effects

View Source
const (
	HTMLColorDefaultText   = "deft"
	HTMLColorBlackText     = "blat"
	HTMLColorRedText       = "redt"
	HTMLColorGreenText     = "gret"
	HTMLColorYellowText    = "yelt"
	HTMLColorBlueText      = "blut"
	HTMLColorMagentaText   = "magt"
	HTMLColorCyanText      = "cyat"
	HTMLColorLightGrayText = "lgrt"
	HTMLColorDarkGrayText  = "dgrt"
	HTMLColorWhiteText     = "whit"
)

List of HTML Text Colors

View Source
const (
	HTMLColorDefaultTextCSS   = "black"
	HTMLColorBlackTextCSS     = "black"
	HTMLColorRedTextCSS       = "red"
	HTMLColorGreenTextCSS     = "green"
	HTMLColorYellowTextCSS    = "yellow"
	HTMLColorBlueTextCSS      = "blue"
	HTMLColorMagentaTextCSS   = "magenta"
	HTMLColorCyanTextCSS      = "cyan"
	HTMLColorLightGrayTextCSS = "lightgray"
	HTMLColorDarkGrayTextCSS  = "darkgray"
	HTMLColorWhiteTextCSS     = "white"
)
View Source
const (
	HTMLColorDefaultBackground   = "defbg"
	HTMLColorBlackBackground     = "blabg"
	HTMLColorRedBackground       = "redbg"
	HTMLColorGreenBackground     = "grebg"
	HTMLColorYellowBackground    = "yelbg"
	HTMLColorBlueBackground      = "blubg"
	HTMLColorMagentaBackground   = "magbg"
	HTMLColorCyanBackground      = "cyabg"
	HTMLColorLightGrayBackground = "lgrbg"
	HTMLColorDarkGrayBackground  = "dgrbg"
	HTMLColorWhiteBackground     = "whibg"
)

List of HTML Background Colors

View Source
const (
	HTMLColorDefaultBackgroundCSS   = "LightSteelBlue"
	HTMLColorBlackBackgroundCSS     = "black"
	HTMLColorRedBackgroundCSS       = "red"
	HTMLColorGreenBackgroundCSS     = "green"
	HTMLColorYellowBackgroundCSS    = "yellow"
	HTMLColorBlueBackgroundCSS      = "blue"
	HTMLColorMagentaBackgroundCSS   = "magenta"
	HTMLColorCyanBackgroundCSS      = "cyan"
	HTMLColorLightGrayBackgroundCSS = "lightgray"
	HTMLColorDarkGrayBackgroundCSS  = "darkgray"
	HTMLColorWhiteBackgroundCSS     = "white"
)
View Source
const (
	HTMLColorBold       = "bold"
	HTMLColorBolder     = "bolder"
	HTMLColorUnderlined = "underln"
	HTMLColorItalic     = "italic"
)

List of HTML Text Effects

View Source
const (
	Silly    = iota
	Debug    = iota
	Verbose  = iota
	Info     = iota
	Warn     = iota
	Error    = iota
	Critical = iota
)

List of logging levels

View Source
const (
	Console = iota
	File    = iota
)

List of transports

Variables

View Source
var (
	HTMLColorHeader string = `
<style>
*{background-color: ` + HTMLColorDefaultBackgroundCSS + `;}
.` + HTMLColorDefaultText + `{color: ` + HTMLColorDefaultTextCSS + `;}
.` + HTMLColorBlackText + `{color: ` + HTMLColorBlackTextCSS + `;}
.` + HTMLColorRedText + `{color: ` + HTMLColorRedTextCSS + `;}
.` + HTMLColorGreenText + `{color: ` + HTMLColorGreenTextCSS + `;}
.` + HTMLColorYellowText + `{color: ` + HTMLColorYellowTextCSS + `;}
.` + HTMLColorBlueText + `{color: ` + HTMLColorBlueTextCSS + `;}
.` + HTMLColorMagentaText + `{color: ` + HTMLColorMagentaTextCSS + `;}
.` + HTMLColorCyanText + `{color: ` + HTMLColorCyanTextCSS + `;}
.` + HTMLColorLightGrayText + `{color: ` + HTMLColorLightGrayTextCSS + `;}
.` + HTMLColorDarkGrayText + `{color: ` + HTMLColorDarkGrayTextCSS + `;}
.` + HTMLColorWhiteText + `{color: ` + HTMLColorWhiteTextCSS + `;}

.` + HTMLColorDefaultBackground + `{background-color: ` + HTMLColorDefaultBackgroundCSS + `;display:inline;}
.` + HTMLColorBlackBackground + `{background-color: ` + HTMLColorBlackBackgroundCSS + `;display:inline;}
.` + HTMLColorRedBackground + `{background-color: ` + HTMLColorRedBackgroundCSS + `;display:inline;}
.` + HTMLColorGreenBackground + `{background-color: ` + HTMLColorGreenBackgroundCSS + `;display:inline;}
.` + HTMLColorYellowBackground + `{background-color: ` + HTMLColorYellowBackgroundCSS + `;display:inline;}
.` + HTMLColorBlueBackground + `{background-color: ` + HTMLColorBlueBackgroundCSS + `;display:inline;}
.` + HTMLColorMagentaBackground + `{background-color: ` + HTMLColorMagentaBackgroundCSS + `;display:inline;}
.` + HTMLColorCyanBackground + `{background-color: ` + HTMLColorCyanBackgroundCSS + `;display:inline;}
.` + HTMLColorLightGrayBackground + `{background-color: ` + HTMLColorLightGrayBackgroundCSS + `;display:inline;}
.` + HTMLColorDarkGrayBackground + `{background-color: ` + HTMLColorDarkGrayBackgroundCSS + `;display:inline;}
.` + HTMLColorWhiteBackground + `{background-color: ` + HTMLColorWhiteBackgroundCSS + `;display:inline;}

.` + HTMLColorBold + `{font-weight: bold;}
.` + HTMLColorBolder + `{font-weight: bolder;}
.` + HTMLColorUnderlined + `{text-decoration: underline;}
.` + HTMLColorItalic + `{font-style: italic;}
</style>
`
)

HTML Header for CSS

Functions

func ApplyConsoleColor

func ApplyConsoleColor(s string, level int, c ConsoleColorTheme) string

Apply a color theme on a string with the corect logging level

func ApplyHTMLColor

func ApplyHTMLColor(s string, level int, c HTMLColorTheme) string

Apply a color theme on a string with the corect logging level

func ConsoleWrite

func ConsoleWrite(t *Transport, s string)

Write function of the transport

func FileWrite

func FileWrite(t *Transport, s string)

Write function of the transport

Append a string at the end of the file

func GoRoutineID

func GoRoutineID() string

Return the current go routine id

func LevelToString

func LevelToString(level int) string

func Log

func Log(args ...interface{})

This function is the simpliest form to write logs with LoGo. It writes variables in the console like fmt.Println()

This function can be rewrite like this:

func Log(args ...interface{}) {
	fmt.Println(args...)
}

But I want to leave the choice of the format to the users. Like replacing: s += fmt.Sprintf("%v ", v) with: s += fmt.Sprintf("%+v ", v) To add struct field names

Types

type ConsoleColorAspect

type ConsoleColorAspect struct {
	TextColor       string
	BackgroundColor string
	TextEffect      []string
}

A Color Aspect describes text and background colors and effects

type ConsoleColorTheme

type ConsoleColorTheme struct {
	Silly    ConsoleColorAspect
	Debug    ConsoleColorAspect
	Verbose  ConsoleColorAspect
	Info     ConsoleColorAspect
	Warn     ConsoleColorAspect
	Error    ConsoleColorAspect
	Critical ConsoleColorAspect
}

A Color Theme is a list of Color Aspect (one for each logging level)

type HTMLColorAspect

type HTMLColorAspect struct {
	TextColor       string
	BackgroundColor string
	TextEffect      []string
}

A Color Aspect describes text and background colors and effects

type HTMLColorTheme

type HTMLColorTheme struct {
	Silly    HTMLColorAspect
	Debug    HTMLColorAspect
	Verbose  HTMLColorAspect
	Info     HTMLColorAspect
	Warn     HTMLColorAspect
	Error    HTMLColorAspect
	Critical HTMLColorAspect
}

A Color Theme is a list of Color Aspect (one for each logging level)

type JSONFormatted

type JSONFormatted struct {
	Prefix      string `json:"prefix,omitempty"`
	Suffix      string `json:"suffix,omitempty"`
	Level       string `json:"level"`
	GoRoutineID string `json:"goroutineid,omitempty"`
	Time        string `json:"time,omitempty"`
	Text        string `json:"text"`
}

JSON Formatted struct

type Logger

type Logger struct {
	Transports               []Transport
	Time                     string
	Prefix                   string
	Suffix                   string
	SillyL                   bool
	DebugL                   bool
	VerboseL                 bool
	InfoL                    bool
	WarnL                    bool
	ErrorL                   bool
	CriticalL                bool
	SillyAttachedFunction    func(string)
	DebugAttachedFunction    func(string)
	VerboseAttachedFunction  func(string)
	InfoAttachedFunction     func(string)
	WarnAttachedFunction     func(string)
	ErrorAttachedFunction    func(string)
	CriticalAttachedFunction func(string)
	GoRoutineId              bool
}

A Logger struct

func (*Logger) AddTime

func (l *Logger) AddTime(layout string)

Attach a new time to a Logger

func (*Logger) AddTransport

func (l *Logger) AddTransport(t int, param ...interface{}) *Transport

Attach a new Transport to a Logger

func (*Logger) AttachFunction

func (l *Logger) AttachFunction(level int, function func(string))

Attach a function

func (*Logger) Critical

func (l *Logger) Critical(args ...interface{})

func (*Logger) CriticalF

func (l *Logger) CriticalF(s string, args ...interface{})

func (*Logger) CriticalNR

func (l *Logger) CriticalNR(args ...interface{})

func (*Logger) Debug

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

func (*Logger) DebugF

func (l *Logger) DebugF(s string, args ...interface{})

func (*Logger) DebugNR

func (l *Logger) DebugNR(args ...interface{})

func (*Logger) DisableAllLevels

func (l *Logger) DisableAllLevels() *Logger

These functions disable or enable logging level :

func (*Logger) DisableLevel

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

func (*Logger) EnableAllLevels

func (l *Logger) EnableAllLevels() *Logger

func (*Logger) EnableLevel

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

func (*Logger) Error

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

func (*Logger) ErrorF

func (l *Logger) ErrorF(s string, args ...interface{})

func (*Logger) ErrorNR

func (l *Logger) ErrorNR(args ...interface{})

func (*Logger) Info

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

func (*Logger) InfoF

func (l *Logger) InfoF(s string, args ...interface{})

func (*Logger) InfoNR

func (l *Logger) InfoNR(args ...interface{})

func (*Logger) Log

func (l *Logger) Log(level int, args ...interface{})

Log something with a parameterizable logging level

func (*Logger) LogNR

func (l *Logger) LogNR(level int, args ...interface{})

Log something with a parameterizable logging level without line return

func (*Logger) RemoveTime

func (l *Logger) RemoveTime()

Remove time to a Logger

func (*Logger) SetPrefix

func (l *Logger) SetPrefix(s string) *Logger

Set a prefix

func (*Logger) SetSuffix

func (l *Logger) SetSuffix(s string) *Logger

Set a suffix

func (*Logger) Silly

func (l *Logger) Silly(args ...interface{})

These functions call Log with the correct logging level :

func (*Logger) SillyF

func (l *Logger) SillyF(s string, args ...interface{})

These functions call Log with the correct logging level with formatting:

func (*Logger) SillyNR

func (l *Logger) SillyNR(args ...interface{})

These functions call Log with the correct logging level without line return:

func (*Logger) Verbose

func (l *Logger) Verbose(args ...interface{})

func (*Logger) VerboseF

func (l *Logger) VerboseF(s string, args ...interface{})

func (*Logger) VerboseNR

func (l *Logger) VerboseNR(args ...interface{})

func (*Logger) Warn

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

func (*Logger) WarnF

func (l *Logger) WarnF(s string, args ...interface{})

func (*Logger) WarnNR

func (l *Logger) WarnNR(args ...interface{})

type Transport

type Transport struct {
	Type              int
	Write             func(*Transport, string)
	ConsoleColorTheme *ConsoleColorTheme
	HTMLColorTheme    *HTMLColorTheme
	Data              []interface{}
	JSONFormatted     bool
}

A Transport struct

func (*Transport) AddColor

func (t *Transport) AddColor(colorType int)

Attach a new Color to a Logger

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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