logger

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

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

Go to latest
Published: Oct 15, 2020 License: Apache-2.0 Imports: 15 Imported by: 0

README

go-logger

Go Report Card GoDoc Github All Releases GitHub release GitHub Release Date GitHub license GitHub stars GitHub forks Sourcegraph

Description

A log library for golang. Can be initialized from xml format configuration file, supports scrolling based on file size and time of log files.

Installation

This package can be installed with the go get command:

    go get gitera.cn/ron/go-logger
Logger Level
    ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF
Initialized

LoggerWriter is a complete logger that supports automatic scrolling of logger files by time or file size. It's initial by config file with xml format "example/logger.xml":

package main

import (
    "gitera.cn/ron/go-logger"
)

func main()  {
    err := logger.Init("example/logger.xml")

    if err != nil {
        logger.DefaultConsoleLogger().Error(err.Error())
        return
    }

    logger.Trace("Test Logger trace message")
    logger.Debug("Test Logger debug message")
    logger.Info("Test Logger info message")
    logger.Warn("Test Logger warn message")
    logger.Error("Test Logger error message")
}
ConsoleLogger

Console logger outputs log information to stdout:

package main

import (
    "gitera.cn/ron/go-logger"
)

func main()  {
    consoleLogger := logger.NewConsoleLogger(logger.ALL)

    consoleLogger.Info("ConsoleLogger info message") 
}
FileLogger

File logger outputs log information to a file:

package main

import (
    "gitera.cn/ron/go-logger"
)

func main()  {
    fileLogger, err := logger.NewFileLogger(logger.ALL, "logs/fileLogger.log")
    if err != nil {
        logger.DefaultConsoleLogger().Error(err.Error())
        return
    }

    fileLogger.Info("FileLogger info message")
}

Documentation

Index

Constants

View Source
const (
	ALL   = 0
	TRACE = 1
	DEBUG = 2
	INFO  = 3
	WARN  = 4
	ERROR = 5
	FATAL = 6
	OFF   = 7

	DefaultLogTimeFormat = "2006/01/02 15:04:05.000000"
)

ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF

Variables

View Source
var (
	DefaultWriter  = os.Stdout
	FileWithoutPKG = false
)
View Source
var (
	DefaultFormat = "%{Prefix} - %{Time:yyyy-mm-dd HH:MM:SS.ms} - %{Level:5} - %{File}:%{Line:3} - %{Message}"
)

Functions

func ConvertLevel2String

func ConvertLevel2String(level LogLevel) string

func Debug

func Debug(args ...interface{})

func Debugf

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

func Error

func Error(args ...interface{})

func Errorf

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

func Fatal

func Fatal(args ...interface{})

func Fatalf

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

func GetCaller

func GetCaller(skip int) *runtime.Frame

func GetFileName

func GetFileName(frame *runtime.Frame) string

func GetPackageName

func GetPackageName(f string) string

Get the package name by runtime.Frame.Function

func Info

func Info(args ...interface{})

func Infof

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

func Initial

func Initial(configFile string) error

func Initialized

func Initialized() bool

func RemoveEnterAndSpace

func RemoveEnterAndSpace(str string) string

func StartRolling

func StartRolling()

func StopRolling

func StopRolling()

func Trace

func Trace(args ...interface{})

func Tracef

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

func Variable

func Variable(varchar, pattern, str string) (string, string)

Find the variable define in string

func VariableReplaceByConfig

func VariableReplaceByConfig(str string) string

Replace the variable define in properties

func Warn

func Warn(args ...interface{})

func Warnf

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

Types

type Config

type Config struct {
	XMLName         xml.Name   `xml:"Configuration"`
	RollingInterval int        `xml:"rollingInterval,attr"`
	Properties      []Property `xml:"Properties>Property"`
	Loggers         []Logger   `xml:"Loggers>Logger"`
	DefaultFilter   Filter     `xml:"Filters>DefaultFilter>Filter"`
	PackageFilters  []Filter   `xml:"Filters>PackageFilter>Filter"`
}

func NewConfig

func NewConfig(configFile string) (*Config, error)

type ConsoleLogger

type ConsoleLogger struct {
	*LoggerWriter
	// contains filtered or unexported fields
}

func DefaultConsoleLogger

func DefaultConsoleLogger() *ConsoleLogger

func NewConsoleLogger

func NewConsoleLogger(level LogLevel) *ConsoleLogger

func (*ConsoleLogger) CheckRollingSize

func (this *ConsoleLogger) CheckRollingSize()

Do nothing with implement interface Writer

func (*ConsoleLogger) Debug

func (this *ConsoleLogger) Debug(args ...interface{})

func (*ConsoleLogger) Debugf

func (this *ConsoleLogger) Debugf(format string, args ...interface{})

func (*ConsoleLogger) Error

func (this *ConsoleLogger) Error(args ...interface{})

func (*ConsoleLogger) Errorf

func (this *ConsoleLogger) Errorf(format string, args ...interface{})

func (*ConsoleLogger) FatalWithExit

func (this *ConsoleLogger) FatalWithExit(exit bool, args ...interface{})

func (*ConsoleLogger) FatalfWithExit

func (this *ConsoleLogger) FatalfWithExit(exit bool, format string, args ...interface{})

func (*ConsoleLogger) Info

func (this *ConsoleLogger) Info(args ...interface{})

func (*ConsoleLogger) Infof

func (this *ConsoleLogger) Infof(format string, args ...interface{})

func (*ConsoleLogger) Trace

func (this *ConsoleLogger) Trace(args ...interface{})

func (*ConsoleLogger) Tracef

func (this *ConsoleLogger) Tracef(format string, args ...interface{})

ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF

func (*ConsoleLogger) Warn

func (this *ConsoleLogger) Warn(args ...interface{})

func (*ConsoleLogger) Warnf

func (this *ConsoleLogger) Warnf(format string, args ...interface{})

type FileLogger

type FileLogger struct {
	*LoggerWriter
	// contains filtered or unexported fields
}

func NewFileLogger

func NewFileLogger(level LogLevel, logFile string) (*FileLogger, error)

func NewFileLoggerWithConfig

func NewFileLoggerWithConfig(v Logger) (*FileLogger, error)

func (*FileLogger) CheckRollingSize

func (this *FileLogger) CheckRollingSize()

func (*FileLogger) RollingFile

func (this *FileLogger) RollingFile()

Rolling a new file to write logger

type Filter

type Filter struct {
	XMLName xml.Name `xml:"Filter"`
	Name    string   `xml:"name,attr"`
	Loggers []string `xml:"Logger"`
}

type Format

type Format struct {
	XMLName xml.Name `xml:"Format"`
	Type    string   `xml:"type,attr"`
	Value   string   `xml:",innerxml"`
}

type Formatter

type Formatter interface {
	Message(data map[string]interface{}, args ...interface{}) string
}

type JSONFormatter

type JSONFormatter struct {
	Indent bool
}

func NewJSONFormatter

func NewJSONFormatter() *JSONFormatter

func (*JSONFormatter) Message

func (this *JSONFormatter) Message(data map[string]interface{}, args ...interface{}) string

type Level

type Level struct {
	XMLName xml.Name `xml:"Level"`
	Allow   string   `xml:"Allow"`
	Deny    string   `xml:"Deny"`
}

type LogLevel

type LogLevel int

Logger level

func ConvertString2Level

func ConvertString2Level(str string) LogLevel

Convert string level name to level

type Logger

type Logger struct {
	XMLName     xml.Name `xml:"Logger"`
	Name        string   `xml:"name,attr"`
	Target      string   `xml:"target,attr"`
	FileName    string   `xml:"fileName,attr"`
	FilePattern string   `xml:"filePattern,attr"`
	Compress    string   `xml:"compress,attr"`
	Format      Format   `xml:"Format"`
	Level       Level    `xml:"Level"`
	Rolling     Rolling  `xml:"Rolling"`
}

type LoggerWriter

type LoggerWriter struct {
	*log.Logger
	// contains filtered or unexported fields
}

func NewLoggerWriter

func NewLoggerWriter(w io.Writer, level LogLevel) *LoggerWriter

func (*LoggerWriter) AfterSQL

func (this *LoggerWriter) AfterSQL(context xormlog.LogContext)

func (*LoggerWriter) BeforeSQL

func (this *LoggerWriter) BeforeSQL(context xormlog.LogContext)

func (*LoggerWriter) Debug

func (this *LoggerWriter) Debug(args ...interface{})

func (*LoggerWriter) Debugf

func (this *LoggerWriter) Debugf(format string, args ...interface{})

func (*LoggerWriter) Error

func (this *LoggerWriter) Error(args ...interface{})

func (*LoggerWriter) Errorf

func (this *LoggerWriter) Errorf(format string, args ...interface{})

func (*LoggerWriter) Fatal

func (this *LoggerWriter) Fatal(v ...interface{})

Fatal is equivalent to l.Print() followed by a call to os.Exit(1).

func (*LoggerWriter) FatalWithExit

func (this *LoggerWriter) FatalWithExit(exit bool, args ...interface{})

func (*LoggerWriter) Fatalf

func (this *LoggerWriter) Fatalf(format string, v ...interface{})

Fatalf is equivalent to l.Printf() followed by a call to os.Exit(1).

func (*LoggerWriter) FatalfWithExit

func (this *LoggerWriter) FatalfWithExit(exit bool, format string, args ...interface{})

func (*LoggerWriter) Fatalln

func (this *LoggerWriter) Fatalln(v ...interface{})

Fatalln is equivalent to l.Println() followed by a call to os.Exit(1).

func (*LoggerWriter) Info

func (this *LoggerWriter) Info(args ...interface{})

func (*LoggerWriter) Infof

func (this *LoggerWriter) Infof(format string, args ...interface{})

func (*LoggerWriter) IsShowSQL

func (this *LoggerWriter) IsShowSQL() bool

func (*LoggerWriter) Level

func (this *LoggerWriter) Level() xormlog.LogLevel

func (*LoggerWriter) NewLogger

func (this *LoggerWriter) NewLogger(w io.Writer)

func (*LoggerWriter) Output

func (this *LoggerWriter) Output(calldepth int, s string) error

Rewrite log.Logger function

func (*LoggerWriter) Panic

func (this *LoggerWriter) Panic(v ...interface{})

Panic is equivalent to l.Print() followed by a call to panic().

func (*LoggerWriter) Panicf

func (this *LoggerWriter) Panicf(format string, v ...interface{})

Panicf is equivalent to l.Printf() followed by a call to panic().

func (*LoggerWriter) Panicln

func (this *LoggerWriter) Panicln(v ...interface{})

Panicln is equivalent to l.Println() followed by a call to panic().

func (*LoggerWriter) Prefix

func (this *LoggerWriter) Prefix() string

Prefix returns the output prefix for the logger.

func (*LoggerWriter) Print

func (this *LoggerWriter) Print(v ...interface{})

Print calls this.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*LoggerWriter) Printf

func (this *LoggerWriter) Printf(format string, v ...interface{})

Printf calls this.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*LoggerWriter) Println

func (this *LoggerWriter) Println(v ...interface{})

Println calls this.Output to print to the logger. Arguments are handled in the manner of fmt.Println.

func (*LoggerWriter) SetDenyLevel

func (this *LoggerWriter) SetDenyLevel(level LogLevel)

func (*LoggerWriter) SetFormatter

func (this *LoggerWriter) SetFormatter(formatter Formatter)

func (*LoggerWriter) SetLevel

func (this *LoggerWriter) SetLevel(l xormlog.LogLevel)

func (*LoggerWriter) SetPrefix

func (this *LoggerWriter) SetPrefix(prefix string)

SetPrefix sets the output prefix for the logger.

func (*LoggerWriter) SetSkipCallerDepth

func (this *LoggerWriter) SetSkipCallerDepth(skipCallerDepth int)

func (*LoggerWriter) SetWriter

func (this *LoggerWriter) SetWriter(w io.Writer)

func (*LoggerWriter) ShowSQL

func (this *LoggerWriter) ShowSQL(show ...bool)

func (*LoggerWriter) Trace

func (this *LoggerWriter) Trace(args ...interface{})

ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF

func (*LoggerWriter) Tracef

func (this *LoggerWriter) Tracef(format string, args ...interface{})

Implement Writer

func (*LoggerWriter) Warn

func (this *LoggerWriter) Warn(args ...interface{})

func (*LoggerWriter) Warnf

func (this *LoggerWriter) Warnf(format string, args ...interface{})

func (*LoggerWriter) Write

func (this *LoggerWriter) Write(level LogLevel, args ...interface{}) error

type Property

type Property struct {
	XMLName xml.Name `xml:"Property"`
	Name    string   `xml:"name,attr"`
	Value   string   `xml:",innerxml"`
}

type Rolling

type Rolling struct {
	XMLName   xml.Name `xml:"Rolling"`
	TimeBased string   `xml:"TimeBased"`
	SizeBased int      `xml:"SizeBased"`
	KeepCount int      `xml:"KeepCount"`
}

type TextFormatter

type TextFormatter struct {
	Format string
}

func NewTextFormatter

func NewTextFormatter() *TextFormatter

func NewTextFormatterWithFormat

func NewTextFormatterWithFormat(format string) *TextFormatter

func (*TextFormatter) Message

func (this *TextFormatter) Message(data map[string]interface{}, args ...interface{}) string

func (*TextFormatter) SetFormat

func (this *TextFormatter) SetFormat(format string)

type Writer

type Writer interface {
	Tracef(format string, args ...interface{})

	Debugf(format string, args ...interface{})

	Infof(format string, args ...interface{})

	Warnf(format string, args ...interface{})

	Errorf(format string, args ...interface{})

	FatalfWithExit(exit bool, format string, args ...interface{})

	Trace(args ...interface{})

	Debug(args ...interface{})

	Info(args ...interface{})

	Warn(args ...interface{})

	Error(args ...interface{})

	FatalWithExit(exit bool, args ...interface{})

	CheckRollingSize()

	BeforeSQL(context xormlog.LogContext) // only invoked when IsShowSQL is true

	AfterSQL(context xormlog.LogContext) // only invoked when IsShowSQL is true

	Level() xormlog.LogLevel

	SetLevel(l xormlog.LogLevel)

	ShowSQL(show ...bool)

	IsShowSQL() bool
}

logger writer interface

func GetByPackage

func GetByPackage(packageName string) []Writer

Jump to

Keyboard shortcuts

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