Help us shape the future of Go! Take the 2026 Go Developer Survey

logger

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 17, 2021 License: Apache-2.0 Imports: 6 Imported by: 336

README

Kris Nóva Logger

History

  • [2017] Originally ported from it's original location in the Kubicorn code base.
  • [2021] Refactored to support custom io.Writer's

Install

go get github.com/kris-nova/logger

Basic Usage

package main

import (
	"github.com/kris-nova/logger"
	"os"
)

func main() {
	// Options
	logger.Writer = os.Stdout // This is not needed
	logger.BitwiseLevel = logger.LogCritical | logger.LogWarning // Customize your levels
	logger.BitwiseLevel = logger.LogEverything // Turn everything on
	logger.BitwiseLevel = logger.LogAlways // Only log Always()
	logger.BitwiseLevel = logger.LogEverything // Turn everything back on 
	// 
	
	// Log lines
	logger.Debug("Check this out %d", 123)
	logger.Info("Cool!")
	logger.Success("Hooray!")
	logger.Always("Hello!")
	logger.Critical("Oh No!")
	logger.Warning("Beware...")
	logger.Deprecated("Don't do this!")
	//
}

Rainbow logs


package main

import (
	"github.com/kris-nova/logger"
	lol "github.com/kris-nova/lolgopher"
)


func main(){
	//
	logger.Writer = lol.NewLolWriter()          // Sometimes this will work better
	logger.Writer = lol.NewTruecolorLolWriter() // Comment one of these out
	//

	logger.BitwiseLevel = logger.LogEverything
	logger.Always("Rainbow logging")
	logger.Always("Rainbow logging")
	logger.Always("Rainbow logging")
	logger.Always("Rainbow logging")
}

Documentation

Index

Constants

View Source
const (
	// [Log Constants]
	//
	// These are the bitwise values for the
	// various log options.
	//
	// Also these are the string prefixes
	// for the log lines.
	LogAlways     = 1
	PreAlways     = "Always    "
	LogSuccess    = 2
	PreSuccess    = "Success   "
	LogCritical   = 4
	PreCritical   = "Critical  "
	LogWarning    = 8
	PreWarning    = "Warning   "
	LogInfo       = 16
	PreInfo       = "Info      "
	LogDebug      = 32
	PreDebug      = "Debug     "
	LogDeprecated = 64
	PreDeprecated = "Deprecated"

	LogLegacyLevel2           = LogAlways | LogSuccess | LogCritical | LogWarning | LogInfo
	LogLegacyLevel2Deprecated = LogLegacyLevel2 | LogDeprecated

	// Enable all Logging levels
	// [127]
	LogEverything = LogAlways | LogSuccess | LogDebug | LogInfo | LogWarning | LogCritical | LogDeprecated
)

Variables

View Source
var (

	// BitwiseLevel is the preferred
	// way of managing log levels.
	//
	// ----- [ Bitwise Chart ] ------
	//
	// LogEverything (All Levels)
	// LogAlways
	// LogSuccess
	// LogCritical
	// LogWarning
	// LogInfo
	// LogDebug
	// LogDeprecated
	//
	// TODO @kris-nova In the next release flip to LogEverything
	// BitwiseLevel = LogEverything
	BitwiseLevel = LogLegacyLevel2Deprecated

	// A custom io.Writer to use regardless of Mode
	Writer io.Writer = os.Stdout

	// Layout is the time layout string to use
	Layout string = time.RFC3339
)
View Source
var (

	// Timestamps are used to toggle timestamps
	// deprecated
	Timestamps = true

	// TestMode is used for running
	// the regression tests.
	// deprecated
	TestMode = false

	// Color is no longer used
	// deprecated
	Color = true

	// Level is the legacy log level
	//
	// 0 (Least verbose)
	// 1
	// 2
	// 3
	// 4 (Most verbose)
	//
	// deprecated
	Level = -1
)

Legacy Logic for 0.1.0

Here we store the legacy (0.1.0) compatible configuration options that will eventually be deprecated.

View Source
var Line = func(prefix, format string, a ...interface{}) string {
	if !strings.Contains(format, "\n") {
		format = fmt.Sprintf("%s%s", format, "\n")
	}
	if Timestamps {
		now := time.Now()
		fNow := now.Format(Layout)
		prefix = fmt.Sprintf("%s [%s]", fNow, prefix)
	} else {
		prefix = fmt.Sprintf("[%s]", prefix)
	}
	return fmt.Sprintf("%s  %s", prefix, fmt.Sprintf(format, a...))
}

Line will format a log line, and return a string

Functions

func Always

func Always(format string, a ...interface{})

Always

func Critical

func Critical(format string, a ...interface{})

Critical

func Debug

func Debug(format string, a ...interface{})

Debug

func Deprecated

func Deprecated(format string, a ...interface{})

Used to show deprecated log lines

func Info

func Info(format string, a ...interface{})

Info

func LineBytes

func LineBytes(prefix, format string, a ...interface{}) []byte

LineBytes will format a log line, and return a slice of bytes []byte

func Success

func Success(format string, a ...interface{})

Success

func Warning

func Warning(format string, a ...interface{})

Warning

Types

type LoggerFunc

type LoggerFunc func(format string, a ...interface{})

type WriterMode

type WriterMode int

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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