logpher

package module
v0.0.0-...-cc09070 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2020 License: MIT Imports: 7 Imported by: 2

README

logpher Documentation

logpher is a basic autumn-enabled golang logger package with various writers.

Configuration

Logpher is built around the concept of named loggers. Each logger has its own level, which can be specified via configuration. Additionally, Logpher supports 3 writers out of the box:

  • A combination writer
  • A console writer
  • A file writer
  • A rolling file writer

All of these settings are controlled via a configuration object:

config := &logpher.Configuration{
    Type:       "console",          // This can be "combination", "console", "file", or "rolling"
    Combine:    "console,rolling"   // The writers to combine when using the "combination" type
    File:       "./mylog.txt",      // The name of the file to log to when the type is "file" or "rolling"        
    Size:       8,                  // The maximum log file size in MB when the type is "rolling"
    Count:      5,                  // The number of files to keep when the type is "rolling"
    Levels: map[string]string{      // The levels to use for the various loggers
    	"default": "info",          // The default log level for new loggers
    	"main": "debug",            // Overrides the log level for the "main" logger
    }
}

Standard Usage

Standard usage is as simple as initializing Logpher and creating a logger:

// Initialize Logpher
l := logpher.New(config)

// Create a logger
mainLogger := l.NewLogger("main")

// Log something
mainLogger.Trace("something")
mainLogger.Debug("something")
mainLogger.Info("something")
mainLogger.Warn("something")
mainLogger.Error("something")

// Var args will be concatenated with spaces
mainLogger.Debug("something", "happened")

// Close open files
l.Close()

Autumn Usage

Logpher is designed to work nicely with Autumn:

// Initialize the autumn tree
tree := autumn.NewTree()

// Add the configuration object
tree.AddNamedLeaf("logConfiguration", config)

// Add the main logpher leaf
tree.AddLeaf(&logpher.Logpher{})

// Add loggers
tree.AddLeaf(logpher.NewLogger("main"))

// The logger will be wired into the following structure automatically
type main struct {
	Logger *logpher.Logger `autumn:"mainLogger"`
}

// The log file will be closed when the tree is chopped
tree.Chop()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Trace = &level{0, traceString, color.WhiteString}
	Debug = &level{1, debugString, color.BlueString}
	Info  = &level{2, infoString, color.CyanString}
	Warn  = &level{3, warnString, color.YellowString}
	Error = &level{4, errString, color.RedString}
	Fatal = &level{5, fatalString, color.HiRedString}
	Off   = &level{6, offString, nil}
)

Functions

This section is empty.

Types

type Configuration

type Configuration struct {
	Type    string // The main writer type
	Combine string // A comma separated string indicating which loggers to combine when using a combination writer
	File    string // The file path for file-based writers
	Size    int    // The maximum size in bytes for the rolling writer
	Count   int    // The maximum file count for the rolling writer
	Levels  map[string]string
	// contains filtered or unexported fields
}

Configuration defines the configuration structure for logging

func NewConfiguration

func NewConfiguration() *Configuration

NewConfiguration creates a new configuration object

type Logger

type Logger struct {
	Logpher *Logpher `autumn:"logpher"`
	// contains filtered or unexported fields
}

Logger defines a logger structure

func NewLogger

func NewLogger(name string) *Logger

NewLogger creates a new logger using the autumn Logpher instance configuration

func (*Logger) Debug

func (l *Logger) Debug(data ...interface{})

Debug logs at the debug level

func (*Logger) Error

func (l *Logger) Error(data ...interface{})

Error logs at the error level

func (*Logger) Fatal

func (l *Logger) Fatal(data ...interface{})

Fatal logs at the fatal level

func (*Logger) GetLeafName

func (l *Logger) GetLeafName() string

GetLeafName gets the autumn leaf name

func (*Logger) Info

func (l *Logger) Info(data ...interface{})

Info logs at the info level

func (*Logger) LevelEnabled

func (l *Logger) LevelEnabled(level *level) bool

LevelEnabled determines if logs at the specified level will be written by this logger

func (*Logger) PostConstruct

func (l *Logger) PostConstruct()

PostConstruct initializes the logger when it's used as an autumn leaf

func (*Logger) Trace

func (l *Logger) Trace(data ...interface{})

Trace logs at the trace level

func (*Logger) Warn

func (l *Logger) Warn(data ...interface{})

Warn logs at the warn level

type Logpher

type Logpher struct {
	Configuration *Configuration `autumn:"logConfiguration"`
}

Logpher defines the main logging structure

func New

func New(configuration *Configuration) *Logpher

New creates a new logpher instance with the supplied configuration

func (*Logpher) Close

func (l *Logpher) Close()

Close closes the log writer

func (*Logpher) GetLeafName

func (l *Logpher) GetLeafName() string

GetLeafName gets the autumn leaf name

func (*Logpher) NewLogger

func (l *Logpher) NewLogger(name string) *Logger

NewLogger constructs the a new logger with the specified name

func (*Logpher) PostConstruct

func (l *Logpher) PostConstruct()

PostConstruct enables autumn post construct functionality

func (*Logpher) PreDestroy

func (l *Logpher) PreDestroy()

PreDestroy enables autumn pre destroy functionality

Jump to

Keyboard shortcuts

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