log4go

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2021 License: BSD-3-Clause Imports: 17 Imported by: 0

README

log4go

Lightweight log module, derived from a Google log project, officially has stopped maintenance update, here fork a copy of your own.

with go1.16+ and support go mod

Install

go get github.com/imb2022/log4go

Features
  • logs can be output to files and files can be divided by date
  • logs can output to the console
  • supports the syslog protocol

Documentation

Index

Constants

View Source
const (
	DEBUG = iota
	INFO
	WARNING
	ERROR
	FATAL
)

Variables

View Source
var GlobalLevel = DEBUG

GlobalLevel global level

View Source
var (
	LevelFlags = [...]string{"DEBUG", "INFO", "WARN", "ERROR", "FATAL"}
)

Functions

func Close

func Close()

Close loggerDefault close logger

func Debug

func Debug(fmt string, args ...interface{})

Debug loggerDefault deliver record to writer

func Error

func Error(fmt string, args ...interface{})

Error loggerDefault deliver record to writer

func Fatal

func Fatal(fmt string, args ...interface{})

Fatal loggerDefault deliver record to writer

func Info

func Info(fmt string, args ...interface{})

Info loggerDefault deliver record to writer

func Register

func Register(w Writer)

Register loggerDefault register writer

func SetLayout

func SetLayout(layout string)

SetLayout loggerDefault set the time format layout

func SetLevel

func SetLevel(lvl int)

SetLevel global set level is ignore logger level should be set in specific logger

func SetupLog

func SetupLog(lc LogConfig) (err error)

SetupLog setup log, caller shall use this method

func SetupLogWithConf

func SetupLogWithConf(file string) (err error)

SetupLogWithConf setup log with config file

func ShowFullPath

func ShowFullPath(show bool)

ShowFullPath loggerDefault show full path

func Warn

func Warn(fmt string, args ...interface{})

Warn loggerDefault deliver record to writer

Types

type ConfAliLogHubWriter

type ConfAliLogHubWriter struct {
	Level           string `json:"level" mapstructure:"level"`
	Enable          bool   `json:"enable" mapstructure:"enable"`
	LogName         string `json:"log_name" mapstructure:"log_name"`
	LogSource       string `json:"log_source" mapstructure:"log_source"`
	ProjectName     string `json:"project_name" mapstructure:"project_name"`
	Endpoint        string `json:"endpoint" mapstructure:"endpoint"`
	AccessKeyId     string `json:"access_key_id" mapstructure:"access_key_id"`
	AccessKeySecret string `json:"access_key_secret" mapstructure:"access_key_secret"`
	StoreName       string `json:"store_name" mapstructure:"store_name"`
	BufSize         int    `json:"buf_size" mapstructure:"buf_size"`
}

ConfAliLogHubWriter ali log hub writer config

type ConfConsoleWriter

type ConfConsoleWriter struct {
	Level  string `json:"level" mapstructure:"level"`
	Enable bool   `json:"enable" mapstructure:"enable"`
	Color  bool   `json:"color" mapstructure:"color"`
}

ConfConsoleWriter console writer config

type ConfFileWriter

type ConfFileWriter struct {
	Level   string `json:"level" mapstructure:"level"`
	LogPath string `json:"log_path" mapstructure:"log_path"`
	Enable  bool   `json:"enable" mapstructure:"enable"`
}

ConfFileWriter file writer config

type ConfKafKaWriter

type ConfKafKaWriter struct {
	Level          string `json:"level" mapstructure:"level"`
	Enable         bool   `json:"enable" mapstructure:"enable"`
	BufferSize     int    `json:"buffer_size" mapstructure:"buffer_size"`
	Debug          bool   `json:"debug" mapstructure:"debug"`                     // if true, will output the send msg
	SpecifyVersion bool   `json:"specify_version" mapstructure:"specify_version"` // if use the input version, default false
	Version        string `json:"version" mapstructure:"version"`                 // used to specify the kafka version, ex: 0.10.0.1 or 1.1.1

	Key string `json:"key" mapstructure:"key"` // kafka producer key, temp set, choice field

	ProducerTopic           string        `json:"producer_topic" mapstructure:"producer_topic"`
	ProducerReturnSuccesses bool          `json:"producer_return_successes" mapstructure:"producer_return_successes"`
	ProducerTimeout         time.Duration `json:"producer_timeout" mapstructure:"producer_timeout"` // ms
	Brokers                 []string      `json:"brokers" mapstructure:"brokers"`

	MSG KafKaMSGFields
}

ConfKafKaWriter kafka writer conf

type ConsoleWriter

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

ConsoleWriter console writer define

func NewConsoleWriter

func NewConsoleWriter() *ConsoleWriter

NewConsoleWriter create new console writer

func NewConsoleWriterWithLevel

func NewConsoleWriterWithLevel(level int) *ConsoleWriter

NewConsoleWriterWithLevel create new console writer with level

func (*ConsoleWriter) Init

func (w *ConsoleWriter) Init() error

Init console init without implement

func (*ConsoleWriter) SetColor

func (w *ConsoleWriter) SetColor(c bool)

SetColor console output color control

func (*ConsoleWriter) Write

func (w *ConsoleWriter) Write(r *Record) (err error)

Write console write

type FileWriter

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

FileWriter file writer define

func NewFileWriter

func NewFileWriter() *FileWriter

NewFileWriter create new file writer

func NewFileWriterWithLevel

func NewFileWriterWithLevel(level int) *FileWriter

NewFileWriterWithLevel create new file writer with level

func (*FileWriter) Flush

func (w *FileWriter) Flush() error

Flush for file writer

func (*FileWriter) Init

func (w *FileWriter) Init() error

Init for file writer

func (*FileWriter) Rotate

func (w *FileWriter) Rotate() error

Rotate for file writer

func (*FileWriter) SetPathPattern

func (w *FileWriter) SetPathPattern(pattern string) error

SetPathPattern for file writer

func (*FileWriter) Write

func (w *FileWriter) Write(r *Record) error

Write for file writer

type Flusher

type Flusher interface {
	Flush() error
}

Flusher flush interface

type KafKaMSGFields

type KafKaMSGFields struct {
	ESIndex     string                 `json:"es_index" mapstructure:"es_index"`         // required, init field
	Level       string                 `json:"level"`                                    // dynamic, set by logger
	Code        string                 `json:"file"`                                     // dynamic, source code file:line_number
	Message     string                 `json:"message"`                                  // dynamic, message
	ServerIP    string                 `json:"server_ip" mapstructure:"server_ip"`       // required, init field, set by app
	PublicIP    string                 `json:"public_ip" mapstructure:"public_ip"`       // required, init field, set by app
	Timestamp   string                 `json:"timestamp" mapstructure:"timestamp"`       // required, dynamic, set by logger
	Now         int64                  `json:"now" mapstructure:"now"`                   // choice, unix timestamp, second
	ExtraFields map[string]interface{} `json:"extra_fields" mapstructure:"extra_fields"` // extra fields will be added
}

KafKaMSGFields kafka msg fields

type KafKaWriter

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

KafKaWriter kafka writer

func NewKafKaWriter

func NewKafKaWriter(conf *ConfKafKaWriter) *KafKaWriter

NewKafKaWriter new kafka writer

func NewKafKaWriterWithWriter

func NewKafKaWriterWithWriter(conf *ConfKafKaWriter, level int) *KafKaWriter

NewKafKaWriterWithWriter new kafka writer with level

func (*KafKaWriter) Init

func (k *KafKaWriter) Init() error

Init service for Record

func (*KafKaWriter) Start

func (k *KafKaWriter) Start() (err error)

Start start the kafka writer

func (*KafKaWriter) Stop

func (k *KafKaWriter) Stop()

Stop stop the kafka writer

func (*KafKaWriter) Write

func (k *KafKaWriter) Write(r *Record) error

Write service for Record

type LogConfig

type LogConfig struct {
	// global level, maybe override by real minimum multi writer level
	Level         string            `json:"level" mapstructure:"level"`
	FullPath      bool              `json:"full_path" mapstructure:"full_path"`
	FileWriter    ConfFileWriter    `json:"file_writer" mapstructure:"file_writer"`
	ConsoleWriter ConfConsoleWriter `json:"console_writer" mapstructure:"console_writer"`
	KafKaWriter   ConfKafKaWriter   `json:"kafka_writer" mapstructure:"kafka_writer"`
}

LogConfig log config

type Logger

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

Logger log struct

func NewLogger

func NewLogger() *Logger

NewLogger create the logger instance

func (*Logger) Close

func (l *Logger) Close()

Close Logger close buffer, flush and stop logger

func (*Logger) Debug

func (l *Logger) Debug(fmt string, args ...interface{})

Debug Logger deliver record to writer

func (*Logger) Error

func (l *Logger) Error(fmt string, args ...interface{})

Error Logger deliver record to writer

func (*Logger) Fatal

func (l *Logger) Fatal(fmt string, args ...interface{})

Fatal Logger deliver record to writer

func (*Logger) Info

func (l *Logger) Info(fmt string, args ...interface{})

Info Logger deliver record to writer

func (*Logger) Register

func (l *Logger) Register(w Writer)

Register register logger writer

func (*Logger) SetLayout

func (l *Logger) SetLayout(layout string)

SetLayout Logger set the time data format, layout

func (*Logger) SetLevel

func (l *Logger) SetLevel(lvl int)

SetLevel Logger set default level

func (*Logger) Warn

func (l *Logger) Warn(fmt string, args ...interface{})

Warn Logger deliver record to writer

type Record

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

Record record struct

func (*Record) String

func (r *Record) String() string

String record string

type Rotater

type Rotater interface {
	Rotate() error
	SetPathPattern(string) error
}

Rotater rotate interface

type ShortRecord

type ShortRecord Record

ShortRecord short record

func (*ShortRecord) String

func (r *ShortRecord) String() string

String string

type SyslogWriter

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

SyslogWriter sys log writer

func NewSyslogWriter

func NewSyslogWriter() *SyslogWriter

func (*SyslogWriter) Init

func (w *SyslogWriter) Init() (err error)

func (*SyslogWriter) SetAddr

func (w *SyslogWriter) SetAddr(addr string)

func (*SyslogWriter) SetNetwork

func (w *SyslogWriter) SetNetwork(network string)

func (*SyslogWriter) SetTag

func (w *SyslogWriter) SetTag(tag string)

func (*SyslogWriter) Write

func (w *SyslogWriter) Write(r *Record) (err error)

type Writer

type Writer interface {
	Init() error
	Write(*Record) error
}

Writer writer interface

Directories

Path Synopsis
examples
*@description HTTP相关的方法集合 @author imb2022 *
*@description HTTP相关的方法集合 @author imb2022 *

Jump to

Keyboard shortcuts

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