daslog

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 9, 2016 License: BSD-2-Clause Imports: 8 Imported by: 1

README

DASLOG

Daslog is a simple logger package for Go (golang) programms.

DAmn Simple LOGger.

go get -u github.com/iu0v1/daslog
Why?

Yes. This is yet another logger. I made this package, because any other logger that I met - is a monster. They can do a lot of tricks, with one exception - a simple and transparent logging. Sometimes, you need only logging, without the possibility of launching a space shuttle through the services of Amazon or Google.

Prefix format

You can use some GNU date format commands in prefix line.

'GNU date' compatible format commands:

{{.F}} - full date                               (2016-01-04)
{{.T}} - time                                    (16:52:36)
{{.r}} - 12-hour clock time                      (11:11:04 PM)
{{.Y}} - year                                    (2016)
{{.y}} - last two digits of year                 (00..99)
{{.m}} - month                                   (01..12)
{{.b}} - locale's abbreviated month name         (Jan)
{{.B}} - locale's full month name                (January)
{{.d}} - day of month                            (01)
{{.a}} - locale's abbreviated weekday name       (Sun)
{{.A}} - locale's full weekday name              (Sunday)
{{.H}} - 24-hour clock hour                      (00..23)
{{.I}} - 12-hour clock hour                      (01..12)
{{.M}} - minute                                  (00..59)
{{.S}} - second                                  (00..60)
{{.p}} - locale's equivalent of either AM or PM  (AM)

Incompatible format commands:

{{.O}} - same as {{.F}} + {{.T}}                 (2016-01-04 16:52:36)
{{.Q}} - message urgency level                   (info, critical, etc)

Simple example
package main

import (
	"fmt"
	"os"

	"github.com/iu0v1/daslog"
)

func main() {
	o := daslog.Options{
		Destination: os.Stdout,
		Prefix:      "{{.O}} [{{.Q}}]: ",
		LogLevel:    daslog.UrgencyLevelCritical,
	}

	l, err := daslog.New(o)
	if err != nil {
		fmt.Print(err)
		return
	}

	// notice in Log style
	l.Log(daslog.UrgencyLevelNotice, "test notice message")

	// info
	l.Info("test info message")

	// error
	l.Errorf("%s %s %s", "test", "error", "message")
}

Output:

2016-01-04 21:16:41 [notice]: test notice message
2016-01-04 21:16:41 [info]: test info message
2016-01-04 21:16:42 [error]: test error message
Performance

Compare with a log package.

Daslog equal to a log, when you use a pure prefix; in ~2x slower, if you use format options in a prefix; in ~2.5x slower if you use {{.Q}} option in a prefix. Avoid to use Daslog, if you need high performance.

BenchmarkLog-4                         2000000       836 ns/op       137 B/op       2 allocs/op
BenchmarkDaslogPurePrefix-4            2000000       704 ns/op       106 B/op       4 allocs/op
BenchmarkDaslogTemplatePrefix-4        1000000      1369 ns/op       199 B/op       5 allocs/op
BenchmarkDaslogTemplatePrefixQ-4       1000000      1792 ns/op       270 B/op       7 allocs/op
DOC

For more infomation, please look at the examples and read the doc.

Documentation

Overview

Package daslog is a simple logger package.

Explanation of the name: DAmn Simple LOGger.

Simple example:

func main() {
	o := daslog.Options{
        Destination: os.Stdout,
        Prefix:      "{{.O}} [{{.Q}}]: ",
        LogLevel:    daslog.UrgencyLevelCritical,
	}

	l, err := daslog.New(o)
	if err != nil {
		fmt.Print(err)
		return
	}

	l.Log(daslog.UrgencyLevelNotice, "test notice message")

	l.Info("test info message")

	l.Errorf("%s %s %s", "test", "error", "message")
}

Output:

2016-01-04 21:16:41 [notice]: test notice message
2016-01-04 21:16:41 [info]: test info message
2016-01-04 21:16:42 [error]: test error message

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Daslog

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

Daslog - main struct.

func New

func New(options Options) (*Daslog, error)

New - return a new copy of Daslog.

func (*Daslog) Critical

func (l *Daslog) Critical(message string)

Critical - print "critical" level message to log.

func (*Daslog) Criticalf

func (l *Daslog) Criticalf(message string, a ...interface{})

Criticalf - same as a Critical, but in printf style.

func (*Daslog) Error

func (l *Daslog) Error(message string)

Error - print "error" level message to log.

func (*Daslog) Errorf

func (l *Daslog) Errorf(message string, a ...interface{})

Errorf - same as a Error, but in printf style.

func (*Daslog) Info

func (l *Daslog) Info(message string)

Info - print "info" level message to log.

func (*Daslog) Infof

func (l *Daslog) Infof(message string, a ...interface{})

Infof - same as a Info, but in printf style.

func (*Daslog) Log

func (l *Daslog) Log(urgencyLevel UrgencyLevel, message string)

Log - print message to log.

func (*Daslog) Logf

func (l *Daslog) Logf(urgencyLevel UrgencyLevel, message string, a ...interface{})

Logf - print message to log in printf style :)

func (*Daslog) Notice

func (l *Daslog) Notice(message string)

Notice - print "notice" level message to log.

func (*Daslog) Noticef

func (l *Daslog) Noticef(message string, a ...interface{})

Noticef - same as a Notice, but in printf style.

type LogHandler

type LogHandler func(urgencyLevel UrgencyLevel, message string)

LogHandler - log handler type.

type Options

type Options struct {
	// LogLevel provides the opportunity to choose the level of
	// information messages.
	// Each level includes the messages from the previous level.
	// UrgencyLevelNone     - no messages // 0
	// UrgencyLevelNotice   - notice      // 1
	// UrgencyLevelInfo     - info        // 2
	// UrgencyLevelError    - error       // 3
	// UrgencyLevelCritical - critical    // 4
	//
	// Default: 'UrgencyLevelNone'.
	LogLevel UrgencyLevel

	// Prefix provides the ability to add prefix text to all log messages.
	// In addition, you can use commands to display the date and time (these
	// format commands are based on the linux 'GNU date' format syntax).
	//
	// 'GNU date' compatible format commands:
	//
	// {{.F}} - full date                               (2016-01-04)
	// {{.T}} - time                                    (16:52:36)
	// {{.r}} - 12-hour clock time                      (11:11:04 PM)
	//
	// {{.Y}} - year                                    (2016)
	// {{.y}} - last two digits of year                 (00..99)
	// {{.m}} - month                                   (01..12)
	// {{.b}} - locale's abbreviated month name         (Jan)
	// {{.B}} - locale's full month name                (January)
	// {{.d}} - day of month                            (01)
	// {{.a}} - locale's abbreviated weekday name       (Sun)
	// {{.A}} - locale's full weekday name              (Sunday)
	// {{.H}} - 24-hour clock hour                      (00..23)
	// {{.I}} - 12-hour clock hour                      (01..12)
	// {{.M}} - minute                                  (00..59)
	// {{.S}} - second                                  (00..60)
	// {{.p}} - locale's equivalent of either AM or PM  (AM)
	//
	// Incompatible format commands:
	//
	// {{.O}} - same as {{.F}} + {{.T}}                 (2016-01-04 16:52:36)
	// {{.Q}} - message urgency level                   (info, critical, etc)
	Prefix string

	// Destination provides the opportunity to choose the own
	// destination for log messages (errors, info, etc).
	//
	// Default: 'os.Stdout'.
	Destination io.Writer

	// Destinations provides the opportunity to choose several destinations
	// for delivery of log messages.
	//
	// If 'Destinations' is selected - 'Destination' will be ignored.
	Destinations []io.Writer

	// LogHandler takes a log messages to bypass the internal
	// mechanism of the message processing.
	//
	// If LogHandler is selected - all log settings will be ignored.
	Handler LogHandler
}

Options - struct, which is used to configure a Daslog.

type UrgencyLevel

type UrgencyLevel int

UrgencyLevel - declare the level of informativity of log message.

const (
	UrgencyLevelNone UrgencyLevel = iota
	UrgencyLevelNotice
	UrgencyLevelInfo
	UrgencyLevelError
	UrgencyLevelCritical
)

predefined UrgencyLevel levels

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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