log

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2017 License: Apache-2.0 Imports: 4 Imported by: 91

Documentation

Overview

Package log provides functionality to output human readable, colorful test to STDOUT and STDERR. It's best used for programs, such as CLI apps, that write output to people rather than machines. It is not intended for logging to log aggregators or other systems. It takes advantage of the github.com/deis/pkg/prettyprint to provide colorful output.

This package provides global functions for use as well as a 'Logger' struct that you can instantiate at-will for customized logging. All global funcs operate on a DefaultLogger, which is pre-configured to log to os.Stdout and os.Stderr, with debug logs turned off.

Example usage of global functions:

import "github.com/deis/pkg/log"
log.Info("Hello Gophers!") // equivalent of log.DefaultLogger.Info("hello gophers!")
log.Debug("log.DefaultLogger initializes with debug logs turned off, so you can't see me!")
log.DefaultLogger.SetDebug(true)
log.Debug("Now that we turned debug logs on, you can see me now!")

Example usage of instantiating an individual logger:

// create a new logger that sends all stderr logs to /dev/null, and turns on debug logs
logger := log.NewLogger(os.Stdout, iouitl.Discard, true)
log.Debug("Hello Gophers!")

Package log is a convenience wrapper for logging messages of various levels (associated colors to come) to the terminal. Much of this code has been shamelessly stolen from https://github.com/helm/helm/blob/master/log/log.go

Index

Constants

View Source
const (
	DebugPrefix = "[DEBUG]"
	ErrorPrefix = "[ERROR]"
	WarnPrefix  = "[WARN]"
	InfoPrefix  = "--->"
)

Variables

View Source
var (
	// Default resets the console color
	Default = Color(prettyprint.Colors["Default"])
	// Red sets the console color to red
	Red = Color(prettyprint.Colors["Red"])
	// Cyan sets the console color to cyan
	Cyan = Color(prettyprint.Colors["Cyan"])
	// Yellow sets the console color to yellow
	Yellow = Color(prettyprint.Colors["Yellow"])
	// Green sets the console color to green
	Green = Color(prettyprint.Colors["Green"])
)
View Source
var DefaultLogger = &Logger{stdout: os.Stdout, stderr: os.Stderr, debug: false}

DefaultLogger is the default logging implementation. It's used in all top level funcs inside the log package, and represents the equivalent of NewLogger(os.Stdout, os.Stderr)

Functions

func CleanExit

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

CleanExit is a convenience function for DefaultLogger.CleanExit(...)

func Debug

func Debug(msg string, v ...interface{})

Debug is a convenience function for DefaultLogger.Debug(...)

func Die

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

Die is a convenience function for DefaultLogger.Die(...)

func Err

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

Err is a convenience function for DefaultLogger.Err(...)

func Info

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

Info is a convenience function for DefaultLogger.Info(...)

func Msg

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

Msg is a convenience function for DefaultLogger.Msg(...)

func Warn

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

Warn is a convenience function for DefaultLogger.Warn(...)

Types

type Color

type Color string

Color is the representation of a color, to be used in Colorize

func (Color) String

func (c Color) String() string

String is the fmt.Stringer interface implementation

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is the base logging struct from which all logging functionality stems

func NewLogger

func NewLogger(stdout, stderr io.Writer, debug bool) *Logger

NewLogger creates a new logger bound to a stdout and stderr writer, which are most commonly os.Stdout and os.Stderr, respectively

func (*Logger) CleanExit

func (l *Logger) CleanExit(format string, v ...interface{})

CleanExit prints a message and then exits with 0.

func (*Logger) Debug

func (l *Logger) Debug(msg string, v ...interface{})

Debug prints a cyan-tinted message if debug logs are on.

func (*Logger) Die

func (l *Logger) Die(format string, v ...interface{})

Die prints an error and then call os.Exit(1).

func (*Logger) Err

func (l *Logger) Err(format string, v ...interface{})

Err prints an error message. It does not cause an exit.

func (*Logger) Info

func (l *Logger) Info(format string, v ...interface{})

Info prints a green-tinted message

func (*Logger) Msg

func (l *Logger) Msg(format string, v ...interface{})

Msg passes through the formatter, but otherwise prints exactly as-is.

No prettification.

func (*Logger) SetDebug

func (l *Logger) SetDebug(debug bool)

SetDebug sets the internal debugging field on or off. This func is not concurrency safe.

func (*Logger) SetStderr added in v0.5.0

func (l *Logger) SetStderr(w io.Writer)

SetStderr sets the internal stderr writer. This func is not concurrency safe.

func (*Logger) SetStdout added in v0.5.0

func (l *Logger) SetStdout(w io.Writer)

SetStdout sets the internal stdout writer. This func is not concurrency safe.

func (*Logger) Warn

func (l *Logger) Warn(format string, v ...interface{})

Warn prints a yellow-tinted warning message.

Jump to

Keyboard shortcuts

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