log

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

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

Go to latest
Published: Nov 28, 2016 License: MIT Imports: 9 Imported by: 4

README

log

go logging library

godoc

package main

import (
	"encoding/json"
	"os"

	"github.com/nuuls/log"
)

func main() {
	logger := &log.Logger{
		Level:        log.LevelDebug,
		Stdout:       os.Stdout,
		Stderr:       os.Stderr,
		DefaultLevel: log.LevelDebug,
		Color:        true,
	}
	log.AddLogger(logger)

	file, err := os.OpenFile("test.log", os.O_CREATE|os.O_APPEND, 666)
	if err != nil {
		log.Critical(err)
	}
	var jsonLogger *log.Logger
	jsonLogger = &log.Logger{
		Level:        log.LevelError,
		Stdout:       file,
		Stderr:       file,
		DefaultLevel: log.LevelError,
		Marshal:      json.Marshal,
		LogFunc: func(m *log.Message) error {
			err := jsonLogger.DefaultLogFunc(m)
			if err != nil {
				logger.Log(log.NewMessage(log.LevelCritical, "cannot write to file test.log:", err))
			}
			return nil
		},
	}
	log.AddLogger(jsonLogger)
	log.Debug("debug")
	log.Infof("%s infof", "test")
	log.Error("error")
	log.Fatal("fatal")
}

console output:

test.log:

{"level":2,"levelString":"ERRO","time":"2016-10-24T14:18:08.483591+02:00","caller":"test/test.go:42","text":"error"}
{"level":0,"levelString":"FATAL","time":"2016-10-24T14:18:08.4840918+02:00","caller":"test/test.go:43","text":"fatal"}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// TimeFormat is the Default Time Format
	TimeFormat = "2006-01-02 15:04:05.000"

	Stack Level = LevelError

	Loggers = []*Logger{}

	CallerStrLen = 35
)
View Source
var DefaultLogger = &Logger{
	Stdout:       os.Stdout,
	Stderr:       os.Stderr,
	Level:        LevelDebug,
	DefaultLevel: LevelDebug,
	Color:        true,
}

Functions

func AddLogger

func AddLogger(l *Logger)

AddLogger appends a logger

func Caller

func Caller(skip int) caller

func Critical

func Critical(a ...interface{})

func Criticalf

func Criticalf(format string, a ...interface{})

func Debug

func Debug(a ...interface{})

func Debugf

func Debugf(format string, a ...interface{})

func Error

func Error(a ...interface{})

func Errorf

func Errorf(format string, a ...interface{})

func Fatal

func Fatal(a ...interface{})

func Fatalf

func Fatalf(format string, a ...interface{})

func Import

func Import() struct{}

Import can be used to prevent goimports from importing "log" var _ = log.Import()

func Info

func Info(a ...interface{})

func Infof

func Infof(format string, a ...interface{})

func Warning

func Warning(a ...interface{})

func Warningf

func Warningf(format string, a ...interface{})

Types

type Level

type Level int

Level xd

const (
	LevelFatal Level = iota
	LevelCritical
	LevelError
	LevelWarning
	LevelInfo
	LevelDebug
)

Available Levels

func (Level) String

func (l Level) String() string

type Logger

type Logger struct {
	Stdout       io.Writer
	Stderr       io.Writer
	Level        Level
	DefaultLevel Level
	Color        bool
	Marshal      MarshalFunc
	LogFunc      LoggerFunc
}

func (*Logger) DefaultLogFunc

func (l *Logger) DefaultLogFunc(m *Message) error

func (*Logger) Log

func (l *Logger) Log(m *Message) error

func (*Logger) Write

func (l *Logger) Write(bs []byte) (int, error)

Write always returns 0, nil

type LoggerFunc

type LoggerFunc func(m *Message) error

type MarshalFunc

type MarshalFunc func(interface{}) ([]byte, error)

type Message

type Message struct {
	Service     string                 `json:"service"`
	Level       Level                  `json:"level"`
	LevelString string                 `json:"levelString"`
	Time        time.Time              `json:"time"`
	Caller      string                 `json:"caller"`
	Stack       string                 `json:"stack,omitempty"`
	Text        string                 `json:"text"`
	Fields      map[string]interface{} `json:"fields,omitempty"`
	// contains filtered or unexported fields
}

func NewMessage

func NewMessage(level Level, a ...interface{}) *Message

func (*Message) String

func (m *Message) String() string

Jump to

Keyboard shortcuts

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