1 Star 1 Fork 0

zhuyuns/basic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
logger.go 3.17 KB
一键复制 编辑 原始数据 按行查看 历史
wing 提交于 2021-12-18 11:42 . 添加基础包
package cfg
import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"strings"
"sync"
"time"
)
var (
LOGFILE_MAXSIZE_DEFAULT int64 = 50 << 20
_logger_map map[string]*log.Logger
_logfile_maxsize int64 = LOGFILE_MAXSIZE_DEFAULT
_logfile_base string
_base_path string
)
var logger_map_max sync.RWMutex
func init() {
_logger_map = make(map[string]*log.Logger)
}
func GetLogger(typ string) *log.Logger {
logger_map_max.RLock()
if logger, ok := _logger_map[typ]; ok {
logger_map_max.RUnlock()
return logger
}
logger_map_max.RUnlock()
var dir string
tag :=""
if typ=="error"{
tag="error"
}
tmpDir, filename := path.Split(_logfile_base)
filenameArr := strings.Split(filename, ".")
if len(filenameArr) >= 1 {
dir = tmpDir + filenameArr[0] + "." + tag + ".log"
} else {
dir = tmpDir + tag + ".log"
}
file, err := OpenLogFile(dir, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Printf("error opening file %v\n", err)
return nil
}
logger := log.New(file, "", log.LstdFlags)
logger_map_max.Lock()
_logger_map[typ] = logger
logger_map_max.Unlock()
return logger
}
func InitLogLevel(lv int) {
_log_level = lv
}
func InitLogger(pathParam, logfile string, maxSize int64) {
var err error
var fullpath string
_base_path = pathParam
if filepath.IsAbs(logfile) {
fullpath = logfile
} else {
fullpath = path.Join(_base_path, "", logfile)
}
dir, filename := path.Split(logfile)
if filename == "" {
fullpath = path.Join(fullpath, "log")
}
_logfile_base = fullpath
dir = path.Join(_base_path, "", dir)
err = os.MkdirAll(dir, 0777)
if err != nil {
LogFatalf("MkdirAll err:%v,%v", err, dir)
return
}
_logfile_maxsize = int64(maxSize) << 10 //单位是k
if _logfile_maxsize < (1 << 16) { //日志文件最小64k
_logfile_maxsize = LOGFILE_MAXSIZE_DEFAULT
}
log.Println("logfile size:", _logfile_maxsize)
startLogger(fullpath)
}
func startLogger(logfile string) {
f, err := OpenLogFile(logfile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
log.Printf("cannot open logfile %v\n", err)
os.Exit(-1)
}
log.SetOutput(f)
log.SetFlags(log.LstdFlags)
}
func tmpLog(p *[]byte, format string, v ...interface{}) {
*p = append([]byte(fmt.Sprintf(format, v...)), (*p)...)
}
type logFile struct {
*os.File
}
func OpenLogFile(name string, flag int, perm os.FileMode) (file *logFile, err error) {
f, err := os.OpenFile(name, flag, perm)
if err != nil {
return nil, err
}
lf := logFile{}
lf.File = f
return &lf, nil
}
func (f *logFile) Write(p []byte) (int, error) {
fi, err := f.Stat()
if err != nil {
tmpLog(&p, "file.Stat err:%v.", err)
}
if fi.Size() >= _logfile_maxsize {
now := time.Now().Format("2006_01_02-15_04_05")
curFileName := f.Name()
newFileName := fmt.Sprintf("%s.%s", f.Name(), now)
err = os.Rename(curFileName, newFileName)
if err != nil {
tmpLog(&p, "[RAW] rename [%s] to [%s] err:%v\n",
curFileName, newFileName, err)
}
newFile, err := os.OpenFile(curFileName,
os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
tmpLog(&p, "[RAW] open file %s err:%v", curFileName, err)
} else {
f.File.Close()
f.File = newFile
}
}
return f.File.Write(p)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/zhuyuns/basic.git
git@gitee.com:zhuyuns/basic.git
zhuyuns
basic
basic
v0.0.55

搜索帮助