log

package module
Version: v3.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: May 19, 2018 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-log

GoDoc Build Status Coverage Status Go Report Card License

Logging package similar to log4j for the Golang.

  • Support dynamic log level
  • Support customized formatter
    • TextFormatter
    • JSONFormatter
  • Support multiple rolling file writers
    • FixedSizeFileWriter
    • DailyFileWriter
    • AlwaysNewFileWriter

Installation

$ go get github.com/subchen/go-log

Usage

package main

import (
	"os"
	"errors"
	"github.com/subchen/go-log"
)

func main() {
	log.Debugf("app = %s", os.Args[0])
	log.Errorf("error = %v", errors.New("some error"))

	// dynamic set level
	log.Default.Level = log.WARN

	log.Debug("cannot output debug message")
	log.Errorln("can output error message", errors.New("some error"))
}
Output

Default log to console, you can set Logger.Out to set a file writer into log.

import (
	"github.com/subchen/go-log"
	"github.com/subchen/go-log/writers"
)

log.Default.Out = &writers.FixedSizeFileWriter{
	Name:	 "/tmp/test.log",
	MaxSize:  10 * 1024 * 1024, // 10m
	MaxCount: 10,
})

Three builtin writers for use

// Create log file if file size large than fixed size (10m)
// files: /tmp/test.log.0 .. test.log.10
&writers.FixedSizeFileWriter{
	Name:	 "/tmp/test.log",
	MaxSize:  10 * 1024 * 1024, // 10m
	MaxCount: 10,
}

// Create log file every day.
// files: /tmp/test.log.20160102
&writers.DailyFileWriter{
	Name: "/tmp/test.log",
	MaxCount: 10,
}

// Create log file every process.
// files: /tmp/test.log.20160102_150405
&writers.AlwaysNewFileWriter{
	Name: "/tmp/test.log",
	MaxCount: 10,
}

// Output to multiple writes
io.MultiWriter(
	os.Stdout,
	&writers.DailyFileWriter{
		Name: "/tmp/test.log",
		MaxCount: 10,
	}
	//...
)
Formatter
import (
	"github.com/subchen/go-log"
	"github.com/subchen/go-log/formatters"
)

log.Default.Formatter = new(formatters.TextFormatter)
New Logger instance
import (
	"github.com/subchen/go-log"
)

func main() {
	logger := &log.Logger{
		Level:     log.INFO,
		Formatter: new(formatters.JSONFormatter),
		Out:       os.Stdout,
	}

	logger.Infof("i = %d", 99)
}

LICENSE

Apache 2.0

Documentation

Overview

Package log is a simple and configurable Logging in Go, with level, formatters and writers.

It is completely API compatible with the standard library logger.

The simplest way to use log is simply the package-level exported logger:

package main

import (
	"os"
	"github.com/subchen/go-log"
)

func main() {
	log.Print("some message")
	log.Infof("$HOME = %v", os.Getenv("HOME"))
	log.Errorln("Got err:", os.ErrPermission)
}

Output:

07:34:23.039 INFO some message
07:34:23.039 INFO $HOME = /home/subchen
07:34:23.039 ERROR Got err: permission denied

You also can config `log.Default` or new `log.Logger` to customize formatter and writer.

package main

import (
	"os"
	"github.com/subchen/go-log"
	"github.com/subchen/go-log/formatters"
	"github.com/subchen/go-log/writers"
)

func main() {
	logger := &log.Logger{
		Level:     log.INFO,
		Formatter: new(formatters.TextFormatter),
		Out:       &writers.FixedSizeFileWriter{
			Name:     "/tmp/test.log",
			MaxSize:  10 * 1024 * 1024, // 10m
			MaxCount: 10,
		},
	}

	logger.Info("some message")
}

Output log in `/tmp/test.log`:

2018-05-19T07:49:05.979+0000 INFO devbox main 9981 example/main.go:17 some message

For a full guide visit https://github.com/subchen/go-log

Index

Constants

This section is empty.

Variables

View Source
var Default = New()

Default is a default Logger instance

View Source
var Exit = os.Exit

Exit is equals os.Exit

Functions

func Debug

func Debug(obj ...interface{})

Debug outputs message, Arguments are handled by fmt.Sprint

func Debugf

func Debugf(msg string, args ...interface{})

Debugf outputs message, Arguments are handled by fmt.Sprintf

func Debugln

func Debugln(obj ...interface{})

Debugln outputs message, Arguments are handled by fmt.Sprintln

func Error

func Error(obj ...interface{})

Error outputs message, Arguments are handled by fmt.Sprint

func Errorf

func Errorf(msg string, args ...interface{})

Errorf outputs message, Arguments are handled by fmt.Sprintf

func Errorln

func Errorln(obj ...interface{})

Errorln outputs message, Arguments are handled by fmt.Sprintln

func Fatal

func Fatal(obj ...interface{})

Fatal outputs message, and followed by a call to os.Exit(1) Arguments are handled by fmt.Sprint

func Fatalf

func Fatalf(msg string, args ...interface{})

Fatalf outputs message and followed by a call to os.Exit(1), Arguments are handled by fmt.Sprintf

func Fatalln

func Fatalln(obj ...interface{})

Fatalln outputs message and followed by a call to os.Exit(1), Arguments are handled by fmt.Sprintln

func Info

func Info(obj ...interface{})

Info outputs message, Arguments are handled by fmt.Sprint

func Infof

func Infof(msg string, args ...interface{})

Infof outputs message, Arguments are handled by fmt.Sprintf

func Infoln

func Infoln(obj ...interface{})

Infoln outputs message, Arguments are handled by fmt.Sprintln

func IsDebugEnabled

func IsDebugEnabled() bool

IsDebugEnabled indicates whether output message

func IsDisabled

func IsDisabled() bool

IsDisabled indicates whether output message

func IsErrorEnabled

func IsErrorEnabled() bool

IsErrorEnabled indicates whether output message

func IsFatalEnabled

func IsFatalEnabled() bool

IsFatalEnabled indicates whether output message

func IsInfoEnabled

func IsInfoEnabled() bool

IsInfoEnabled indicates whether output message

func IsPanicEnabled

func IsPanicEnabled() bool

IsPanicEnabled indicates whether output message

func IsPrintEnabled

func IsPrintEnabled() bool

IsPrintEnabled indicates whether output message

func IsWarnEnabled

func IsWarnEnabled() bool

IsWarnEnabled indicates whether output message

func Panic

func Panic(obj ...interface{})

Panic outputs message, and followed by a call to panic() Arguments are handled by fmt.Sprint

func Panicf

func Panicf(msg string, args ...interface{})

Panicf outputs message and followed by a call to panic(), Arguments are handled by fmt.Sprintf

func Panicln

func Panicln(obj ...interface{})

Panicln outputs message and followed by a call to panic(), Arguments are handled by fmt.Sprintln

func Print

func Print(obj ...interface{})

Print outputs message, Arguments are handled by fmt.Sprint

func Printf

func Printf(msg string, args ...interface{})

Printf outputs message, Arguments are handled by fmt.Sprintf

func Println

func Println(obj ...interface{})

Println outputs message, Arguments are handled by fmt.Sprintln

func Warn

func Warn(obj ...interface{})

Warn outputs message, Arguments are handled by fmt.Sprint

func Warnf

func Warnf(msg string, args ...interface{})

Warnf outputs message, Arguments are handled by fmt.Sprintf

func Warnln

func Warnln(obj ...interface{})

Warnln outputs message, Arguments are handled by fmt.Sprintln

Types

type Formatter

type Formatter interface {
	Format(level Level, msg string, logger *Logger) []byte
}

Formatter is a interface used to implement a custom Formatter

type Level

type Level uint32

Level type

const (
	OFF Level = iota
	FATAL
	PANIC
	ERROR
	WARN
	INFO
	DEBUG
)

These are the different logging levels

func ParseLevel

func ParseLevel(name string) (Level, error)

ParseLevel takes a string level and returns the log level constant.

func (Level) ColorString

func (level Level) ColorString() string

ColorString converts the Level to a string with term colorful

func (Level) String

func (level Level) String() string

String converts the Level to a string

type LogInterface

type LogInterface interface {
	Debug(...interface{})
	Info(...interface{})
	Print(...interface{})
	Warn(...interface{})
	Error(...interface{})
	Panic(...interface{})
	Fatal(...interface{})

	Debugln(...interface{})
	Infoln(...interface{})
	Println(...interface{})
	Warnln(...interface{})
	Errorln(...interface{})
	Panicln(...interface{})
	Fatalln(...interface{})

	Debugf(string, ...interface{})
	Infof(string, ...interface{})
	Printf(string, ...interface{})
	Warnf(string, ...interface{})
	Errorf(string, ...interface{})
	Panicf(string, ...interface{})
	Fatalf(string, ...interface{})
}

LogInterface is interface for this logger

type Logger

type Logger struct {
	Level     Level
	Formatter Formatter
	Out       io.Writer
	// contains filtered or unexported fields
}

Logger is represents an active logging object

func New

func New() *Logger

New creates a new Logger

func (*Logger) Debug

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

Debug outputs message, Arguments are handled by fmt.Sprint

func (*Logger) Debugf

func (l *Logger) Debugf(msg string, args ...interface{})

Debugf outputs message, Arguments are handles by fmt.Sprintf

func (*Logger) Debugln

func (l *Logger) Debugln(obj ...interface{})

Debugln outputs message, Arguments are handled by fmt.Sprintln

func (*Logger) Error

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

Error outputs message, Arguments are handled by fmt.Sprint

func (*Logger) Errorf

func (l *Logger) Errorf(msg string, args ...interface{})

Errorf outputs message, Arguments are handles by fmt.Sprintf

func (*Logger) Errorln

func (l *Logger) Errorln(obj ...interface{})

Errorln outputs message, Arguments are handled by fmt.Sprintln

func (*Logger) Fatal

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

Fatal outputs message and followed by a call to os.Exit(1), Arguments are handled by fmt.Sprint

func (*Logger) Fatalf

func (l *Logger) Fatalf(msg string, args ...interface{})

Fatalf outputs message and followed by a call to os.Exit(1), Arguments are handles by fmt.Sprintf

func (*Logger) Fatalln

func (l *Logger) Fatalln(obj ...interface{})

Fatalln outputs message and followed by a call to os.Exit(1), Arguments are handled by fmt.Sprintln

func (*Logger) Info

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

Info outputs message, Arguments are handled by fmt.Sprint

func (*Logger) Infof

func (l *Logger) Infof(msg string, args ...interface{})

Infof outputs message, Arguments are handles by fmt.Sprintf

func (*Logger) Infoln

func (l *Logger) Infoln(obj ...interface{})

Infoln outputs message, Arguments are handled by fmt.Sprintln

func (*Logger) IsDebugEnabled

func (l *Logger) IsDebugEnabled() bool

IsDebugEnabled indicates whether output message

func (*Logger) IsDisabled

func (l *Logger) IsDisabled() bool

IsDisabled indicates whether output message

func (*Logger) IsErrorEnabled

func (l *Logger) IsErrorEnabled() bool

IsErrorEnabled indicates whether output message

func (*Logger) IsFatalEnabled

func (l *Logger) IsFatalEnabled() bool

IsFatalEnabled indicates whether output message

func (*Logger) IsInfoEnabled

func (l *Logger) IsInfoEnabled() bool

IsInfoEnabled indicates whether output message

func (*Logger) IsPanicEnabled

func (l *Logger) IsPanicEnabled() bool

IsPanicEnabled indicates whether output message

func (*Logger) IsPrintEnabled

func (l *Logger) IsPrintEnabled() bool

IsPrintEnabled indicates whether output message

func (*Logger) IsWarnEnabled

func (l *Logger) IsWarnEnabled() bool

IsWarnEnabled indicates whether output message

func (*Logger) Panic

func (l *Logger) Panic(obj ...interface{})

Panic outputs message, and followed by a call to panic() Arguments are handled by fmt.Sprint

func (*Logger) Panicf

func (l *Logger) Panicf(msg string, args ...interface{})

Panicf outputs message and followed by a call to panic(), Arguments are handles by fmt.Sprintf

func (*Logger) Panicln

func (l *Logger) Panicln(obj ...interface{})

Panicln outputs message and followed by a call to panic(), Arguments are handled by fmt.Sprintln

func (*Logger) Print

func (l *Logger) Print(obj ...interface{})

Print outputs message, Arguments are handled by fmt.Sprint

func (*Logger) Printf

func (l *Logger) Printf(msg string, args ...interface{})

Printf outputs message, Arguments are handles by fmt.Sprintf

func (*Logger) Println

func (l *Logger) Println(obj ...interface{})

Println outputs message, Arguments are handled by fmt.Sprintln

func (*Logger) Warn

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

Warn outputs message, Arguments are handled by fmt.Sprint

func (*Logger) Warnf

func (l *Logger) Warnf(msg string, args ...interface{})

Warnf outputs message, Arguments are handles by fmt.Sprintf

func (*Logger) Warnln

func (l *Logger) Warnln(obj ...interface{})

Warnln outputs message, Arguments are handled by fmt.Sprintln

type StdLog

type StdLog interface {
	Print(...interface{})
	Printf(string, ...interface{})
	Println(...interface{})

	Fatal(...interface{})
	Fatalf(string, ...interface{})
	Fatalln(...interface{})

	Panic(...interface{})
	Panicf(string, ...interface{})
	Panicln(...interface{})
}

StdLog is interface for builtin log

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL