colorine

package module
v0.0.0-...-45d1916 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2018 License: MIT Imports: 5 Imported by: 35

README

colorine GoDoc Build Status

A simple colorized console logger for golang

Example

import "github.com/motemen/go-colorine"

logger := colorine.NewLogger(
    colorine.Prefixes{
        // Using colorine's predefined TextStyles
        "create": colorine.Info,
        "exist":  colorine.Notice,
        // Or specify your own
        "debug": colorine.TextStyle{colorine.White, colorine.None},
        "error": colorine.TextStyle{colorine.BrightRed, colorine.White},
    },
    // The default prefix color
    colorine.TextStyle{colorine.Green, colorine.None},
)

logger.Log("create", "path/to/file")
logger.Log("exist", "path/to/another/file")
logger.Log("error", "something went wrong!")

Screenshot

screenshot

Documentation

Overview

Package colorine provides a simple feature to print messages to console.

A log line consists of a prefix and a message. The prefix is colored by its content (for example below, "create" is green, "exist" is blue, and "error" is red, and so on.) and the message is not colored.

$ your_program
    create path/to/file
     exist path/to/another/file
     error something went wrong!

By default, no prefix color is defined. You must specify prefix-to-color mapping when you create a Logger. See the Logger's example.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	None = textColor{ct.None, false}

	Black   = textColor{ct.Black, false}
	Red     = textColor{ct.Red, false}
	Green   = textColor{ct.Green, false}
	Yellow  = textColor{ct.Yellow, false}
	Blue    = textColor{ct.Blue, false}
	Magenta = textColor{ct.Magenta, false}
	Cyan    = textColor{ct.Cyan, false}
	White   = textColor{ct.White, false}

	BrightBlack   = textColor{ct.Black, true}
	BrightRed     = textColor{ct.Red, true}
	BrightGreen   = textColor{ct.Green, true}
	BrightYellow  = textColor{ct.Yellow, true}
	BrightBlue    = textColor{ct.Blue, true}
	BrightMagenta = textColor{ct.Magenta, true}
	BrightCyan    = textColor{ct.Cyan, true}
	BrightWhite   = textColor{ct.White, true}
)

Text color constants. Use these to build a TextStyle.

View Source
var (
	Verbose = TextStyle{White, None}
	Info    = TextStyle{Green, None}
	Notice  = TextStyle{Blue, None}
	Warn    = TextStyle{Yellow, None}
	Error   = TextStyle{Red, None}
	Default = TextStyle{Green, None}
)

Predefined TextStyles. Of course you can ignore these.

Functions

This section is empty.

Types

type Logger

type Logger struct {
	// Prefix string to TextStyle mapping.
	Prefixes Prefixes
	// contains filtered or unexported fields
}

A Logger handles the printing stuff. You can create one with NewLogger().

Example
logger := colorine.NewLogger(
	colorine.Prefixes{
		// Using colorine's predefined TextStyles
		"create": colorine.Info,
		"exist":  colorine.Notice,
		// Or specify your own
		"debug": colorine.TextStyle{colorine.White, colorine.None},
		"error": colorine.TextStyle{colorine.BrightRed, colorine.White},
	},
	// The default prefix color
	colorine.TextStyle{colorine.Green, colorine.None},
)

logger.Log("create", "path/to/file")
logger.Log("exist", "path/to/another/file")
logger.Log("error", "something went wrong!")
Output:

func NewLogger

func NewLogger(prefixes Prefixes, defaultStyle TextStyle) *Logger

NewLogger creates a new Logger. prefixes is a prefix-string-to-text-style mapping. The log prefix is colored using this table. If no entry is found in prefixes, defaultStyle is used.

By default, no prefix is registered to any text style.

func (*Logger) Log

func (logger *Logger) Log(prefix, message string)

Log prints messages to console. prefix is colored using the logger.prefixes table.

func (*Logger) SetOutput

func (logger *Logger) SetOutput(w io.Writer)

SetOutput sets the output destination for the logger.

type Prefixes

type Prefixes map[string]TextStyle

A Prefixes is a map of prefix string to TextStyle. Prefix strings of log lines are colored according to this mapping. The TextStyle for an empty prefix ("") is used for default TextStyle.

type TextStyle

type TextStyle struct {
	Foreground textColor
	Background textColor
}

A TextStyle represents the style that strings are displayed with. Consists of a foreground and a background, each of which is one of the text color constants.

Jump to

Keyboard shortcuts

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