console

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2016 License: Apache-2.0 Imports: 10 Imported by: 0

README

Welcome

GoDoc Build Status codecov.io

Console is a logger package for Go with the following features:

  • Multiple priorities
  • Fully configurable
  • Thread safe
  • Runtime execution of func() string arguments
  • Introduces Hooks that can match some messages and reply with some actions.

Usage

It's recommended to use gopkg.in to ensure to use a stable version.

import (
	"gopkg.in/klaidliadon/console.v1"
)
Basic Usage

The standard console uses os.Stdout:

c := console.Std()
c.Info("This is console")
Custom Console

You can define your custom logger and use it:

// Create a *console.Console
var custom = console.New(console.Cfg{
	Color: true, 
	Date: console.DateFull,
}, w)
custom.Trace("Ignored message %d", 1)
custom.Info("Message not ignored %d", 1)

Features

Runtime execution

You can use a func() string as argument. Instead of the function the result string will be printed. If the message is ignored the function will not be executed.

var a = Tree{} // With a very expensive String method

l := console.New(console.Cfg{Lvl: console.LvlDebug})
// Tree.String is executed
l.Info("Method result: %s", a)
// Tree.String is ignored
l.Trace("Method result: %s", a)
Hooks

An hook is interface used to capture certain conditions and execute an action.

Here's an example:

type Mailer struct {
	Id   int64
	lvl  console.Lvl
	Addr string
	Auth smtp.Auth
	From string
	To   []string
}

func (m Mailer) Id() string {
	return fmt.Sprintf("mailer-hook-%d", m.Id) 
}

func (m Mailer) Match(l Lvl, _, _ string, _ ...interface{}) bool { 
	return l == s.Lvl
}

func (m Mailer) Action(l Lvl, msg, _ string, _ ...interface{}) { 
	smtp.SendMail(m.Addr, m.Auth, m.From, m.To, fmt.Sprintf("[%s] from MailHook: %s", l, msg)
}

This hook captures the messages from a certain level and sends an email with the message content.

Help

For a complete reference read the docs.

Documentation

Overview

Console package implements multi priority logger.

The standard log uses os.Stdout:

c := console.Std()
c.Info("This is console")

You can define your custom logger and use it:

var custom = console.New(console.Cfg{ Color: true, Date: console.DateFull}, w)
custom.Trace("Ignored message %d", 1)
custom.Info("Message not ignored %d", 1)

You can use a `func() string` as argument. Instead of the function the result string will be printed. If the message is ignored the function will not be executed.

// Very expensive struct method
var a func() string = myObject.createTreeString
// Very expensive interface method
var b func() string = myInterface.createTreeString

l := logger.New(console.Cfg{Lvl: console.LvlDebug})
// func a is executed
l.Info("Method result: %s", a)
// func b is ignored
l.Trace("Method result: %s", b)

A main feature of the package is Hooks: a hook is interface used to capture certain conditions and execute an action. Here's an example:

type MailHook struct {
	lvl  console.LogLvl
	Addr string
	Auth Auth
	From string
	To   []string
}

func (m *MailHook) Match(l console.LogLvl, format string, args ...interface{}) bool {
	return l >= m.lvl
}

func (m *MailHook) Action(l LogLvl, msg string){
	smtp.SendMail(m.Addr, m.Auth, m.From, m.To, fmt.Sprintf("[%s] from MailHook: %s\n\n%s", l, fileline, msg)
}

This hook captures the messages from a certain level and sends an email with the message content.

Index

Constants

View Source
const (
	DateHide = DateFmt(iota)
	DateHour
	DateFull
)

All the date format configurations

View Source
const (
	FileHide = FileFmt(iota)
	FileShow
	FileFull
)

All the file path configurations

View Source
const (
	LvlTrace = Lvl(iota)
	LvlDebug
	LvlInfo
	LvlWarn
	LvlError
	LvlPanic
)

List of priorities

Variables

View Source
var Defaults = Cfg{
	Color: true,
	Date:  DateHour,
	File:  FileShow,
}

Defaults is the configuration for standard console

Functions

func Debug

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

Debug writes the default console with LvlDebug.

func Error

func Error(format string, args ...interface{})

Error writes the default console with LvlError.

func Info

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

Info writes the default console with LvlInfo.

func Panic

func Panic(format string, args ...interface{})

Panic writes the default console with LvlPanic.

func SetDefaultCfg

func SetDefaultCfg(c Cfg)

SetDefaultCfg changes the configuration of default console, used by Trace, Debug, Info, Warning, Error, Panic

func Trace

func Trace(format string, args ...interface{})

Trace writes the default console with LvlTrace.

func Warn

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

Warn writes the default console with LvlWarn.

Types

type Cfg

type Cfg struct {
	Date  DateFmt
	File  FileFmt
	Lvl   Lvl
	Color bool
	// contains filtered or unexported fields
}

Holds the configuration of a Console

func (*Cfg) Label

func (c *Cfg) Label(l Lvl) string

type Console

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

func New

func New(c Cfg, w Writer) *Console

New creates a Console.

func Std

func Std() *Console

Std creates a standard Console on `os.Stdout`.

func (*Console) Add

func (c *Console) Add(h Hook)

Adds a Hook to the logger.

func (Console) Clone

func (c Console) Clone(prefix string) *Console

Clone creates a copy of the console with the given prefix.

func (*Console) Debug

func (c *Console) Debug(format string, args ...interface{})

Debug writes the console with LvlDebug.

func (*Console) Error

func (c *Console) Error(format string, args ...interface{})

Error writes the console with LvlError.

func (*Console) Info

func (c *Console) Info(format string, args ...interface{})

Info writes the console with LvlInfo.

func (*Console) Panic

func (c *Console) Panic(format string, args ...interface{})

Panic writes the console with LvlPanic.

func (*Console) Release

func (c *Console) Release(h Hook)

Release an Hook from the logger.

func (*Console) Trace

func (c *Console) Trace(format string, args ...interface{})

Trace writes the console with LvlTrace.

func (*Console) Warn

func (c *Console) Warn(format string, args ...interface{})

Warn writes the console with LvlWarn.

type DateFmt

type DateFmt int

Date format type

type FileFmt

type FileFmt int

Filename format type

type Hook

type Hook interface {
	// Unique Id to identify Hook
	Id() string
	// Action performed by the Hook.
	Action(l Lvl, msg, format string, args ...interface{})
	// Condition that triggers the Hook.
	Match(l Lvl, msg, format string, args ...interface{}) bool
}

Hook intercepts log message and perform certain tasks, like sending email

type Lvl

type Lvl int

Logging level

type Writer

type Writer interface {
	io.Writer
	WriteString(string) (int, error)
}

Writer implements the WriteString method, as the os.File

Jump to

Keyboard shortcuts

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