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
- Variables
- func Debug(format string, args ...interface{})
- func Error(format string, args ...interface{})
- func Info(format string, args ...interface{})
- func Panic(format string, args ...interface{})
- func SetDefaultCfg(c Cfg)
- func Trace(format string, args ...interface{})
- func Warn(format string, args ...interface{})
- type Cfg
- type Console
- func (c *Console) Add(h Hook)
- func (c Console) Clone(prefix string) *Console
- func (c *Console) Debug(format string, args ...interface{})
- func (c *Console) Error(format string, args ...interface{})
- func (c *Console) Info(format string, args ...interface{})
- func (c *Console) Panic(format string, args ...interface{})
- func (c *Console) Release(h Hook)
- func (c *Console) Trace(format string, args ...interface{})
- func (c *Console) Warn(format string, args ...interface{})
- type DateFmt
- type FileFmt
- type Hook
- type Lvl
- type Writer
Constants ¶
const ( DateHide = DateFmt(iota) DateHour DateFull )
All the date format configurations
const ( FileHide = FileFmt(iota) FileShow FileFull )
All the file path configurations
const ( LvlTrace = Lvl(iota) LvlDebug LvlInfo LvlWarn LvlError LvlPanic )
List of priorities
Variables ¶
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
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
type Console ¶
type Console struct {
// contains filtered or unexported fields
}
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