代码拉取完成,页面将自动刷新
package log
import (
"fmt"
"os"
"runtime"
"strconv"
"sync"
"time"
)
const (
_VER string = "1.0.0"
)
type LEVEL int32
var logLevel LEVEL = 1
var maxFileSize int64
var maxFileCount int32
var dailyRolling bool = true
var consoleAppender bool = true
var RollingFile bool = false
var logObj *_FILE
var srcRoot string
const DATEFORMAT = "2006-01-02"
type UNIT int64
const (
_ = iota
KB UNIT = 1 << (iota * 10)
MB
GB
TB
)
const (
ALL LEVEL = iota
TRACE
DEBUG
INFO
WARN
ERROR
FATAL
OFF
)
type _FILE struct {
dir string
filename string
_suffix int
isCover bool
_date *time.Time
mu *sync.Mutex
logfile *os.File
}
func init() {
}
func SetSourceRoot(src string) {
srcRoot = src
}
func SetConsole(isConsole bool) {
consoleAppender = isConsole
}
func SetLevel(_level LEVEL) {
logLevel = _level
}
func SetRollingFile(fileDir, fileName string, maxNumber int32, maxSize int64, _unit UNIT) {
maxFileCount = maxNumber
maxFileSize = maxSize * int64(_unit)
RollingFile = true
dailyRolling = false
logObj = &_FILE{dir: fileDir, filename: fileName, isCover: false, mu: new(sync.Mutex)}
for i := 1; i <= int(maxNumber); i++ {
if isExist(fileDir + "/" + fileName + "." + strconv.Itoa(i)) {
logObj._suffix = i
} else {
break
}
}
if !logObj.isMustRename() {
logObj.logfile, _ = os.OpenFile(fileDir+"/"+fileName, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0)
} else {
logObj.rename()
}
go fileMonitor()
}
func SetRollingDaily(fileDir, fileName string) {
RollingFile = false
dailyRolling = true
t, _ := time.Parse(DATEFORMAT, time.Now().Format(DATEFORMAT))
logObj = &_FILE{dir: fileDir, filename: fileName, _date: &t, isCover: false, mu: new(sync.Mutex)}
if !logObj.isMustRename() {
logObj.logfile, _ = os.OpenFile(fileDir+"/"+fileName, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0)
} else {
logObj.rename()
}
}
func Trace(v ...interface{}) {
if logLevel <= TRACE {
appendLog("TRC", v)
}
}
func Debug(v ...interface{}) {
if logLevel <= DEBUG {
appendLog("DBG", v)
}
}
func Info(v ...interface{}) {
if logLevel <= INFO {
appendLog("INF", v)
}
}
func Warn(v ...interface{}) {
if logLevel <= DEBUG {
appendLog("WRN", v)
}
}
func Error(v ...interface{}) {
if logLevel <= ERROR {
appendLog("ERR", v)
}
}
func Fatal(v ...interface{}) {
if logLevel <= FATAL {
appendLog("FAT", v)
}
}
func appendLog(t string, s []interface{}) {
timestamp := time.Now().Unix()
tm := time.Unix(timestamp, 0)
logObj.mu.Lock()
defer logObj.mu.Unlock()
_, file, line, _ := runtime.Caller(2)
// short := file
// for i := len(file) - 1; i > 0; i-- {
// if file[i] == '/' {
// short = file[i+1:]
// break
// }
// }
// file = short
// i := len(srcRoot)
// file = file[i:]
text := fmt.Sprint(s...)
msg := fmt.Sprintf("%s [%s] %s (%s:%d)\n", tm.Format("2006/01/02 15:04:05"), t, text, file, line)
appendFileLog(msg)
if consoleAppender {
fmt.Print(msg)
}
}
func appendFileLog(msg string) {
if dailyRolling {
fileCheck()
}
//defer catchError()
logObj.logfile.Write([]byte(msg))
}
func catchError() {
if err := recover(); err != nil {
//log.Println("err", err)
}
}
func (f *_FILE) isMustRename() bool {
if dailyRolling {
t, _ := time.Parse(DATEFORMAT, time.Now().Format(DATEFORMAT))
if t.After(*f._date) {
return true
}
} else {
if maxFileCount > 1 {
if fileSize(f.dir+"/"+f.filename) >= maxFileSize {
return true
}
}
}
return false
}
func (f *_FILE) rename() {
if dailyRolling {
fn := f.dir + "/" + f.filename + "." + f._date.Format(DATEFORMAT)
if !isExist(fn) && f.isMustRename() {
if f.logfile != nil {
f.logfile.Close()
}
err := os.Rename(f.dir+"/"+f.filename, fn)
if err != nil {
//f.lg.Println("rename err", err.Error())
}
t, _ := time.Parse(DATEFORMAT, time.Now().Format(DATEFORMAT))
f._date = &t
f.logfile, _ = os.Create(f.dir + "/" + f.filename)
//f.lg = log.New(logObj.logfile, "\n", log.Ldate|log.Ltime|log.Lshortfile)
}
} else {
f.coverNextOne()
}
}
func (f *_FILE) nextSuffix() int {
return int(f._suffix%int(maxFileCount) + 1)
}
func (f *_FILE) coverNextOne() {
f._suffix = f.nextSuffix()
if f.logfile != nil {
f.logfile.Close()
}
if isExist(f.dir + "/" + f.filename + "." + strconv.Itoa(int(f._suffix))) {
os.Remove(f.dir + "/" + f.filename + "." + strconv.Itoa(int(f._suffix)))
}
os.Rename(f.dir+"/"+f.filename, f.dir+"/"+f.filename+"."+strconv.Itoa(int(f._suffix)))
f.logfile, _ = os.Create(f.dir + "/" + f.filename)
//f.lg = log.New(logObj.logfile, "\n", log.Ldate|log.Ltime|log.Lshortfile)
}
func fileSize(file string) int64 {
//fmt.Println("fileSize", file)
f, e := os.Stat(file)
if e != nil {
fmt.Println(e.Error())
return 0
}
return f.Size()
}
func isExist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
func fileMonitor() {
timer := time.NewTicker(1 * time.Second)
for {
select {
case <-timer.C:
fileCheck()
}
}
}
func fileCheck() {
defer func() {
if err := recover(); err != nil {
//log.Println(err)
}
}()
if logObj != nil && logObj.isMustRename() {
logObj.mu.Lock()
defer logObj.mu.Unlock()
logObj.rename()
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。