verbose

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2016 License: MIT Imports: 8 Imported by: 17

README

Verbose - Logging for Go

GoDoc

Verbose allows for organized, simplified, custom logging for any application. Verbose makes creating logs easy. Create multiple loggers for different purposes each with their own handlers.

Usage

Creating a Logger:

import "github.com/dragonrider23/verbose"

appLogger := verbose.New("app")
appLogger.Info("Application started")
appLogger.Warning("User not found")
appLogger.Error("I can't handle this")
appLogger.Fatal("Unhandled error occured") // Calls os.Exit(1)

To specify your own error level, use the Log function:

appLogger.Log(verbose.LogLevelCustom, "This is a custom log level message")

You can also use the Get() func to get a specific logger. If Get() can't find the logger by name, it will create a new logger:

logger := verbose.New("app")
logger = verbose.Get("app").Info("Error message") // Uses existing logger
logger = verbose.Get("module").Error("Error") // Creates new logger named 'module' and issues error

A Logger initially is nothing more than a shell. Without handlers it won't do anything. Verbose comes with two prebuilt handlers. You can use your own handlers so long as they satisfy the verbose.Handler interface. You can add a handler by calling logger.AddHandler(Handler). A Logger will cycle through all the handlers and send the message to any that report they can handle the log level.

Included handlers

StdoutHandler

The StdoutHandler will print colored log messages to stdout. This handler supports specifying a minimum and maximum log level.

FileHandler

The FileHandler will write log messages to a file or directory. If it's writing to a directory, each log level will have its own file. Otherwise all log levels are written to a single file. Like the StdoutHandler, FileHandlers support specifying a minimum and maximum log level to handle.

Release Notes

v1.0.0

  • Initial Release

Versioning

For transparency into the release cycle and in striving to maintain backward compatibility, This application is maintained under the Semantic Versioning guidelines. Sometimes I screw up, but I'll adhere to these rules whenever possible.

Releases will be numbered with the following format:

<major>.<minor>.<patch>

And constructed with the following guidelines:

  • Breaking backward compatibility bumps the major while resetting minor and patch
  • New additions without breaking backward compatibility bumps the minor while resetting the patch
  • Bug fixes and misc changes bumps only the patch

For more information on SemVer, please visit http://semver.org/.

License

This package is released under the terms of the MIT license. Please see LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Color

type Color string

Color is an escaped color code for the terminal

const (
	ColorReset   Color = "\033[0m"
	ColorRed     Color = "\033[31m"
	ColorGreen   Color = "\033[32m"
	ColorYellow  Color = "\033[33m"
	ColorBlue    Color = "\033[34m"
	ColorMagenta Color = "\033[35m"
	ColorCyan    Color = "\033[36m"
	ColorWhite   Color = "\033[37m"
	ColorGrey    Color = "\033[90m"
)

Pre-defined colors

type FileHandler

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

FileHandler writes log messages to a file to a directory

func NewFileHandler

func NewFileHandler(path string) (*FileHandler, error)

NewFileHandler takes the path and returns a FileHandler. If the path exists, file or directory mode will be Determined by what path is. If it doesn't exist, the mode will be file if path has an extension, otherwise it will be directory. In file mode, all log messages are written to a single file. In directory mode, each level is written to it's own file.

func (*FileHandler) Close

func (f *FileHandler) Close()

Close satisfies the interface, NOOP

func (*FileHandler) Handles

func (f *FileHandler) Handles(l LogLevel) bool

Handles returns whether the handler handles log level l.

func (*FileHandler) SetLevel

func (f *FileHandler) SetLevel(l LogLevel)

SetLevel will set both the minimum and maximum log levels to l. This makes the handler only respond to the single level l.

func (*FileHandler) SetMaxLevel

func (f *FileHandler) SetMaxLevel(l LogLevel)

SetMaxLevel will set the maximum log level the handler will handle.

func (*FileHandler) SetMinLevel

func (f *FileHandler) SetMinLevel(l LogLevel)

SetMinLevel will set the minimum log level the handler will handle.

func (*FileHandler) WriteLog

func (f *FileHandler) WriteLog(l LogLevel, name, msg string)

WriteLog will write the log message to a file.

type Handler

type Handler interface {
	// Handles returns if it wants to handle a particular log level
	// This can be used to suppress the higher log levels in production
	Handles(level LogLevel) bool

	// WriteLog actually logs the message using any system the Handler wishes.
	// The Handler must accept a LogLevel l which can be used for furthur processing
	// of specific levels, the name of the logger, and the log message.
	WriteLog(l LogLevel, name, message string)

	// Close is used to give a handler a chance to close any open resources
	Close()
}

A Handler is an object that can be used by the Logger to log a message

type LogLevel

type LogLevel int

LogLevel is used to compare levels in a consistant manner

const (
	LogLevelDebug LogLevel = iota
	LogLevelInfo
	LogLevelNotice
	LogLevelWarning
	LogLevelError
	LogLevelCritical
	LogLevelAlert
	LogLevelEmergency
	LogLevelFatal
	LogLevelCustom
)

These are the defined log levels

func (LogLevel) String

func (l LogLevel) String() string

String returns the stringified version of LogLevel. I.e., "Error" for LogLevelError, and "Debug" for LogLevelDebug It will return an empty string for any undefined level.

type Logger

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

A Logger takes a message and writes it to as many handlers as possible

func Classic

func Classic(n, path string) (*Logger, error)

Classic creates a logger with both the StdoutHandler and FileHandlers already added. If path is "", the FileHandler is not added. This is meant for convenience. The handlers use their default min and max levels. The StdoutHandler is named "stdout" and the FileHandler is named "file"

func Get

func Get(n string) *Logger

Get returns an existing logger with name n or a new Logger if one doesn't exist. To ensure Loggers are never overwritten, it may be safer to always use this method.

func New

func New(n string) *Logger

New will create a new Logger with name n. If with the same name already exists, it will be replaced with the new logger.

func (*Logger) AddHandler

func (l *Logger) AddHandler(n string, h Handler)

AddHandler will add Handler h to the logger named n. If a handler with the same name already exists, it will be overwritten.

func (*Logger) Alert

func (l *Logger) Alert(m string)

Alert - Log Alert message

func (*Logger) Alertf

func (l *Logger) Alertf(m string, v ...interface{})

Alertf - Log formatted Alert message

func (*Logger) Close

func (l *Logger) Close()

Close calls Close() on all the handlers then removes itself from the logger registry

func (*Logger) Critical

func (l *Logger) Critical(m string)

Critical - Log Critical message

func (*Logger) Criticalf

func (l *Logger) Criticalf(m string, v ...interface{})

Criticalf - Log formatted Critical message

func (*Logger) Debug

func (l *Logger) Debug(m string)

Debug - Log Debug message

func (*Logger) Debugf

func (l *Logger) Debugf(m string, v ...interface{})

Debugf - Log formatted Debug message

func (*Logger) Emergency

func (l *Logger) Emergency(m string)

Emergency - Log Emergency message

func (*Logger) Emergencyf

func (l *Logger) Emergencyf(m string, v ...interface{})

Emergencyf - Log formatted Emergency message

func (*Logger) Error

func (l *Logger) Error(m string)

Error - Log Error message

func (*Logger) Errorf

func (l *Logger) Errorf(m string, v ...interface{})

Errorf - Log formatted Error message

func (*Logger) Fatal

func (l *Logger) Fatal(m string)

Fatal - Log Fatal message

func (*Logger) Fatalf

func (l *Logger) Fatalf(m string, v ...interface{})

Fatalf - Log formatted Fatal message

func (*Logger) GetHandler

func (l *Logger) GetHandler(n string) Handler

GetHandler will return handler with name n or nil if it doesn't exist.

func (*Logger) Info

func (l *Logger) Info(m string)

Info - Log Info message

func (*Logger) Infof

func (l *Logger) Infof(m string, v ...interface{})

Infof - Log formatted Info message

func (*Logger) Log

func (l *Logger) Log(level LogLevel, msg string)

Log is the generic function to log a message with the handlers. All other logging functions are simply wrappers around this.

func (*Logger) Name

func (l *Logger) Name() string

Name returns the name of the logger

func (*Logger) Notice

func (l *Logger) Notice(m string)

Notice - Log Notice message

func (*Logger) Noticef

func (l *Logger) Noticef(m string, v ...interface{})

Noticef - Log formatted Notice message

func (*Logger) RemoveHandler

func (l *Logger) RemoveHandler(n string)

RemoveHandler will remove the handler named n.

func (*Logger) Warning

func (l *Logger) Warning(m string)

Warning - Log Warning message

func (*Logger) Warningf

func (l *Logger) Warningf(m string, v ...interface{})

Warningf - Log formatted Warning message

type StdoutHandler

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

StdoutHandler writes log message to standard out It even uses color!

func NewStdoutHandler

func NewStdoutHandler() *StdoutHandler

NewStdoutHandler creates a new StdoutHandler, surprise!

func (*StdoutHandler) Close

func (s *StdoutHandler) Close()

Close satisfies the interface, NOOP

func (*StdoutHandler) Handles

func (s *StdoutHandler) Handles(l LogLevel) bool

Handles returns whether the handler handles log level l.

func (*StdoutHandler) SetLevel

func (s *StdoutHandler) SetLevel(l LogLevel)

SetLevel will set both the minimum and maximum log levels to l. This makes the handler only respond to the single level l.

func (*StdoutHandler) SetMaxLevel

func (s *StdoutHandler) SetMaxLevel(l LogLevel)

SetMaxLevel will set the maximum log level the handler will handle.

func (*StdoutHandler) SetMinLevel

func (s *StdoutHandler) SetMinLevel(l LogLevel)

SetMinLevel will set the minimum log level the handler will handle.

func (*StdoutHandler) WriteLog

func (s *StdoutHandler) WriteLog(l LogLevel, name, msg string)

WriteLog writes the log message to standard output

Jump to

Keyboard shortcuts

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