picolog

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

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

Go to latest
Published: Mar 28, 2014 License: BSD-3-Clause Imports: 6 Imported by: 5

README

picolog

Tiny levelled logging framework for go.

Documentation

http://godoc.org/github.com/anchor/picolog

Example

package picolog_test

import (
	"github.com/anchor/picolog"
	"os"
	"fmt"
)

func Example() {
	level, err := picolog.ParseLogLevel("info")
	if err != nil {
		fmt.Printf("This can't happen: %v", err)
	}
	// Log messages will be prefixed by "[example]" and a timestamp.
	logger := picolog.NewLogger(level, "example", os.Stdout)
	logger.Infof("Printing a log message!")
	logger.Debugf("Not printing this message at INFO log level.")
}

func ExampleSubLogger() {
	level, err := picolog.ParseLogLevel("info")
	if err != nil {
		fmt.Printf("This can't happen: %v", err)
	}
	logger := picolog.NewLogger(level, "things", os.Stdout)
	logger.Infof("Things!")
	subLogger := logger.NewSubLogger("related-things")
	subLogger.Infof("This will be prefixed by [things][related-things] <timestamp>.")
}

Documentation

Overview

picolog is a tiny levelled logging package for go. It supports syslog log levels, subloggers, and not much else. Written because all the existing solutions either didn't do what I needed or were too weighty.

Example
package main

import (
	"fmt"
	"github.com/anchor/picolog"
	"os"
)

func main() {
	level, err := picolog.ParseLogLevel("info")
	if err != nil {
		fmt.Printf("This can't happen: %v", err)
	}
	// Log messages will be prefixed by "[example]" and a timestamp.
	logger := picolog.NewLogger(level, "example", os.Stdout)
	logger.Infof("Printing a log message!")
	logger.Debugf("Not printing this message at INFO log level.")
}
Output:

Index

Examples

Constants

Variables

This section is empty.

Functions

This section is empty.

Types

type LogLevel

type LogLevel syslog.Priority

LogLevel is a type representing the usual syslog log levels from LOG_DEBUG to LOG_EMERG. It does not reflect the go syslog package's concept of 'Priority'.

func ParseLogLevel

func ParseLogLevel(level string) (LogLevel, error)

ParseLogLevel takes a string and returns a LogLevel according to the standard syslog string representation. Not case-sensitive.

func (LogLevel) String

func (l LogLevel) String() string

String returns the default (lowercase) string representation of a LogLevel.

type Logger

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

Logger is a leveled logger type. It can be a sublogger of another logger, and have an arbitrary number of subloggers itself.

func NewDefaultLogger

func NewDefaultLogger() *Logger

NewDefaultLogger returns a picolog.Logger initialized with workable defaults (outputs to stderr, prefix "default", priority DEBUG). Useful as a fallback when a logger hasn't been initialized.

func NewLogger

func NewLogger(logLevel LogLevel, subpackage string, dest *os.File) *Logger

Return a new Logger. logLevel is a syslog log level, subpackage is used to construct the log prefix, and dest is where to write the log to.

func (*Logger) Alertf

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

Alertf logs one printf-formatted message at LOG_ALERT.

func (*Logger) Debugf

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

Debugf logs one printf-formatted message at LOG_DEBUG.

func (*Logger) Emergf

func (l *Logger) Emergf(format string, v ...interface{})

Emergf logs one printf-formatted message at LOG_EMERG.

func (*Logger) Errorf

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

Errorf logs one printf-formatted message at LOG_ERR.

func (*Logger) Fatalf

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

Fatalf logs one printf-formatted message at LOG_CRIT, and then exits with an error code.

func (*Logger) Infof

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

Infof logs one printf-formatted message at LOG_INFO.

func (*Logger) NewSubLogger

func (l *Logger) NewSubLogger(prefix string) *Logger

NewSubLogger returns a Logger writing to the same stream, with a prefix constructed from the provided prefix and the parent Logger's prefix. Subloggers can be nested.

func (*Logger) Noticef

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

Noticef logs one printf-formatted message at LOG_NOTICE.

func (*Logger) Printf

func (l *Logger) Printf(format string, level LogLevel, v ...interface{})

Printf is the lowest-level output function of our Logger. Will use a default logger if l is not initialized.

func (*Logger) Warningf

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

Warningf logs one printf-formatted message at LOG_WARNING.

Jump to

Keyboard shortcuts

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