瀏覽代碼

feat:完善golog

renzheng 3 年之前
父節點
當前提交
8a3e9071f0
共有 7 個文件被更改,包括 212 次插入1 次删除
  1. 34 0
      .gitignore
  2. 34 1
      README.md
  3. 14 0
      go.mod
  4. 16 0
      go.sum
  5. 98 0
      golog/golog.go
  6. 15 0
      golog/golog_test.go
  7. 1 0
      jylog.go

+ 34 - 0
.gitignore

@@ -0,0 +1,34 @@
+# ---> Go
+# Compiled Object files, Static and Dynamic libs (Shared Objects)
+*.o
+*.a
+*.so
+
+# Folders
+_obj
+_test
+
+# Architecture specific extensions/prefixes
+*.[568vq]
+[568vq].out
+
+*.cgo1.go
+*.cgo2.c
+_cgo_defun.c
+_cgo_gotypes.go
+_cgo_export.*
+
+_testmain.go
+
+*.exe
+*.test
+*.prof
+
+src/*.exe
+src/src
+pkg
+
+golog/jylog
+golog/jylog/*
+golog/*.exe
+

+ 34 - 1
README.md

@@ -1 +1,34 @@
-jylog
+# jylog  
+> 日志工具类
+
+## golog
+> 简单的原生日志输出包  
+### 实现功能  
+- 保留日志,定时清理过期日志
+- 支持自定义输出流
+- 支持配置保留天数(默认15天)、配置清理时间(默认零点)
+- 支持输出日志到自定义目录
+
+### start
+
+(```)
+  
+  go get bp.jydev.jianyu360.cn/BP/jylog
+  import "bp.jydev.jianyu360.cn/BP/jylog"
+  
+    //4个参数,参数不分先后顺序,整数代表保留天数,函数代表输出流,字符串代表定时任务或日志文件目录
+    
+    //默认保留15天,每天0点清理,默认输出当前执行目录下jylog目录
+    InitGoLog()   
+    
+	InitGoLog(10)
+	
+	InitGoLog(xweb.RootApp().Logger.SetOutput)
+	
+	InitGoLog(7,xweb.RootApp().Logger.SetOutput) 
+	
+	InitGoLog(7,xweb.RootApp().Logger.SetOutput,"0 0 0 * * *") 
+	
+	InitGoLog(7,xweb.RootApp().Logger.SetOutput,"0 0 0 * * *","D:/jylog") 
+
+(```)

+ 14 - 0
go.mod

@@ -0,0 +1,14 @@
+module bp.jydev.jianyu360.cn/BP/jylog
+
+go 1.16
+
+require (
+	github.com/go-xweb/httpsession v0.0.0-20141220075701-356d3b4d38d6 // indirect
+	github.com/go-xweb/log v0.0.0-20140701090824-270d183ad77e // indirect
+	github.com/go-xweb/uuid v0.0.0-20140604020037-d7dce341f851 // indirect
+	github.com/go-xweb/xweb v0.2.1
+	github.com/howeyc/fsnotify v0.9.0 // indirect
+	github.com/lunny/csession v0.0.0-20130910075847-fe53c5de3dfd // indirect
+	github.com/mattn/go-sqlite3 v1.14.10 // indirect
+	github.com/robfig/cron v1.2.0
+)

+ 16 - 0
go.sum

@@ -0,0 +1,16 @@
+github.com/go-xweb/httpsession v0.0.0-20141220075701-356d3b4d38d6 h1:DUiWdm3rS8cC96Y0XeVkDeizzEK6X+qiNgXytLVtbkM=
+github.com/go-xweb/httpsession v0.0.0-20141220075701-356d3b4d38d6/go.mod h1:lwPk13GS+i/NK4FkMm68IcJrAwiu+HtjYa1Y4kW59aY=
+github.com/go-xweb/log v0.0.0-20140701090824-270d183ad77e h1:xmffs7hgrWpAOcquZrdlWpAEaAdlI9myaYcUUmhIP7k=
+github.com/go-xweb/log v0.0.0-20140701090824-270d183ad77e/go.mod h1:ASmYUSBf32lWkkNVX/pnOU4MLuUQpFH4qYHvWHt/l0w=
+github.com/go-xweb/uuid v0.0.0-20140604020037-d7dce341f851 h1:D46USD6oGNWzoJ/h5CWaFq3ELLoLoJzllJ03Xh78VYg=
+github.com/go-xweb/uuid v0.0.0-20140604020037-d7dce341f851/go.mod h1:OmDEC58ZYO1Esk+Uy32SB6LWof9lyROl7q76dBFOCWw=
+github.com/go-xweb/xweb v0.2.1 h1:u5t/ttuSfxiIMDTXj/Pouw9C2ASNABWT16JWHyrtdvY=
+github.com/go-xweb/xweb v0.2.1/go.mod h1:vPjYJgfidYAgBKIwiAyKFC1hfczlqsw9rRT8LtwrGew=
+github.com/howeyc/fsnotify v0.9.0 h1:0gtV5JmOKH4A8SsFxG2BczSeXWWPvcMT0euZt5gDAxY=
+github.com/howeyc/fsnotify v0.9.0/go.mod h1:41HzSPxBGeFRQKEEwgh49TRw/nKBsYZ2cF1OzPjSJsA=
+github.com/lunny/csession v0.0.0-20130910075847-fe53c5de3dfd h1:DXxmBCahjva4Ox4AWOv6pR1Csv33zSj97SaLOElfIsw=
+github.com/lunny/csession v0.0.0-20130910075847-fe53c5de3dfd/go.mod h1:3w9PScemEkJoLw3OYvLWMoD8XRCmXgGwsSpT6pFpJ0g=
+github.com/mattn/go-sqlite3 v1.14.10 h1:MLn+5bFRlWMGoSRmJour3CL1w/qL96mvipqpwQW/Sfk=
+github.com/mattn/go-sqlite3 v1.14.10/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
+github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
+github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=

+ 98 - 0
golog/golog.go

@@ -0,0 +1,98 @@
+package golog
+
+/**
+日志文件自动切换,默认保留15天内日志
+**/
+
+import (
+	"io"
+	"log"
+	"os"
+	"path/filepath"
+	"time"
+
+	"github.com/robfig/cron"
+)
+
+const (
+	//时间格式
+	FMT = "2006_01_02_15_04_05"
+)
+
+var (
+	//当前日志文件句柄
+	LogFile *os.File
+	//日志目录
+	LogPath = "./jylog"
+	//默认保留15天内的日志,-1为永久保留
+	SaveDays = 15
+	Cron     = "0 0 0 * * ?"
+)
+
+/*
+使用方法:
+需要调用 InitGoLog进行初始化,不传任何参数,默认保留15天、默认原生log输出
+InitGoLog()
+InitGoLog(10)
+InitGoLog(xweb.RootApp().Logger.SetOutput)
+InitGoLog(7,xweb.RootApp().Logger.SetOutput)
+*/
+
+func InitGoLog(args ...interface{}) {
+	fileFunc := []func(w io.Writer){log.SetOutput}
+	for _, v := range args {
+		if vd, ok := v.(int); ok {
+			SaveDays = vd
+		} else if vf, ok := v.(func(w io.Writer)); ok {
+			fileFunc = append(fileFunc, vf)
+		} else if vs, ok := v.(string); ok {
+			_, err := cron.ParseStandard(vs)
+			if err == nil {
+				Cron = vs
+			} else { // 输出位置
+				LogPath = vs
+				log.Println(vs, err)
+			}
+		}
+	}
+	log.Println(fileFunc, SaveDays, Cron)
+	os.Mkdir(LogPath, os.ModePerm)
+	go logfile(fileFunc...)
+	task := cron.New()
+	task.Start()
+	task.AddFunc(Cron, func() {
+		go logfile(fileFunc...)
+		time.Sleep(50 * time.Second)
+		if SaveDays > 0 {
+			filepath.Walk(LogPath, func(path string, info os.FileInfo, err error) error {
+				if (time.Now().Unix()-info.ModTime().Unix())/86400 > int64(SaveDays) {
+					log.Println("delete log file:", path, os.Remove(path))
+				}
+				return nil
+			})
+		}
+	})
+}
+
+//创建并切换输出文件
+func logfile(fileFunc ...func(w io.Writer)) {
+	now := time.Now().Format(FMT)
+	file, _ := os.Create(LogPath + "/" + now + ".log")
+	// log.SetOutput(file) // xweb.RootApp().Logger.SetOutput(file)
+	for _, func1 := range fileFunc {
+		func1(file)
+	}
+	go func(file *os.File) {
+		time.Sleep(5 * time.Second)
+		if LogFile != nil {
+			LogFile.Close()
+		}
+		LogFile = file
+	}(file)
+}
+
+//fileReg = regexp.MustCompile("^(\\d{4}_[0-9_]{14})\\.log$")
+// str := fileReg.FindStringSubmatch(info.Name())
+// if len(str) == 2 {
+// t, er := time.ParseInLocation(FMT, str[1], time.Local)
+// if er == nil {}}

+ 15 - 0
golog/golog_test.go

@@ -0,0 +1,15 @@
+package golog
+
+import (
+	"log"
+	"testing"
+	"time"
+)
+
+func Test_log1(t *testing.T) {
+	InitGoLog(7, "*/5 * * * * *", "D:/aaa")
+	for {
+		log.Println(time.Now().Format(FMT))
+		time.Sleep(5 * time.Second)
+	}
+}

+ 1 - 0
jylog.go

@@ -0,0 +1 @@
+package jylog