log

package module
v5.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2019 License: Apache-2.0 Imports: 16 Imported by: 15

README

go-log

Logging package similar to log4j for the Golang.

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

Installation

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

Usage

package main

import (
	"os"
	"errors"
	"github.com/fangdingjun/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/fangdingjun/go-log"
	"github.com/fangdingjun/go-log/log"
)

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

Three builtin log for use

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

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

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

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

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

func main() {
	logger := &log.Logger{
		Level:     log.INFO,
		Formatter: new(log.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, log and log.

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/fangdingjun/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/fangdingjun
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/fangdingjun/go-log"
)

func main() {
	logger := &log.Logger{
		Level:     log.INFO,
		Formatter: new(log.TextFormatter),
		Out:       &log.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/fangdingjun/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 FilelineCaller

func FilelineCaller(skip int) (file string, line int)

FilelineCaller returns file and line for caller

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 IsTerminal

func IsTerminal(w io.Writer) bool

IsTerminal returns whether is a valid tty for io.Writer

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 AlwaysNewFileWriter

type AlwaysNewFileWriter struct {
	Name     string
	MaxCount int
	// contains filtered or unexported fields
}

AlwaysNewFileWriter create new log for every process

func (*AlwaysNewFileWriter) Write

func (w *AlwaysNewFileWriter) Write(p []byte) (n int, err error)

Write implements io.Writer

type DailyFileWriter

type DailyFileWriter struct {
	Name     string
	MaxCount int
	// contains filtered or unexported fields
}

DailyFileWriter create new log for every day

func (*DailyFileWriter) Write

func (w *DailyFileWriter) Write(p []byte) (n int, err error)

Write implements io.Writer

type FixedSizeFileWriter

type FixedSizeFileWriter struct {
	Name     string
	MaxSize  int64
	MaxCount int
	// contains filtered or unexported fields
}

FixedSizeFileWriter create new log if log size exceed

func (*FixedSizeFileWriter) Write

func (w *FixedSizeFileWriter) Write(p []byte) (n int, err error)

Write implements io.Writer

type Formatter

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

Formatter is a interface used to implement a custom Formatter

type Interface

type Interface 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{})
}

Interface is interface for this logger

type JSONFormatter

type JSONFormatter struct {
	AppName    string
	TimeFormat string
	// contains filtered or unexported fields
}

JSONFormatter is a json formatter

func (*JSONFormatter) Format

func (f *JSONFormatter) Format(level Level, msg string, logger *Logger) []byte

Format implements log.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 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

type TextFormatter

type TextFormatter struct {
	AppName    string
	TimeFormat string
	// contains filtered or unexported fields
}

TextFormatter is a text line formatter

func (*TextFormatter) Format

func (f *TextFormatter) Format(level Level, msg string, logger *Logger) []byte

Format implements Formatter

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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