1 Star 0 Fork 0

Wsage/go-framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
log_default.go 3.01 KB
一键复制 编辑 原始数据 按行查看 历史
王少奇 提交于 2021-08-09 09:39 . 优化
package log
import (
"errors"
"fmt"
"gitee.com/scottq/go-framework/src/miscs"
"log"
"os"
"path"
"path/filepath"
"strconv"
"time"
)
type MyLoggerConfig struct {
Module string
Path string
PathAutoCreate bool
ConsoleOut bool
Flag int
}
func (c *MyLoggerConfig) LogPath() string {
p := c.Path
if p == "" {
p = DefaultLogPath
}
return p
}
type MyLogger struct {
logger *log.Logger
config *MyLoggerConfig
}
func (logger *MyLogger) Write(p []byte) (int, error) {
level := string(p[len(p)-2])
l, err := strconv.Atoi(level)
if err != nil {
return 0, err
}
levelStr, ok := LogLevelMap[l]
if !ok {
return 0, errors.New("log level not exists")
}
file, err := os.OpenFile(logger.LogPath(), os.O_CREATE|os.O_RDWR|os.O_APPEND, 0777)
if err != nil {
return 0, err
}
defer file.Close()
pl := []byte(fmt.Sprintf("%s %s", levelStr, string(p[:len(p)-2])))
if logger.config.ConsoleOut {
fmt.Print(string(pl))
}
return file.Write(pl)
}
//设置的path
func (logger *MyLogger) Path() string {
return logger.config.Path
}
func (logger *MyLogger) checkLogPath() error {
var err error
p := logger.config.LogPath()
absPath, err := filepath.Abs(filepath.Dir(p))
if err != nil {
return err
}
err = os.MkdirAll(absPath, 0700)
if err != nil {
return err
}
s, err := os.Stat(path.Dir(p))
if err != nil {
return fmt.Errorf("log dir error : %s", p)
}
if !s.IsDir() {
return fmt.Errorf("log dir not exists : %s", p)
}
return nil
}
//最终日志path
func (logger *MyLogger) LogPath() string {
p := logger.config.LogPath()
var parseT time.Time
parseT = time.Now()
t := parseT.Format(miscs.DateLayout)
return fmt.Sprintf("%s-%s.log", p[:len(p)-len(".log")], t)
}
func (logger *MyLogger) Trace(s string) error {
return logger.logItem(TraceLog, s)
}
func (logger *MyLogger) Debug(s string) error {
return logger.logItem(DebugLog, s)
}
func (logger *MyLogger) Info(s string) error {
return logger.logItem(InfoLog, s)
}
func (logger *MyLogger) Warn(s string) error {
return logger.logItem(WarnLog, s)
}
func (logger *MyLogger) Error(s string) error {
return logger.logItem(ErrorLog, s)
}
func (logger *MyLogger) Panic(s string) error {
return logger.logItem(PanicLog, s)
}
func (logger *MyLogger) Fatal(s string) error {
return logger.logItem(FatalLog, s)
}
func (logger *MyLogger) logItem(level int, s string) error {
sl := fmt.Sprintf("%s\n%d\n", s, level)
return logger.logger.Output(3, sl)
}
func (logger *MyLogger) logItemf(level int, format string, s ...interface{}) error {
sl := fmt.Sprintf("%s\n%d\n", fmt.Sprintf(format, s...), level)
return logger.logger.Output(3, sl)
}
func NewDefaultLogger(c *MyLoggerConfig) *MyLogger {
myLogger := &MyLogger{
config: c,
}
if c.Flag <= 0 {
c.Flag = log.Ldate | log.Lshortfile | log.Lmicroseconds
}
prefix := c.Module
if prefix != "" {
prefix = fmt.Sprintf("【%s】", prefix)
}
err := myLogger.checkLogPath()
if err != nil {
panic(err)
}
myLogger.logger = log.New(myLogger, prefix, c.Flag)
return myLogger
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/scottq/go-framework.git
git@gitee.com:scottq/go-framework.git
scottq
go-framework
go-framework
v1.1.3

搜索帮助