gol

package module
Version: v0.0.0-...-2d8c917 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2016 License: BSD-3-Clause Imports: 7 Imported by: 1

README

gol Build Status GoDoc Coverage Status

Simple logging framework for Go applications, inspired by SLF4J.

Introduction

gol (or golog) provides a generic logging API and a simple implementation which supports logging level and hierarchy.

The Logger interface is kept minimal and does not allow you to set level directly but the DefaultLogger, its default implementation, does. You can also create a Logger hierarchy with the DefaultLogger. For example, logger a/b/c will inherit logging level and appender from logger a/b unless its own properties are set.

Example

See example/example.go

package main

import (
	"os"
	"time"

	"github.com/goburrow/gol"
)

var exampleLogger, appLogger gol.Logger

func init() {
	// Get logger with name "app/example"
	exampleLogger = gol.GetLogger("app/example")
	// Logger "app" is the parent of the logger "app/example"
	appLogger = gol.GetLogger("app")
}

func main() {
	exampleLogger.Infof("Running app with arguments: %v.", os.Args)

	exampleLogger.Warnf("Going to do nothing.")
	time.Sleep(1 * time.Second)

	// DefaultLogger is the internal implementation of Logger
	appLogger.(*gol.DefaultLogger).SetLevel(gol.Warn)

	exampleLogger.Infof("You won't see this message.")
	appLogger.Errorf("I %v! %[2]v %[2]v.", "quit", "bye")

	// Output:
	// INFO  [2015-01-14T12:43:35.546+10:00] app/example: Running app with arguments: [/go/bin/example].
	// WARN  [2015-01-14T12:43:35.546+10:00] app/example: Going to do nothing.
	// ERROR [2015-01-14T12:43:36.546+10:00] app: I quit! bye bye.
}

Documentation

Overview

Package gol provides a simple logging framework, inspired by SLF4J.

Index

Constants

View Source
const (
	// RootLoggerName is the name of the root logger.
	RootLoggerName = "root"
)

Variables

This section is empty.

Functions

func LevelString

func LevelString(level Level) string

LevelString returns the text for the level.

func Print

func Print(args ...interface{})

Print prints to standard error, used for debugging.

func SetDebugMode

func SetDebugMode(val bool)

SetDebugMode sets debug mode in gol package.

Types

type Appender

type Appender interface {
	Append(*LoggingEvent)
}

Appender appends contents to a Writer.

type DefaultAppender

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

DefaultAppender implements Appender interface.

func NewAppender

func NewAppender(target io.Writer) *DefaultAppender

NewAppender allocates and returns a new DefaultAppender.

func (*DefaultAppender) Append

func (appender *DefaultAppender) Append(event *LoggingEvent)

Append uses its encoder to send the event to the target.

type DefaultFactory

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

DefaultFactory implements Factory interface.

func NewFactory

func NewFactory(writer io.Writer) *DefaultFactory

NewFactory allocates and returns new DefaultFactory.

func (*DefaultFactory) GetLogger

func (factory *DefaultFactory) GetLogger(name string) Logger

GetLogger returns a new Logger or an existing one if the same name is found.

type DefaultLogger

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

DefaultLogger implements Logger interface.

func New

func New(name string, parent *DefaultLogger) *DefaultLogger

New allocates and returns a new DefaultLogger. This method should not be called directly in application, use LoggerFactory.GetLogger() instead as a DefaultLogger requires Appender from itself or its parent.

func (*DefaultLogger) Appender

func (logger *DefaultLogger) Appender() Appender

Appender returns appender of this logger or parent if not set.

func (*DefaultLogger) DebugEnabled

func (logger *DefaultLogger) DebugEnabled() bool

DebugEnabled checks if Debug level is enabled.

func (*DefaultLogger) Debugf

func (logger *DefaultLogger) Debugf(format string, args ...interface{})

Debugf logs message at Debug level.

func (*DefaultLogger) ErrorEnabled

func (logger *DefaultLogger) ErrorEnabled() bool

ErrorEnabled checks if Error level is enabled.

func (*DefaultLogger) Errorf

func (logger *DefaultLogger) Errorf(format string, args ...interface{})

Errorf logs message at Error level.

func (*DefaultLogger) InfoEnabled

func (logger *DefaultLogger) InfoEnabled() bool

InfoEnabled checks if Info level is enabled.

func (*DefaultLogger) Infof

func (logger *DefaultLogger) Infof(format string, args ...interface{})

Infof logs message at Info level.

func (*DefaultLogger) Level

func (logger *DefaultLogger) Level() Level

Level returns level of this logger or parent if not set.

func (*DefaultLogger) Printf

func (logger *DefaultLogger) Printf(level Level, format string, args []interface{})

log performs logging with given parameters.

func (*DefaultLogger) SetAppender

func (logger *DefaultLogger) SetAppender(appender Appender)

SetAppender changes appender of this logger.

func (*DefaultLogger) SetLevel

func (logger *DefaultLogger) SetLevel(level Level)

SetLevel changes logging level of this logger.

func (*DefaultLogger) TraceEnabled

func (logger *DefaultLogger) TraceEnabled() bool

TraceEnabled checks if Trace level is enabled.

func (*DefaultLogger) Tracef

func (logger *DefaultLogger) Tracef(format string, args ...interface{})

Tracef logs message at Trace level.

func (*DefaultLogger) WarnEnabled

func (logger *DefaultLogger) WarnEnabled() bool

WarnEnabled checks if Warning level is enabled.

func (*DefaultLogger) Warnf

func (logger *DefaultLogger) Warnf(format string, args ...interface{})

Warnf logs message at Warning level.

type Factory

type Factory interface {
	GetLogger(name string) Logger
}

Factory produces Logger.

type Level

type Level int

Level represents logging level.

const (
	Uninitialized Level = iota
	All
	Trace
	Debug
	Info
	Warn
	Error
	Off
)

Log levels

type Logger

type Logger interface {
	Tracef(string, ...interface{})
	TraceEnabled() bool
	Debugf(string, ...interface{})
	DebugEnabled() bool
	Infof(string, ...interface{})
	InfoEnabled() bool
	Warnf(string, ...interface{})
	WarnEnabled() bool
	Errorf(string, ...interface{})
	ErrorEnabled() bool
}

Logger specifies how logging in application is done.

var (
	NOPLogger Logger = (*nopLogger)(nil)
)

func GetLogger

func GetLogger(name string) Logger

GetLogger returns Logger in the default logger factory.

type LoggingEvent

type LoggingEvent struct {
	// Name is the of the logger.
	Name string
	// Level is the current logger level.
	Level Level
	// Time is when the logging happens.
	Time time.Time

	Message bytes.Buffer
}

LoggingEvent is the representation of logging events.

Directories

Path Synopsis
Package async provides an asynchronous appender for gol.
Package async provides an asynchronous appender for gol.
Package file provides logging to file.
Package file provides logging to file.
rotation
Package rotation provides triggering and rolling policy for system files.
Package rotation provides triggering and rolling policy for system files.
Package filter provides an appender which has filter.
Package filter provides an appender which has filter.
Package log provides a bridge of default go log to gol.
Package log provides a bridge of default go log to gol.
Package syslog provides logging to syslog.
Package syslog provides logging to syslog.

Jump to

Keyboard shortcuts

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