panlei
2019-10-23 9fc8aaf3af0a179dbb2e614496d39c922661de4a
改造日志的输出方式
1个文件已添加
11个文件已修改
248 ■■■■■ 已修改文件
insertdata/EsClient.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
insertdata/insertDataToEs.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
labelFilter/readyDataForLabel.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
labelFilter/req.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
labelFilter/ruleForLabel.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
logger/logger.go 225 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
main.go 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruleserver/personTrack.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruleserver/readyDataForRule.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruleserver/ruleToformula.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruleserver/server.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
ruleserver/timeTicker.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
insertdata/EsClient.go
@@ -7,7 +7,7 @@
    "io"
    "io/ioutil"
    "net/http"
    "basic.com/valib/logger.git"
    "ruleprocess/logger"
    "strconv"
    "strings"
    "time"
insertdata/insertDataToEs.go
@@ -1,7 +1,7 @@
package insertdata
import (
    "basic.com/valib/logger.git"
    "ruleprocess/logger"
    "encoding/base64"
    "encoding/json"
    "errors"
labelFilter/readyDataForLabel.go
@@ -1,7 +1,7 @@
package labelFilter
import (
    "basic.com/valib/logger.git"
    "ruleprocess/logger"
    "ruleprocess/cache"
    "ruleprocess/structure"
    "time"
labelFilter/req.go
@@ -1,7 +1,7 @@
package labelFilter
import (
    "basic.com/valib/logger.git"
    "ruleprocess/logger"
    "fmt"
    "github.com/golang/protobuf/proto"
    "nanomsg.org/go-mangos"
labelFilter/ruleForLabel.go
@@ -3,7 +3,7 @@
import (
    "basic.com/dbapi.git"
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/logger.git"
    "ruleprocess/logger"
    "github.com/knetic/govaluate"
    "ruleprocess/structure"
    "strconv"
logger/logger.go
New file
@@ -0,0 +1,225 @@
package logger
import (
    "fmt"
    "log"
    "os"
    "os/exec"
    "strings"
    "time"
)
const (
    PanicLevel int = iota
    FatalLevel
    ErrorLevel
    WarnLevel
    InfoLevel
    DebugLevel
)
var loggerString string = ""
const (
    color_red = uint8(iota + 91)
    color_green        //    绿
    color_yellow        //    黄
    color_blue            //     蓝
    color_magenta         //    洋红
)
const (
    fatalPrefix        =    "[FATAL] "
    errorPrefix        =    "[ERROR] "
    warnPrefix        =    "[WARN] "
    infoPrefix        =    "[INFO] "
    debugPrefix        =    "[DEBUG] "
)
const (
    ByDay    int = iota
    ByWeek
    ByMonth
    BySize
)
type LogFile struct {
    level    int        // 日志等级
    saveMode int        // 保存模式
    saveDays int        // 日志保存天数
    logTime  int64        //
    fileName string        // 日志文件名
    filesize int64        // 文件大小, 需要设置 saveMode 为 BySize 生效
    fileFd   *os.File
}
var logFile LogFile
func init()  {
    logFile.saveMode = ByDay    // 默认按天保存
    logFile.saveDays = 7        // 默认保存三天的
    logFile.level = DebugLevel
    //logFile.filesize = 1024 * 1024 * 10    // 默认10M, 需要设置 saveMode 为 BySize
}
func Config(logFolder string, level int) {
    logFile.fileName = logFolder
    logFile.level = level
    log.SetOutput(logFile)
    //log.SetFlags(log.Lmicroseconds | log.Lshortfile)
    log.SetFlags(log.Ldate | log.Ltime)
}
func GetLogFile() (*LogFile) {
    return &logFile
}
func SetLevel(level int) {
    logFile.level = level
}
func SetSaveMode(saveMode int)  {
    logFile.saveMode = saveMode
}
func SetSaveDays(saveDays int)  {
    logFile.saveDays = saveDays
}
func SetSaveSize(saveSize int64)  {
    logFile.filesize = saveSize
}
func Debug(args ...interface{}) {
    if logFile.level >= DebugLevel {
        log.SetPrefix(blue(debugPrefix))
        //_ = log.Output(2, fmt.Sprintln(args...))
        loggerString += fmt.Sprintln(args...)
    }
}
func Info(args ...interface{}) {
    if logFile.level >= InfoLevel {
        log.SetPrefix(green(infoPrefix))
        //_ = log.Output(2, fmt.Sprintln(args...))
        loggerString += fmt.Sprintln(args...)
    }
}
func Warn(args ...interface{}) {
    if logFile.level >= WarnLevel {
        log.SetPrefix(magenta(warnPrefix))
        //_ = log.Output(2, fmt.Sprintln(args...))
        loggerString += fmt.Sprintln(args...)
    }
}
func Error(args ...interface{}) {
    if logFile.level >= ErrorLevel {
        log.SetPrefix(red(errorPrefix))
        //_ = log.Output(2, fmt.Sprintln(args...))
        loggerString += fmt.Sprintln(args...)
    }
}
func Fatal(args ...interface{}) {
    if logFile.level >= FatalLevel {
        log.SetPrefix(red(fatalPrefix))
        //_ = log.Output(2, fmt.Sprintln(args...))
        loggerString += fmt.Sprintln(args...)
    }
}
func OutPutByPanlei() {
    _ = log.Output(2, loggerString)
}
func GetRedPrefix(s string) string {
    return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_red, s)
}
func red(s string) string {
    return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_red, s)
}
func green(s string) string {
    return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_green, s)
}
func yellow(s string) string {
    return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_yellow, s)
}
func blue(s string) string {
    return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_blue, s)
}
func magenta(s string) string {
    return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_magenta, s)
}
func (me LogFile) Write(buf []byte) (n int, err error) {
    if me.fileName == "" {
        fmt.Printf("consol: %s", buf)
        return len(buf), nil
    }
    switch logFile.saveMode {
    case BySize:
        fileInfo, err := os.Stat(logFile.fileName)
        if  err != nil {
            logFile.createLogFile()
            logFile.logTime = time.Now().Unix()
        }else {
            filesize:= fileInfo.Size();
            if logFile.fileFd == nil ||
                filesize > logFile.filesize {
                logFile.createLogFile()
                logFile.logTime = time.Now().Unix()
            }
        }
    default:    // 默认按天  ByDay
        if logFile.logTime+3600 < time.Now().Unix() {
            logFile.createLogFile()
            logFile.logTime = time.Now().Unix()
        }
    }
    if logFile.fileFd == nil {
        fmt.Printf("log fileFd is nil !\n")
        return len(buf), nil
    }
    return logFile.fileFd.Write(buf)
}
func (me *LogFile) createLogFile() {
    logdir := "./"
    if index := strings.LastIndex(me.fileName, "/"); index != -1 {
        logdir = me.fileName[0:index] + "/"
        os.MkdirAll(me.fileName[0:index], os.ModePerm)
    }
    now := time.Now()
    filename := fmt.Sprintf("%s_%04d%02d%02d",
        me.fileName, now.Year(), now.Month(), now.Day())
    if err := os.Rename(me.fileName, filename); err == nil {
        go func() {
            tarCmd := exec.Command("tar", "-zcf", filename+".tar.gz", filename, "--remove-files")
            tarCmd.Run()
            rmCmd := exec.Command("/bin/sh", "-c",
                "find "+logdir+` -type f -mtime +` +string(logFile.saveDays)+ ` -exec rm {} \;`)
            rmCmd.Run()
        }()
    }
    for index := 0; index < 10; index++ {
        if fd, err := os.OpenFile(me.fileName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModeExclusive); nil == err {
            me.fileFd.Sync()
            me.fileFd.Close()
            me.fileFd = fd
            break
        }else {
            fmt.Println("Open logfile error! err: ", err.Error())
        }
        me.fileFd = nil
    }
}
main.go
@@ -7,6 +7,7 @@
    "net/http"
    _ "net/http/pprof"
    "ruleprocess/insertdata"
    "ruleprocess/logger"
    "ruleprocess/structure"
    "ruleprocess/util"
    "time"
@@ -16,7 +17,6 @@
    "flag"
    "fmt"
    "github.com/golang/protobuf/proto"
    "basic.com/valib/logger.git"
    //"gocv.io/x/gocv"
    //"image"
    //"image/color"
@@ -107,6 +107,7 @@
                    logger.Debug("插入完Es所用时间:", time.Since(start))
                    //事件推送
                    //go labelFilter.Judge(resultMsg)
                    logger.OutPutByPanlei()
                }(msg)
            }
        }
ruleserver/personTrack.go
@@ -2,7 +2,7 @@
import (
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/logger.git"
    "ruleprocess/logger"
    "github.com/golang/protobuf/proto"
    "ruleprocess/structure"
    "sync"
ruleserver/readyDataForRule.go
@@ -13,7 +13,7 @@
    "nanomsg.org/go-mangos/transport/tcp"
    "net"
    "ruleprocess/cache"
    "basic.com/valib/logger.git"
    "ruleprocess/logger"
    "ruleprocess/structure"
    "strconv"
    "time"
ruleserver/ruleToformula.go
@@ -3,7 +3,7 @@
import (
    "ruleprocess/cache"
    "ruleprocess/structure"
    "basic.com/valib/logger.git"
    "ruleprocess/logger"
    "sort"
    "strconv"
    "strings"
ruleserver/server.go
@@ -4,7 +4,7 @@
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/gopherdiscovery.git"
    "github.com/gogo/protobuf/proto"
    "basic.com/valib/logger.git"
    "ruleprocess/logger"
    "time"
)
ruleserver/timeTicker.go
@@ -2,7 +2,7 @@
import (
    "fmt"
    "basic.com/valib/logger.git"
    "ruleprocess/logger"
    "ruleprocess/structure"
    "strings"
    "time"