console

package module
v0.0.0-...-1a13e7c Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2015 License: MIT Imports: 6 Imported by: 1

README

console.go godoc reference

Console is a lightweight logging tool inspired by the NodeJS console object and Scribe.js project.

As a js dev, I was lost in the Go world without my console.log.

console.Info("Some info")
console.Tag("Hello").Time().File().Log("Hello World")

With use of ansi colors, it output something like this in terminal :

[Hello] [main.go:48] 2015-02-15T09:22:06+01:00 Hello World

Console provided :

  • Info, Log, Warning and Error logging method
  • Tag, Time, File optional method
  • hook

See Godoc : http://godoc.org/github.com/guillaumewuip/console.go

Related :

Documentation

Overview

Console is a lightweight logging tool inspired by the NodeJS console object and http://github.com/bluejamesbond/scribe.js project.

console.Info("Some info")
console.Tag("Hello").Time().File().Log("Hello %s", "World")

Console provided Info, Log, Warning and Error method and Tag, Time, File optional information

With use of ansi colors, it output something like below in terminal :

[Tag1][Tag2] [main.go:48] 2015-02-15T09:22:06+01:00 Hello World

Author: http://github.com/guillaumewuip

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ColorsOptions

type ColorsOptions map[string]string

Logger colors

Uses colors from the https://github.com/mgutz/ansi package

Default values are :

ColorsOptions{
   "Log":     "green:black",
   "Info":    "cyan:black",
   "Error":   "red:black",
   "Warning": "yellow:black",
}

type HookFunc

type HookFunc func(Logger) error

A Hook implements a function fired each time a log is printed. It receives a Logger.

Example

Here's an example showing how to received all logs.

testHook := func(logger console.Logger) error {

	fmt.Printf(
		"Test hook fired. Message=%s, Level=%s, File=%s \n",
		logger.Message,
		logger.Level,
		logger.Location.Filename,
	)

	return nil
}

myConsole.AddHook(testHook)
Output:

type Location

type Location struct {
	Filename string
	Line     int
}

A log location.

Stores filename and line of the log call

type Logger

type Logger struct {
	Level          string
	Tags           []string
	Timestamp      int64
	ContextMessage string
	Message        string

	Location
	// contains filtered or unexported fields
}

Logger struct stores log params up to the print

func NewConsole

func NewConsole(colors ColorsOptions) *Logger

Create a new console

myConsole = console.NewConsole(console.ColorsOptions{})

Pass a ColorsOptions map (could be empty to use default values).

Colors from https://github.com/mgutz/ansi. See examples below :

Example

Create a new console with default colors

myConsole := console.NewConsole(console.ColorsOptions{})
Output:

Example (WithCustomColors)

Create a new console with custom colors

colors := console.ColorsOptions{
	"Log": "green+i:black",
}

myConsole := console.NewConsole(colors)
Output:

func (*Logger) AddHook

func (logger *Logger) AddHook(hook HookFunc)

Method to add a hook to a console

func (*Logger) Error

func (logger *Logger) Error(msg string, args ...interface{})

Error level

console.Error("Hello World")

func (*Logger) File

func (logger *Logger) File() *Logger

Add file and line information to the log

console.File().Log("Something") //[main.go:22]	Something

func (*Logger) Info

func (logger *Logger) Info(msg string, args ...interface{})

Info level

console.Info("Hello World")

func (*Logger) Log

func (logger *Logger) Log(msg string, args ...interface{})

Log level

console.Log("Hello World")

func (*Logger) Tag

func (logger *Logger) Tag(args ...string) *Logger

Add tags to the log

console.Tag("Tag1", "Tag2").Log("Something") //[Tag1][Tag2]	Something

func (*Logger) Time

func (logger *Logger) Time() *Logger

Add time information to the log

console.Time().Log("Something") //2015-02-15T10:31:46+01:00	Something

func (*Logger) Warning

func (logger *Logger) Warning(msg string, args ...interface{})

Warn level

console.Warn("Hello World")

Directories

Path Synopsis
Godeps
_workspace/src/github.com/mgutz/ansi
Small, fast library to create ANSI colored strings and codes.
Small, fast library to create ANSI colored strings and codes.

Jump to

Keyboard shortcuts

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