瀏覽代碼

fix:日志文件提取

wangkaiyue 3 年之前
父節點
當前提交
a8d047baac

+ 0 - 73
src/jfw/modules/app/src/app/front/jylog.go

@@ -1,73 +0,0 @@
-package front
-
-/**
-日志文件自动切换,默认保留15天内日志
-**/
-
-import (
-	"log"
-	"os"
-	"path/filepath"
-	"regexp"
-	"time"
-
-	"github.com/go-xweb/xweb"
-	"github.com/robfig/cron"
-)
-
-//日志格式
-var fileReg = regexp.MustCompile("^(\\d{4}_[0-9_]{14})\\.log$")
-
-//当前日志文件句柄
-var LogFile *os.File
-
-//时间格式
-var FMT = "2006_01_02_15_04_05"
-
-//日志目录
-var LogPath = "./jylog"
-
-func init() {
-	os.Mkdir(LogPath, os.ModePerm)
-	//默认保留15天内的日志,-1为永久保留
-	initLog(15)
-}
-
-func initLog(saveDay int) {
-	go logfile()
-	task := cron.New()
-	task.Start()
-	task.AddFunc("0 0 0 * * ?", func() {
-		go logfile()
-		time.Sleep(50 * time.Second)
-		if saveDay > 0 {
-			filepath.Walk(LogPath, func(path string, info os.FileInfo, err error) error {
-				str := fileReg.FindStringSubmatch(info.Name())
-				if len(str) == 2 {
-					t, er := time.ParseInLocation(FMT, str[1], time.Local)
-					if er == nil {
-						if (time.Now().Unix()-t.Unix())/86400 > int64(saveDay) {
-							log.Println("delete log file:", path, os.Remove(path))
-						}
-					}
-				}
-				return nil
-			})
-		}
-	})
-}
-
-//创建并切换输出文件
-func logfile() {
-	now := time.Now().Format(FMT)
-	file, _ := os.Create(LogPath + "/" + now + ".log")
-	log.SetOutput(file)
-	xweb.RootApp().Logger.SetOutput(file)
-	go func(file *os.File) {
-		time.Sleep(5 * time.Second)
-		if LogFile != nil {
-			LogFile.Close()
-		}
-		LogFile = file
-	}(file)
-}

+ 1 - 0
src/jfw/modules/app/src/main.go

@@ -12,6 +12,7 @@ import (
 	"jfw/public"
 	"net/http"
 	"qfw/util"
+	_ "qfw/util/jylog" //日志文件
 	"strings"
 	"time"
 

+ 1 - 0
src/jfw/modules/bigmember/src/main.go

@@ -7,6 +7,7 @@ import (
 	_ "entinfo"
 	_ "filter"
 	"net/http"
+	_ "qfw/util/jylog" //日志文件
 	_ "service"
 
 	"github.com/go-xweb/xweb"

+ 0 - 73
src/jfw/modules/bigmember/src/util/jylog.go

@@ -1,73 +0,0 @@
-package util
-
-/**
-日志文件自动切换,默认保留15天内日志
-**/
-
-import (
-	"log"
-	"os"
-	"path/filepath"
-	"regexp"
-	"time"
-
-	"github.com/go-xweb/xweb"
-	"github.com/robfig/cron"
-)
-
-//日志格式
-var fileReg = regexp.MustCompile("^(\\d{4}_[0-9_]{14})\\.log$")
-
-//当前日志文件句柄
-var LogFile *os.File
-
-//时间格式
-var FMT = "2006_01_02_15_04_05"
-
-//日志目录
-var LogPath = "./jylog"
-
-func init() {
-	os.Mkdir(LogPath, os.ModePerm)
-	//默认保留15天内的日志,-1为永久保留
-	initLog(15)
-}
-
-func initLog(saveDay int) {
-	go logfile()
-	task := cron.New()
-	task.Start()
-	task.AddFunc("0 0 0 * * ?", func() {
-		go logfile()
-		time.Sleep(50 * time.Second)
-		if saveDay > 0 {
-			filepath.Walk(LogPath, func(path string, info os.FileInfo, err error) error {
-				str := fileReg.FindStringSubmatch(info.Name())
-				if len(str) == 2 {
-					t, er := time.ParseInLocation(FMT, str[1], time.Local)
-					if er == nil {
-						if (time.Now().Unix()-t.Unix())/86400 > int64(saveDay) {
-							log.Println("delete log file:", path, os.Remove(path))
-						}
-					}
-				}
-				return nil
-			})
-		}
-	})
-}
-
-//创建并切换输出文件
-func logfile() {
-	now := time.Now().Format(FMT)
-	file, _ := os.Create(LogPath + "/" + now + ".log")
-	log.SetOutput(file)
-	xweb.RootApp().Logger.SetOutput(file)
-	go func(file *os.File) {
-		time.Sleep(5 * time.Second)
-		if LogFile != nil {
-			LogFile.Close()
-		}
-		LogFile = file
-	}(file)
-}

+ 1 - 1
src/jfw/front/jylog.go → src/jfw/modules/common/src/qfw/util/jylog/jylog.go

@@ -1,4 +1,4 @@
-package front
+package jylog
 
 /**
 日志文件自动切换,默认保留15天内日志

+ 0 - 73
src/jfw/modules/publicapply/src/a/jylog.go

@@ -1,73 +0,0 @@
-package a
-
-/**
-日志文件自动切换,默认保留15天内日志
-**/
-
-import (
-	"log"
-	"os"
-	"path/filepath"
-	"regexp"
-	"time"
-
-	"github.com/go-xweb/xweb"
-	"github.com/robfig/cron"
-)
-
-//日志格式
-var fileReg = regexp.MustCompile("^(\\d{4}_[0-9_]{14})\\.log$")
-
-//当前日志文件句柄
-var LogFile *os.File
-
-//时间格式
-var FMT = "2006_01_02_15_04_05"
-
-//日志目录
-var LogPath = "./jylog"
-
-func init() {
-	os.Mkdir(LogPath, os.ModePerm)
-	//默认保留15天内的日志,-1为永久保留
-	initLog(15)
-}
-
-func initLog(saveDay int) {
-	go logfile()
-	task := cron.New()
-	task.Start()
-	task.AddFunc("0 0 0 * * ?", func() {
-		go logfile()
-		time.Sleep(50 * time.Second)
-		if saveDay > 0 {
-			filepath.Walk(LogPath, func(path string, info os.FileInfo, err error) error {
-				str := fileReg.FindStringSubmatch(info.Name())
-				if len(str) == 2 {
-					t, er := time.ParseInLocation(FMT, str[1], time.Local)
-					if er == nil {
-						if (time.Now().Unix()-t.Unix())/86400 > int64(saveDay) {
-							log.Println("delete log file:", path, os.Remove(path))
-						}
-					}
-				}
-				return nil
-			})
-		}
-	})
-}
-
-//创建并切换输出文件
-func logfile() {
-	now := time.Now().Format(FMT)
-	file, _ := os.Create(LogPath + "/" + now + ".log")
-	log.SetOutput(file)
-	xweb.RootApp().Logger.SetOutput(file)
-	go func(file *os.File) {
-		time.Sleep(5 * time.Second)
-		if LogFile != nil {
-			LogFile.Close()
-		}
-		LogFile = file
-	}(file)
-}

+ 1 - 1
src/jfw/modules/publicapply/src/main.go

@@ -17,10 +17,10 @@ import (
 	_ "me"
 	"net/http"
 	_ "project"
+	_ "qfw/util/jylog" //日志文件
 	_ "shareFission"
 	_ "subscribe"
 	_ "subscribePush"
-
 	_ "userbase"
 
 	"github.com/go-xweb/xweb"

+ 1 - 0
src/jfw/modules/subscribepay/src/main.go

@@ -8,6 +8,7 @@ import (
 	"net/http"
 	"net/rpc"
 	"qfw/util/endless"
+	_ "qfw/util/jylog" //日志文件
 	"rpcfollow"
 	_ "service"
 	"timetask"

+ 0 - 73
src/jfw/modules/subscribepay/src/util/jylog.go

@@ -1,73 +0,0 @@
-package util
-
-/**
-日志文件自动切换,默认保留15天内日志
-**/
-
-import (
-	"log"
-	"os"
-	"path/filepath"
-	"regexp"
-	"time"
-
-	"github.com/go-xweb/xweb"
-	"github.com/robfig/cron"
-)
-
-//日志格式
-var fileReg = regexp.MustCompile("^(\\d{4}_[0-9_]{14})\\.log$")
-
-//当前日志文件句柄
-var LogFile *os.File
-
-//时间格式
-var FMT = "2006_01_02_15_04_05"
-
-//日志目录
-var LogPath = "./jylog"
-
-func init() {
-	os.Mkdir(LogPath, os.ModePerm)
-	//默认保留15天内的日志,-1为永久保留
-	initLog(15)
-}
-
-func initLog(saveDay int) {
-	go logfile()
-	task := cron.New()
-	task.Start()
-	task.AddFunc("0 0 0 * * ?", func() {
-		go logfile()
-		time.Sleep(50 * time.Second)
-		if saveDay > 0 {
-			filepath.Walk(LogPath, func(path string, info os.FileInfo, err error) error {
-				str := fileReg.FindStringSubmatch(info.Name())
-				if len(str) == 2 {
-					t, er := time.ParseInLocation(FMT, str[1], time.Local)
-					if er == nil {
-						if (time.Now().Unix()-t.Unix())/86400 > int64(saveDay) {
-							log.Println("delete log file:", path, os.Remove(path))
-						}
-					}
-				}
-				return nil
-			})
-		}
-	})
-}
-
-//创建并切换输出文件
-func logfile() {
-	now := time.Now().Format(FMT)
-	file, _ := os.Create(LogPath + "/" + now + ".log")
-	log.SetOutput(file)
-	xweb.RootApp().Logger.SetOutput(file)
-	go func(file *os.File) {
-		time.Sleep(5 * time.Second)
-		if LogFile != nil {
-			LogFile.Close()
-		}
-		LogFile = file
-	}(file)
-}

+ 1 - 0
src/jfw/modules/weixin/src/main.go

@@ -8,6 +8,7 @@ import (
 	"net/rpc"
 	"oauth"
 	"qfw/util/endless"
+	_ "qfw/util/jylog" //日志文件
 	_ "tools"
 	"wx"
 )

+ 0 - 73
src/jfw/modules/weixin/src/tools/wxlog.go

@@ -1,73 +0,0 @@
-package tools
-
-/**
-日志文件自动切换,默认保留15天内日志
-**/
-
-import (
-	"log"
-	"os"
-	"path/filepath"
-	"regexp"
-	"time"
-
-	"github.com/go-xweb/xweb"
-	"github.com/robfig/cron"
-)
-
-//日志格式
-var fileReg = regexp.MustCompile("^(\\d{4}_[0-9_]{14})\\.log$")
-
-//当前日志文件句柄
-var LogFile *os.File
-
-//时间格式
-var FMT = "2006_01_02_15_04_05"
-
-//日志目录
-var LogPath = "./wxlog"
-
-func init() {
-	os.Mkdir(LogPath, os.ModePerm)
-	//默认保留15天内的日志,-1为永久保留
-	initLog(15)
-}
-
-func initLog(saveDay int) {
-	go logfile()
-	task := cron.New()
-	task.Start()
-	task.AddFunc("0 0 0 * * ?", func() {
-		go logfile()
-		time.Sleep(50 * time.Second)
-		if saveDay > 0 {
-			filepath.Walk(LogPath, func(path string, info os.FileInfo, err error) error {
-				str := fileReg.FindStringSubmatch(info.Name())
-				if len(str) == 2 {
-					t, er := time.ParseInLocation(FMT, str[1], time.Local)
-					if er == nil {
-						if (time.Now().Unix()-t.Unix())/86400 > int64(saveDay) {
-							log.Println("delete log file:", path, os.Remove(path))
-						}
-					}
-				}
-				return nil
-			})
-		}
-	})
-}
-
-//创建并切换输出文件
-func logfile() {
-	now := time.Now().Format(FMT)
-	file, _ := os.Create(LogPath + "/" + now + ".log")
-	log.SetOutput(file)
-	xweb.RootApp().Logger.SetOutput(file)
-	go func(file *os.File) {
-		time.Sleep(5 * time.Second)
-		if LogFile != nil {
-			LogFile.Close()
-		}
-		LogFile = file
-	}(file)
-}

+ 1 - 1
src/main.go

@@ -3,7 +3,7 @@ package main
 import (
 	_ "jfw/active"
 	. "jfw/config"
-
+	_ "qfw/util/jylog" //日志文件
 	// _ "jfw/course"  线下课程 dev3.5.2 21年2.24 下线
 	_ "jfw/filter"
 	"jfw/front"