变更为 simplelog.
Signed-off-by: rick.chan <cy@haoan119.com>
This commit is contained in:
parent
6beb5b4b6a
commit
7bcab53f68
@ -1,4 +1,4 @@
|
|||||||
package iotlog
|
package simplelog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -7,7 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//FileHandler writes log to a file.
|
// FileHandler writes log to a file.
|
||||||
type FileHandler struct {
|
type FileHandler struct {
|
||||||
fd *os.File
|
fd *os.File
|
||||||
}
|
}
|
||||||
@ -34,10 +34,10 @@ func (h *FileHandler) Close() error {
|
|||||||
return h.fd.Close()
|
return h.fd.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
//RotatingFileHandler writes log a file, if file size exceeds maxBytes,
|
// RotatingFileHandler writes log a file, if file size exceeds maxBytes,
|
||||||
//it will backup current file and open a new one.
|
// it will backup current file and open a new one.
|
||||||
//
|
//
|
||||||
//max backup file number is set by backupCount, it will delete oldest if backups too many.
|
// max backup file number is set by backupCount, it will delete oldest if backups too many.
|
||||||
type RotatingFileHandler struct {
|
type RotatingFileHandler struct {
|
||||||
fd *os.File
|
fd *os.File
|
||||||
|
|
||||||
@ -129,11 +129,11 @@ func (h *RotatingFileHandler) doRollover() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TimedRotatingFileHandler writes log to a file,
|
// TimedRotatingFileHandler writes log to a file,
|
||||||
//it will backup current and open a new one, with a period time you sepecified.
|
// it will backup current and open a new one, with a period time you sepecified.
|
||||||
//
|
//
|
||||||
//refer: http://docs.python.org/2/library/logging.handlers.html.
|
// refer: http://docs.python.org/2/library/logging.handlers.html.
|
||||||
//same like python TimedRotatingFileHandler.
|
// same like python TimedRotatingFileHandler.
|
||||||
type TimedRotatingFileHandler struct {
|
type TimedRotatingFileHandler struct {
|
||||||
fd *os.File
|
fd *os.File
|
||||||
|
|
||||||
|
4
go.mod
4
go.mod
@ -1,3 +1,3 @@
|
|||||||
module gitmiot.vip.cpolar.top/Libs-Go/iotlog
|
module blacktea.vip.cpolar.top/OrgGo/simplelog
|
||||||
|
|
||||||
go 1.16
|
go 1.21.0
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
|
// Expanded from github.com/siddontang/go/tree/master/log
|
||||||
//
|
//
|
||||||
// It also supports different log handlers which you can log to stdout, file, socket, etc...
|
// It also supports different log handlers which you can log to stdout, file, socket, etc...
|
||||||
//
|
//
|
||||||
// Use
|
// Usage
|
||||||
//
|
//
|
||||||
// import "github.com/siddontang/go/log"
|
// import "blacktea.vip.cpolar.top/OrgGo/simplelog"
|
||||||
//
|
//
|
||||||
// //log with different level
|
// //log with different level
|
||||||
// log.Info("hello world")
|
// simplelog.Info("hello world")
|
||||||
// log.Error("hello world")
|
// simplelog.Error("hello world")
|
||||||
//
|
//
|
||||||
// //create a IotLog with specified handler
|
// //create a simplelog with specified handler
|
||||||
// h := NewStreamHandle(os.Stdout)
|
// h := NewStreamHandle(os.Stdout)
|
||||||
// l := log.NewDefault(h)
|
// l := simplelog.NewDefault(h)
|
||||||
// l.Info("hello world")
|
// l.Info("hello world")
|
||||||
// l.Infof("%s %d", "hello", 123)
|
// l.Infof("%s %d", "hello", 123)
|
||||||
//
|
package simplelog
|
||||||
package iotlog
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -28,7 +28,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//log level, from low to high, more high means more serious
|
// log level, from low to high, more higher means more serious
|
||||||
const (
|
const (
|
||||||
LevelTrace = iota
|
LevelTrace = iota
|
||||||
LevelDebug
|
LevelDebug
|
||||||
@ -39,9 +39,9 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Ltime = 1 << iota //time format "2006/01/02 15:04:05"
|
Ltime = 1 << iota //print timestamp format as "2006/01/02 15:04:05"
|
||||||
Lfile //file.go:123
|
Lfile // print file name and line format as file.go:123
|
||||||
Llevel //[Trace|Debug|Info...]
|
Llevel // print log level format as [Trace|Debug|Info...]
|
||||||
)
|
)
|
||||||
|
|
||||||
var LevelName [6]string = [6]string{"Trace", "Debug", "Info ", "Warn ", "Error", "Fatal"}
|
var LevelName [6]string = [6]string{"Trace", "Debug", "Info ", "Warn ", "Error", "Fatal"}
|
||||||
@ -60,7 +60,7 @@ func (i *atomicInt32) Get() int {
|
|||||||
return int(atomic.LoadInt32((*int32)(i)))
|
return int(atomic.LoadInt32((*int32)(i)))
|
||||||
}
|
}
|
||||||
|
|
||||||
type IotLog struct {
|
type SimpleLog struct {
|
||||||
level atomicInt32
|
level atomicInt32
|
||||||
flag int
|
flag int
|
||||||
|
|
||||||
@ -76,6 +76,7 @@ type IotLog struct {
|
|||||||
// 初始化函数
|
// 初始化函数
|
||||||
// 参数 name 为 log 文件名
|
// 参数 name 为 log 文件名
|
||||||
// 参数 level 为:
|
// 参数 level 为:
|
||||||
|
//
|
||||||
// LevelTrace
|
// LevelTrace
|
||||||
// LevelDebug
|
// LevelDebug
|
||||||
// LevelInfo
|
// LevelInfo
|
||||||
@ -83,12 +84,14 @@ type IotLog struct {
|
|||||||
// LevelError
|
// LevelError
|
||||||
// LevelFatal
|
// LevelFatal
|
||||||
// 其中之一
|
// 其中之一
|
||||||
|
//
|
||||||
// 参数 flag 可以为
|
// 参数 flag 可以为
|
||||||
|
//
|
||||||
// log.Ltime
|
// log.Ltime
|
||||||
// log.Lfile
|
// log.Lfile
|
||||||
// log.Llevel
|
// log.Llevel
|
||||||
// 的组合
|
// 的组合
|
||||||
func (l *IotLog) Init(handler StreamHandler, level, flag int) *IotLog {
|
func (l *SimpleLog) Init(handler StreamHandler, level, flag int) *SimpleLog {
|
||||||
l.level.Set(level)
|
l.level.Set(level)
|
||||||
l.handler = handler
|
l.handler = handler
|
||||||
|
|
||||||
@ -100,7 +103,7 @@ func (l *IotLog) Init(handler StreamHandler, level, flag int) *IotLog {
|
|||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *IotLog) InitStd(level, flag int) *IotLog {
|
func (l *SimpleLog) InitStd(level, flag int) *SimpleLog {
|
||||||
handler, e := NewStreamHandle(os.Stdout)
|
handler, e := NewStreamHandle(os.Stdout)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
panic(e)
|
panic(e)
|
||||||
@ -109,7 +112,7 @@ func (l *IotLog) InitStd(level, flag int) *IotLog {
|
|||||||
return l.Init(handler, level, flag)
|
return l.Init(handler, level, flag)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *IotLog) InitFile(name string, level, flag int) *IotLog {
|
func (l *SimpleLog) InitFile(name string, level, flag int) *SimpleLog {
|
||||||
handler, e := new(FileHandler).InitFile(name)
|
handler, e := new(FileHandler).InitFile(name)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
panic(e)
|
panic(e)
|
||||||
@ -118,7 +121,7 @@ func (l *IotLog) InitFile(name string, level, flag int) *IotLog {
|
|||||||
return l.Init(handler, level, flag)
|
return l.Init(handler, level, flag)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *IotLog) InitRotating(name string, maxBytes, backupCount, level int) *IotLog {
|
func (l *SimpleLog) InitRotating(name string, maxBytes, backupCount, level int) *SimpleLog {
|
||||||
handler, e := new(RotatingFileHandler).InitRotating(name, maxBytes, backupCount)
|
handler, e := new(RotatingFileHandler).InitRotating(name, maxBytes, backupCount)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
panic(e)
|
panic(e)
|
||||||
@ -127,7 +130,7 @@ func (l *IotLog) InitRotating(name string, maxBytes, backupCount, level int) *Io
|
|||||||
return l.Init(handler, level, Ltime|Lfile|Llevel)
|
return l.Init(handler, level, Ltime|Lfile|Llevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *IotLog) InitTimedRotating(name string, when int8, interval, level int) *IotLog {
|
func (l *SimpleLog) InitTimedRotating(name string, when int8, interval, level int) *SimpleLog {
|
||||||
handler, e := new(TimedRotatingFileHandler).InitTimedRotating(name, when, interval)
|
handler, e := new(TimedRotatingFileHandler).InitTimedRotating(name, when, interval)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
panic(e)
|
panic(e)
|
||||||
@ -136,7 +139,7 @@ func (l *IotLog) InitTimedRotating(name string, when int8, interval, level int)
|
|||||||
return l.Init(handler, level, Ltime|Lfile|Llevel)
|
return l.Init(handler, level, Ltime|Lfile|Llevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *IotLog) popBuf() []byte {
|
func (l *SimpleLog) popBuf() []byte {
|
||||||
l.bufMutex.Lock()
|
l.bufMutex.Lock()
|
||||||
var buf []byte
|
var buf []byte
|
||||||
if len(l.bufs) == 0 {
|
if len(l.bufs) == 0 {
|
||||||
@ -150,7 +153,7 @@ func (l *IotLog) popBuf() []byte {
|
|||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *IotLog) putBuf(buf []byte) {
|
func (l *SimpleLog) putBuf(buf []byte) {
|
||||||
l.bufMutex.Lock()
|
l.bufMutex.Lock()
|
||||||
if len(l.bufs) < maxBufPoolSize {
|
if len(l.bufs) < maxBufPoolSize {
|
||||||
buf = buf[0:0]
|
buf = buf[0:0]
|
||||||
@ -159,7 +162,7 @@ func (l *IotLog) putBuf(buf []byte) {
|
|||||||
l.bufMutex.Unlock()
|
l.bufMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *IotLog) Close() {
|
func (l *SimpleLog) Close() {
|
||||||
if l.closed.Get() == 1 {
|
if l.closed.Get() == 1 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -168,13 +171,13 @@ func (l *IotLog) Close() {
|
|||||||
l.handler.Close()
|
l.handler.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
//set log level, any log level less than it will not log
|
// set log level, any log level less than it will not log
|
||||||
func (l *IotLog) SetLevel(level int) {
|
func (l *SimpleLog) SetLevel(level int) {
|
||||||
l.level.Set(level)
|
l.level.Set(level)
|
||||||
}
|
}
|
||||||
|
|
||||||
// name can be in ["trace", "debug", "info", "warn", "error", "fatal"]
|
// name can be in ["trace", "debug", "info", "warn", "error", "fatal"]
|
||||||
func (l *IotLog) SetLevelByName(name string) {
|
func (l *SimpleLog) SetLevelByName(name string) {
|
||||||
name = strings.ToLower(name)
|
name = strings.ToLower(name)
|
||||||
switch name {
|
switch name {
|
||||||
case "trace":
|
case "trace":
|
||||||
@ -192,7 +195,7 @@ func (l *IotLog) SetLevelByName(name string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *IotLog) SetHandler(h StreamHandler) {
|
func (l *SimpleLog) SetHandler(h StreamHandler) {
|
||||||
if l.closed.Get() == 1 {
|
if l.closed.Get() == 1 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -205,7 +208,7 @@ func (l *IotLog) SetHandler(h StreamHandler) {
|
|||||||
l.hMutex.Unlock()
|
l.hMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *IotLog) Output(callDepth int, level int, format string, v ...interface{}) {
|
func (l *SimpleLog) Output(callDepth int, level int, format string, v ...interface{}) {
|
||||||
if l.closed.Get() == 1 {
|
if l.closed.Get() == 1 {
|
||||||
// closed
|
// closed
|
||||||
return
|
return
|
||||||
@ -273,62 +276,62 @@ func (l *IotLog) Output(callDepth int, level int, format string, v ...interface{
|
|||||||
l.putBuf(buf)
|
l.putBuf(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
//log with Trace level
|
// log with Trace level
|
||||||
func (l *IotLog) Trace(v ...interface{}) {
|
func (l *SimpleLog) Trace(v ...interface{}) {
|
||||||
l.Output(2, LevelTrace, "", v...)
|
l.Output(2, LevelTrace, "", v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
//log with Debug level
|
// log with Debug level
|
||||||
func (l *IotLog) Debug(v ...interface{}) {
|
func (l *SimpleLog) Debug(v ...interface{}) {
|
||||||
l.Output(2, LevelDebug, "", v...)
|
l.Output(2, LevelDebug, "", v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
//log with info level
|
// log with info level
|
||||||
func (l *IotLog) Info(v ...interface{}) {
|
func (l *SimpleLog) Info(v ...interface{}) {
|
||||||
l.Output(2, LevelInfo, "", v...)
|
l.Output(2, LevelInfo, "", v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
//log with warn level
|
// log with warn level
|
||||||
func (l *IotLog) Warn(v ...interface{}) {
|
func (l *SimpleLog) Warn(v ...interface{}) {
|
||||||
l.Output(2, LevelWarn, "", v...)
|
l.Output(2, LevelWarn, "", v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
//log with error level
|
// log with error level
|
||||||
func (l *IotLog) Error(v ...interface{}) {
|
func (l *SimpleLog) Error(v ...interface{}) {
|
||||||
l.Output(2, LevelError, "", v...)
|
l.Output(2, LevelError, "", v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
//log with fatal level
|
// log with fatal level
|
||||||
func (l *IotLog) Fatal(v ...interface{}) {
|
func (l *SimpleLog) Fatal(v ...interface{}) {
|
||||||
l.Output(2, LevelFatal, "", v...)
|
l.Output(2, LevelFatal, "", v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
//log with Trace level
|
// log with Trace level
|
||||||
func (l *IotLog) Tracef(format string, v ...interface{}) {
|
func (l *SimpleLog) Tracef(format string, v ...interface{}) {
|
||||||
l.Output(2, LevelTrace, format, v...)
|
l.Output(2, LevelTrace, format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
//log with Debug level
|
// log with Debug level
|
||||||
func (l *IotLog) Debugf(format string, v ...interface{}) {
|
func (l *SimpleLog) Debugf(format string, v ...interface{}) {
|
||||||
l.Output(2, LevelDebug, format, v...)
|
l.Output(2, LevelDebug, format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
//log with info level
|
// log with info level
|
||||||
func (l *IotLog) Infof(format string, v ...interface{}) {
|
func (l *SimpleLog) Infof(format string, v ...interface{}) {
|
||||||
l.Output(2, LevelInfo, format, v...)
|
l.Output(2, LevelInfo, format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
//log with warn level
|
// log with warn level
|
||||||
func (l *IotLog) Warnf(format string, v ...interface{}) {
|
func (l *SimpleLog) Warnf(format string, v ...interface{}) {
|
||||||
l.Output(2, LevelWarn, format, v...)
|
l.Output(2, LevelWarn, format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
//log with error level
|
// log with error level
|
||||||
func (l *IotLog) Errorf(format string, v ...interface{}) {
|
func (l *SimpleLog) Errorf(format string, v ...interface{}) {
|
||||||
l.Output(2, LevelError, format, v...)
|
l.Output(2, LevelError, format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
//log with fatal level
|
// log with fatal level
|
||||||
func (l *IotLog) Fatalf(format string, v ...interface{}) {
|
func (l *SimpleLog) Fatalf(format string, v ...interface{}) {
|
||||||
l.Output(2, LevelFatal, format, v...)
|
l.Output(2, LevelFatal, format, v...)
|
||||||
}
|
}
|
@ -1,16 +1,16 @@
|
|||||||
package iotlog
|
package simplelog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
//StreamHandler writes logs to somewhere
|
// StreamHandler writes logs to somewhere
|
||||||
type StreamHandler interface {
|
type StreamHandler interface {
|
||||||
Write(p []byte) (n int, err error)
|
Write(p []byte) (n int, err error)
|
||||||
Close() error
|
Close() error
|
||||||
}
|
}
|
||||||
|
|
||||||
//StreamHandle writes logs to a specified io Writer, maybe stdout, stderr, etc...
|
// StreamHandle writes logs to a specified io Writer, maybe stdout, stderr, etc...
|
||||||
type StreamHandle struct {
|
type StreamHandle struct {
|
||||||
w io.Writer
|
w io.Writer
|
||||||
}
|
}
|
||||||
@ -31,7 +31,7 @@ func (h *StreamHandle) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//NullHandler does nothing, it discards anything.
|
// NullHandler does nothing, it discards anything.
|
||||||
type NullHandler struct {
|
type NullHandler struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user