Documentation ¶
Overview ¶
Package blog4go provide an efficient and easy-to-use writers library for logging into files, console or sockets. Writers suports formatting string filtering and calling user defined hook in asynchronous mode.
Index ¶
- Constants
- Variables
- func Close()
- func Colored() bool
- func Critical(args ...interface{})
- func Criticalf(format string, args ...interface{})
- func Debug(args ...interface{})
- func Debugf(format string, args ...interface{})
- func Error(args ...interface{})
- func Errorf(format string, args ...interface{})
- func Flush()
- func Info(args ...interface{})
- func Infof(format string, args ...interface{})
- func NewBaseFileWriter(fileName string, timeRotated bool) (err error)
- func NewConsoleWriter(redirected bool) (err error)
- func NewFileWriter(baseDir string, rotate bool) (err error)
- func NewSocketWriter(network string, address string) (err error)
- func NewWriterFromConfigAsFile(configFile string) (err error)
- func Retentions() int64
- func RotateLines() int
- func RotateSize() int64
- func SetBufferSize(size int)
- func SetColored(colored bool)
- func SetHook(hook Hook)
- func SetHookAsync(async bool)
- func SetHookLevel(level LevelType)
- func SetLevel(level LevelType)
- func SetRetentions(retentions int64)
- func SetRotateLines(rotateLines int)
- func SetRotateSize(rotateSize int64)
- func SetTags(tags map[string]string)
- func SetTimeRotated(timeRotated bool)
- func Tags() map[string]string
- func TimeRotated() bool
- func Trace(args ...interface{})
- func Tracef(format string, args ...interface{})
- func Warn(args ...interface{})
- func Warnf(format string, args ...interface{})
- type BLog
- type Config
- type ConsoleWriter
- func (writer *ConsoleWriter) Close()
- func (writer *ConsoleWriter) Closed() bool
- func (writer *ConsoleWriter) Colored() bool
- func (writer *ConsoleWriter) Critical(args ...interface{})
- func (writer *ConsoleWriter) Criticalf(format string, args ...interface{})
- func (writer *ConsoleWriter) Debug(args ...interface{})
- func (writer *ConsoleWriter) Debugf(format string, args ...interface{})
- func (writer *ConsoleWriter) Error(args ...interface{})
- func (writer *ConsoleWriter) Errorf(format string, args ...interface{})
- func (writer *ConsoleWriter) Info(args ...interface{})
- func (writer *ConsoleWriter) Infof(format string, args ...interface{})
- func (writer *ConsoleWriter) Level() LevelType
- func (writer *ConsoleWriter) Retentions() int64
- func (writer *ConsoleWriter) RotateLines() int
- func (writer *ConsoleWriter) RotateSize() int64
- func (writer *ConsoleWriter) SetColored(colored bool)
- func (writer *ConsoleWriter) SetHook(hook Hook)
- func (writer *ConsoleWriter) SetHookAsync(async bool)
- func (writer *ConsoleWriter) SetHookLevel(level LevelType)
- func (writer *ConsoleWriter) SetLevel(level LevelType)
- func (writer *ConsoleWriter) SetRetentions(retentions int64)
- func (writer *ConsoleWriter) SetRotateLines(rotateLines int)
- func (writer *ConsoleWriter) SetRotateSize(rotateSize int64)
- func (writer *ConsoleWriter) SetTags(tags map[string]string)
- func (writer *ConsoleWriter) SetTimeRotated(timeRotated bool)
- func (writer *ConsoleWriter) Tags() map[string]string
- func (writer *ConsoleWriter) TimeRotated() bool
- func (writer *ConsoleWriter) Trace(args ...interface{})
- func (writer *ConsoleWriter) Tracef(format string, args ...interface{})
- func (writer *ConsoleWriter) Warn(args ...interface{})
- func (writer *ConsoleWriter) Warnf(format string, args ...interface{})
- type Hook
- type LevelType
- type MultiWriter
- func (writer *MultiWriter) Close()
- func (writer *MultiWriter) Colored() bool
- func (writer *MultiWriter) Critical(args ...interface{})
- func (writer *MultiWriter) Criticalf(format string, args ...interface{})
- func (writer *MultiWriter) Debug(args ...interface{})
- func (writer *MultiWriter) Debugf(format string, args ...interface{})
- func (writer *MultiWriter) Error(args ...interface{})
- func (writer *MultiWriter) Errorf(format string, args ...interface{})
- func (writer *MultiWriter) Info(args ...interface{})
- func (writer *MultiWriter) Infof(format string, args ...interface{})
- func (writer *MultiWriter) Level() LevelType
- func (writer *MultiWriter) Retentions() int64
- func (writer *MultiWriter) RotateLines() int
- func (writer *MultiWriter) RotateSize() int64
- func (writer *MultiWriter) SetColored(colored bool)
- func (writer *MultiWriter) SetHook(hook Hook)
- func (writer *MultiWriter) SetHookAsync(async bool)
- func (writer *MultiWriter) SetHookLevel(level LevelType)
- func (writer *MultiWriter) SetLevel(level LevelType)
- func (writer *MultiWriter) SetRetentions(retentions int64)
- func (writer *MultiWriter) SetRotateLines(rotateLines int)
- func (writer *MultiWriter) SetRotateSize(rotateSize int64)
- func (writer *MultiWriter) SetTags(tags map[string]string)
- func (writer *MultiWriter) SetTimeRotated(timeRotated bool)
- func (writer *MultiWriter) Tags() map[string]string
- func (writer *MultiWriter) TimeRotated() bool
- func (writer *MultiWriter) Trace(args ...interface{})
- func (writer *MultiWriter) Tracef(format string, args ...interface{})
- func (writer *MultiWriter) Warn(args ...interface{})
- func (writer *MultiWriter) Warnf(format string, args ...interface{})
- type SocketWriter
- func (writer *SocketWriter) Close()
- func (writer *SocketWriter) Colored() bool
- func (writer *SocketWriter) Critical(args ...interface{})
- func (writer *SocketWriter) Criticalf(format string, args ...interface{})
- func (writer *SocketWriter) Debug(args ...interface{})
- func (writer *SocketWriter) Debugf(format string, args ...interface{})
- func (writer *SocketWriter) Error(args ...interface{})
- func (writer *SocketWriter) Errorf(format string, args ...interface{})
- func (writer *SocketWriter) Info(args ...interface{})
- func (writer *SocketWriter) Infof(format string, args ...interface{})
- func (writer *SocketWriter) Level() LevelType
- func (writer *SocketWriter) Retentions() int64
- func (writer *SocketWriter) RotateLines() int
- func (writer *SocketWriter) RotateSize() int64
- func (writer *SocketWriter) SetColored(colored bool)
- func (writer *SocketWriter) SetHook(hook Hook)
- func (writer *SocketWriter) SetHookAsync(async bool)
- func (writer *SocketWriter) SetHookLevel(level LevelType)
- func (writer *SocketWriter) SetLevel(level LevelType)
- func (writer *SocketWriter) SetRetentions(retentions int64)
- func (writer *SocketWriter) SetRotateLines(rotateLines int)
- func (writer *SocketWriter) SetRotateSize(rotateSize int64)
- func (writer *SocketWriter) SetTags(tags map[string]string)
- func (writer *SocketWriter) SetTimeRotated(timeRotated bool)
- func (writer *SocketWriter) Tags() map[string]string
- func (writer *SocketWriter) TimeRotated() bool
- func (writer *SocketWriter) Trace(args ...interface{})
- func (writer *SocketWriter) Tracef(format string, args ...interface{})
- func (writer *SocketWriter) Warn(args ...interface{})
- func (writer *SocketWriter) Warnf(format string, args ...interface{})
- type Writer
Constants ¶
const ( // KB unit of kilobyte KB int64 = 1 << (10 * iota) // MB unit of megabyte MB // GB unit of gigabyte GB // DefaultRotateSize is default size when size base logrotate needed DefaultRotateSize = 500 * MB // DefaultRotateLines is default lines when lines base logrotate needed DefaultRotateLines = 2000000 // 2 million // DefaultLogRetentionCount is the default days of logs to be keeped DefaultLogRetentionCount = 7 )
const ( // EOL end of a line EOL = '\n' // ESCAPE escape character ESCAPE = '\\' // PLACEHOLDER placeholder PLACEHOLDER = '%' // QUOTE quote character QUOTE = '"' // SPACE space character SPACE = ' ' )
const ( // TypeTimeBaseRotate is time base logrotate tag TypeTimeBaseRotate = "time" // TypeSizeBaseRotate is size base logrotate tag TypeSizeBaseRotate = "size" )
const ( // TRACE trace level TRACE LevelType = iota // DEBUG debug level DEBUG // INFO info level INFO // WARNING warn level WARNING // ERROR error level ERROR // CRITICAL critical level CRITICAL // UNKNOWN unknown level UNKNOWN = "UNKNOWN" // DefaultLevel default level for writers DefaultLevel = TRACE // PrefixFormat is the level format ahead every message PrefixFormat = " level=\"%s\" " // pure format // ColoredPrefixFormat is the colored level format adhead every message ColoredPrefixFormat = " level=\"\x1b[%dm%s\x1b[0m\" " // colored format // NOCOLOR no color NOCOLOR = 0 // RED red color RED = 31 // GREEN green color GREEN = 32 // YELLOW yellow color YELLOW = 33 // BLUE blue color BLUE = 34 // GRAY gray color GRAY = 37 )
const ( // PrefixTimeFormat const time format prefix PrefixTimeFormat = "time=\"2006-01-02 15:04:05\"" // DateFormat date format DateFormat = "2006-01-02" )
const (
// VERSION shows the current version for this package
VERSION = "0.5.9"
)
Variables ¶
var ( // DefaultBufferSize bufio buffer size DefaultBufferSize = 4096 // default memory page size // ErrInvalidFormat invalid format error ErrInvalidFormat = errors.New("Invalid format type") // ErrAlreadyInit show that blog is already initialized once ErrAlreadyInit = errors.New("blog4go has been already initialized") )
var ( // ErrConfigFiltersNotFound not found filters ErrConfigFiltersNotFound = errors.New("Please define at least one filter") // ErrConfigBadAttributes wrong attribute ErrConfigBadAttributes = errors.New("Bad attributes setting") // ErrConfigLevelsNotFound not found levels ErrConfigLevelsNotFound = errors.New("Please define levels attribution") // ErrConfigFilePathNotFound not found file path ErrConfigFilePathNotFound = errors.New("Please define the file path") // ErrConfigFileRotateTypeNotFound not found rotate type ErrConfigFileRotateTypeNotFound = errors.New("Please define the file rotate type") // ErrConfigSocketAddressNotFound not found socket address ErrConfigSocketAddressNotFound = errors.New("Please define a socket address") // ErrConfigSocketNetworkNotFound not found socket port ErrConfigSocketNetworkNotFound = errors.New("Please define a socket network type") )
var ( // LevelStrings is string present for each level LevelStrings = [...]string{"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"} // StringLevels is map, level strings to levels StringLevels = map[string]LevelType{"TRACE": TRACE, "DEBUG": DEBUG, "INFO": INFO, "WARN": WARNING, "ERROR": ERROR, "CRITICAL": CRITICAL} // Levels is a slice consist of all levels Levels = [...]LevelType{TRACE, DEBUG, INFO, WARNING, ERROR, CRITICAL} // Prefix is preformatted level prefix string // help reduce string formatted burden in realtime logging Prefix = make(map[LevelType]string) )
var ( // ErrFilePathNotFound file path not found ErrFilePathNotFound = errors.New("File Path must be defined") // ErrInvalidLevel invalid level string ErrInvalidLevel = errors.New("Invalid level string") // ErrInvalidRotateType invalid logrotate type ErrInvalidRotateType = errors.New("Invalid log rotate type") )
Functions ¶
func Criticalf ¶
func Criticalf(format string, args ...interface{})
Criticalf static function for Criticalf
func NewBaseFileWriter ¶
NewBaseFileWriter initialize a base file writer
func NewConsoleWriter ¶
NewConsoleWriter initialize a console writer, singlton
func NewFileWriter ¶
NewFileWriter initialize a file writer baseDir must be base directory of log files rotate determine if it will logrotate
func NewSocketWriter ¶
NewSocketWriter creates a socket writer, singlton
func NewWriterFromConfigAsFile ¶
NewWriterFromConfigAsFile initialize a writer according to given config file configFile must be the path to the config file
func SetBufferSize ¶ added in v0.5.9
func SetBufferSize(size int)
SetBufferSize set bufio buffer size in bytes
func SetRetentions ¶
func SetRetentions(retentions int64)
SetRetentions set how many logs will keep after logrotate
func SetRotateLines ¶
func SetRotateLines(rotateLines int)
SetRotateLines set line number when logrotate
func SetTimeRotated ¶
func SetTimeRotated(timeRotated bool)
SetTimeRotated toggle time base logrotate on the fly
Types ¶
type BLog ¶
type BLog struct {
// contains filtered or unexported fields
}
BLog struct is a threadsafe log writer inherit bufio.Writer
func NewBLog ¶
NewBLog create a BLog instance and return the pointer of it. fileName must be an absolute path to the destination log file
type Config ¶
type Config struct { Filters []filter `xml:"filter"` MinLevel string `xml:"minlevel,attr"` }
Config struct define the config struct used for file wirter
type ConsoleWriter ¶
type ConsoleWriter struct {
// contains filtered or unexported fields
}
ConsoleWriter is a console logger
func (*ConsoleWriter) Closed ¶ added in v0.5.9
func (writer *ConsoleWriter) Closed() bool
Closed get writer status
func (*ConsoleWriter) Critical ¶
func (writer *ConsoleWriter) Critical(args ...interface{})
Critical critical
func (*ConsoleWriter) Criticalf ¶
func (writer *ConsoleWriter) Criticalf(format string, args ...interface{})
Criticalf criticalf
func (*ConsoleWriter) Debugf ¶
func (writer *ConsoleWriter) Debugf(format string, args ...interface{})
Debugf debugf
func (*ConsoleWriter) Errorf ¶
func (writer *ConsoleWriter) Errorf(format string, args ...interface{})
Errorf errorf
func (*ConsoleWriter) Infof ¶
func (writer *ConsoleWriter) Infof(format string, args ...interface{})
Infof infof
func (*ConsoleWriter) Retentions ¶
func (writer *ConsoleWriter) Retentions() int64
Retentions do nothing
func (*ConsoleWriter) RotateLines ¶
func (writer *ConsoleWriter) RotateLines() int
RotateLines do nothing
func (*ConsoleWriter) RotateSize ¶
func (writer *ConsoleWriter) RotateSize() int64
RotateSize do nothing
func (*ConsoleWriter) SetColored ¶
func (writer *ConsoleWriter) SetColored(colored bool)
SetColored set logging color
func (*ConsoleWriter) SetHook ¶
func (writer *ConsoleWriter) SetHook(hook Hook)
SetHook set hook for logging action
func (*ConsoleWriter) SetHookAsync ¶
func (writer *ConsoleWriter) SetHookAsync(async bool)
SetHookAsync set hook async for base file writer
func (*ConsoleWriter) SetHookLevel ¶
func (writer *ConsoleWriter) SetHookLevel(level LevelType)
SetHookLevel set when hook will be called
func (*ConsoleWriter) SetLevel ¶
func (writer *ConsoleWriter) SetLevel(level LevelType)
SetLevel set logger level
func (*ConsoleWriter) SetRetentions ¶
func (writer *ConsoleWriter) SetRetentions(retentions int64)
SetRetentions do nothing
func (*ConsoleWriter) SetRotateLines ¶
func (writer *ConsoleWriter) SetRotateLines(rotateLines int)
SetRotateLines do nothing
func (*ConsoleWriter) SetRotateSize ¶
func (writer *ConsoleWriter) SetRotateSize(rotateSize int64)
SetRotateSize do nothing
func (*ConsoleWriter) SetTags ¶ added in v0.5.9
func (writer *ConsoleWriter) SetTags(tags map[string]string)
SetTags set logging tags
func (*ConsoleWriter) SetTimeRotated ¶
func (writer *ConsoleWriter) SetTimeRotated(timeRotated bool)
SetTimeRotated do nothing
func (*ConsoleWriter) Tags ¶ added in v0.5.9
func (writer *ConsoleWriter) Tags() map[string]string
Tags return logging tags
func (*ConsoleWriter) TimeRotated ¶
func (writer *ConsoleWriter) TimeRotated() bool
TimeRotated do nothing
func (*ConsoleWriter) Tracef ¶
func (writer *ConsoleWriter) Tracef(format string, args ...interface{})
Tracef tracef
func (*ConsoleWriter) Warnf ¶
func (writer *ConsoleWriter) Warnf(format string, args ...interface{})
Warnf warnf
type Hook ¶
Hook Interface determine types of functions should be declared and implemented when user offers user defined function call before every logging action end. users may use this hook as a callback function when something happen. Fire function received two parameters. level is the level associate with that logging action. message is the formatted string already written.
type LevelType ¶
type LevelType int
LevelType type defined for logging level just use int
func LevelFromString ¶
LevelFromString return Level according to given string
type MultiWriter ¶
type MultiWriter struct {
// contains filtered or unexported fields
}
MultiWriter struct defines an instance for multi writers with different message level
func (*MultiWriter) Critical ¶
func (writer *MultiWriter) Critical(args ...interface{})
Critical critical
func (*MultiWriter) Criticalf ¶
func (writer *MultiWriter) Criticalf(format string, args ...interface{})
Criticalf criticalf
func (*MultiWriter) Debugf ¶
func (writer *MultiWriter) Debugf(format string, args ...interface{})
Debugf debugf
func (*MultiWriter) Errorf ¶
func (writer *MultiWriter) Errorf(format string, args ...interface{})
Errorf error
func (*MultiWriter) Infof ¶
func (writer *MultiWriter) Infof(format string, args ...interface{})
Infof infof
func (*MultiWriter) Level ¶
func (writer *MultiWriter) Level() LevelType
Level return logging level threshold
func (*MultiWriter) Retentions ¶
func (writer *MultiWriter) Retentions() int64
Retentions get retentions
func (*MultiWriter) RotateLines ¶
func (writer *MultiWriter) RotateLines() int
RotateLines get rotateLines
func (*MultiWriter) RotateSize ¶
func (writer *MultiWriter) RotateSize() int64
RotateSize get rotateSize
func (*MultiWriter) SetColored ¶
func (writer *MultiWriter) SetColored(colored bool)
SetColored set logging color
func (*MultiWriter) SetHook ¶
func (writer *MultiWriter) SetHook(hook Hook)
SetHook set hook for every logging actions
func (*MultiWriter) SetHookAsync ¶
func (writer *MultiWriter) SetHookAsync(async bool)
SetHookAsync set hook async for base file writer
func (*MultiWriter) SetHookLevel ¶
func (writer *MultiWriter) SetHookLevel(level LevelType)
SetHookLevel set when hook will be called
func (*MultiWriter) SetLevel ¶
func (writer *MultiWriter) SetLevel(level LevelType)
SetLevel set logging level threshold
func (*MultiWriter) SetRetentions ¶
func (writer *MultiWriter) SetRetentions(retentions int64)
SetRetentions set how many logs will keep after logrotate
func (*MultiWriter) SetRotateLines ¶
func (writer *MultiWriter) SetRotateLines(rotateLines int)
SetRotateLines set line number when logrotate
func (*MultiWriter) SetRotateSize ¶
func (writer *MultiWriter) SetRotateSize(rotateSize int64)
SetRotateSize set size when logroatate
func (*MultiWriter) SetTags ¶ added in v0.5.9
func (writer *MultiWriter) SetTags(tags map[string]string)
SetTags set logging tags
func (*MultiWriter) SetTimeRotated ¶
func (writer *MultiWriter) SetTimeRotated(timeRotated bool)
SetTimeRotated toggle time base logrotate
func (*MultiWriter) Tags ¶ added in v0.5.9
func (writer *MultiWriter) Tags() map[string]string
Tags return logging tags
func (*MultiWriter) TimeRotated ¶
func (writer *MultiWriter) TimeRotated() bool
TimeRotated get timeRotated
func (*MultiWriter) Tracef ¶
func (writer *MultiWriter) Tracef(format string, args ...interface{})
Tracef tracef
func (*MultiWriter) Warnf ¶
func (writer *MultiWriter) Warnf(format string, args ...interface{})
Warnf warnf
type SocketWriter ¶
type SocketWriter struct {
// contains filtered or unexported fields
}
SocketWriter is a socket logger
func (*SocketWriter) Critical ¶
func (writer *SocketWriter) Critical(args ...interface{})
Critical critical
func (*SocketWriter) Criticalf ¶
func (writer *SocketWriter) Criticalf(format string, args ...interface{})
Criticalf criticalf
func (*SocketWriter) Debugf ¶
func (writer *SocketWriter) Debugf(format string, args ...interface{})
Debugf debugf
func (*SocketWriter) Errorf ¶
func (writer *SocketWriter) Errorf(format string, args ...interface{})
Errorf error
func (*SocketWriter) Infof ¶
func (writer *SocketWriter) Infof(format string, args ...interface{})
Infof infof
func (*SocketWriter) Retentions ¶
func (writer *SocketWriter) Retentions() int64
Retentions do nothing
func (*SocketWriter) RotateLines ¶
func (writer *SocketWriter) RotateLines() int
RotateLines do nothing
func (*SocketWriter) RotateSize ¶
func (writer *SocketWriter) RotateSize() int64
RotateSize do nothing
func (*SocketWriter) SetColored ¶
func (writer *SocketWriter) SetColored(colored bool)
SetColored do nothing
func (*SocketWriter) SetHook ¶
func (writer *SocketWriter) SetHook(hook Hook)
SetHook set hook for logging action
func (*SocketWriter) SetHookAsync ¶
func (writer *SocketWriter) SetHookAsync(async bool)
SetHookAsync set hook async for base file writer
func (*SocketWriter) SetHookLevel ¶
func (writer *SocketWriter) SetHookLevel(level LevelType)
SetHookLevel set when hook will be called
func (*SocketWriter) SetLevel ¶
func (writer *SocketWriter) SetLevel(level LevelType)
SetLevel set logger level
func (*SocketWriter) SetRetentions ¶
func (writer *SocketWriter) SetRetentions(retentions int64)
SetRetentions do nothing
func (*SocketWriter) SetRotateLines ¶
func (writer *SocketWriter) SetRotateLines(rotateLines int)
SetRotateLines do nothing
func (*SocketWriter) SetRotateSize ¶
func (writer *SocketWriter) SetRotateSize(rotateSize int64)
SetRotateSize do nothing
func (*SocketWriter) SetTags ¶ added in v0.5.9
func (writer *SocketWriter) SetTags(tags map[string]string)
SetTags set logging tags
func (*SocketWriter) SetTimeRotated ¶
func (writer *SocketWriter) SetTimeRotated(timeRotated bool)
SetTimeRotated do nothing
func (*SocketWriter) Tags ¶ added in v0.5.9
func (writer *SocketWriter) Tags() map[string]string
Tags return logging tags
func (*SocketWriter) TimeRotated ¶
func (writer *SocketWriter) TimeRotated() bool
TimeRotated do nothing
func (*SocketWriter) Tracef ¶
func (writer *SocketWriter) Tracef(format string, args ...interface{})
Tracef tracef
func (*SocketWriter) Warnf ¶
func (writer *SocketWriter) Warnf(format string, args ...interface{})
Warnf warnf
type Writer ¶
type Writer interface { // Close do anything end before program end Close() // SetLevel set logging level threshold SetLevel(level LevelType) // Level get log level Level() LevelType Debug(args ...interface{}) Debugf(format string, args ...interface{}) Trace(args ...interface{}) Tracef(format string, args ...interface{}) Info(args ...interface{}) Infof(format string, args ...interface{}) Warn(args ...interface{}) Warnf(format string, args ...interface{}) Error(args ...interface{}) Errorf(format string, args ...interface{}) Critical(args ...interface{}) Criticalf(format string, args ...interface{}) // hook SetHook(hook Hook) SetHookLevel(level LevelType) SetHookAsync(async bool) // logrotate SetTimeRotated(timeRotated bool) TimeRotated() bool SetRotateSize(rotateSize int64) RotateSize() int64 SetRotateLines(rotateLines int) RotateLines() int SetRetentions(retentions int64) Retentions() int64 SetColored(colored bool) Colored() bool // tags SetTags(tags map[string]string) Tags() map[string]string // contains filtered or unexported methods }
Writer interface is a common definition of any writers in this package. Any struct implements Writer interface must implement functions below. Close is used for close the writer and free any elements if needed. write is an internal function that write pure message with specific logging level. writef is an internal function that formatting message with specific logging level. Placeholders in the format string will be replaced with args given. Both write and writef may have an asynchronous call of user defined function before write and writef function end..