美文网首页
分时间段生成日志文件

分时间段生成日志文件

作者: Feng_Sir | 来源:发表于2018-01-03 16:14 被阅读0次

application.yaml

log:
  level: DEBUG
  path: D:/etlive.log
rabbit:
  host: 127.0.0.1
  port: 5672
  username: guest
  password: guest
  vhost: dashboard
  exchange: dashboard
redis:
  host: 127.0.0.1
  port: 6379
  password: dell@123
  db: 0
package commons

import (
    "io/ioutil"
    "gopkg.in/yaml.v2"
    "log"
    "fmt"
)

type Log struct {
    Level string `yaml:"level"`
    Path  string `yaml:"path"`
}

type Rabbit struct {
    Host     string
    Port     int
    Username string
    Password string
    Vhost    string
    Exchange string
}

type Redis struct {
    Host     string
    Port     int
    Password string
    Db       int
}

type applicationConfig struct {
    Log
    Rabbit
    Redis
}

var RabbitConfig *Rabbit
var RedisConfig *Redis
var LogConfig *Log

func init() {
    data, err := ioutil.ReadFile("conf/application.yaml")
    if err != nil {
        log.Fatalf("配置文件读取错误 %s", err)
        panic(fmt.Sprintf("配置文件读取错误 %s", err))
    }
    confEntity := applicationConfig{}
    err = yaml.Unmarshal([]byte(data), &confEntity)
    if err != nil {
        log.Fatalf("配置文件解析错误 %s", err)
        panic(fmt.Sprintf("配置文件解析错误 %s", err))
    }
    RabbitConfig = &confEntity.Rabbit
    RedisConfig = &confEntity.Redis
    LogConfig = &confEntity.Log
}


package main

import (
    "github.com/lestrrat/go-file-rotatelogs"
    "github.com/rifflock/lfshook"
    "github.com/sirupsen/logrus"
    "./execs"
    "./commons"
    "./connectors"
    "./controllers"
    "github.com/astaxie/beego"
    "./services"
    "strings"
    "./plugins"
    "time"
    "fmt"
)

func init() {
    baseLogPaht := commons.LogConfig.Path
    writer, err := rotatelogs.New(
        baseLogPaht + ".%Y%m%d%H%M",
        //rotatelogs.WithLinkName(baseLogPaht), // 生成软链,指向最新日志文件
        rotatelogs.WithMaxAge(365*24*time.Hour),   // 文件最大保存时间
        rotatelogs.WithRotationTime(24*time.Hour), // 日志切割时间间隔
    )
    if err != nil {
        panic(fmt.Sprintf("%s: %s", "日志文件打开错误", err))
    }

    lfHook := lfshook.NewHook(lfshook.WriterMap{
        logrus.DebugLevel: writer, // 为不同级别设置不同的输出目的
        logrus.InfoLevel:  writer,
        logrus.WarnLevel:  writer,
        logrus.ErrorLevel: writer,
        //log.FatalLevel: os.Stdout,
        //log.PanicLevel: os.Stdout,
    })
    lfHook.SetFormatter(&logrus.JSONFormatter{})
    switch strings.ToUpper(commons.LogConfig.Level) {
    case "DEBUG":
        logrus.SetLevel(logrus.DebugLevel)
    case "INFO":
        logrus.SetLevel(logrus.InfoLevel)
    case "WARN":
        logrus.SetLevel(logrus.WarnLevel)
    case "ERROR":
        logrus.SetLevel(logrus.ErrorLevel)
    default:
        logrus.SetLevel(logrus.DebugLevel)
    }
    logrus.AddHook(lfHook)

    beego.Router("/", &controllers.PageController{})
    v1 := beego.NewNamespace("v1",
        beego.NSRouter("/", &controllers.InitController{}, "get:Find"), // 核心地图数据
    )

    beego.AddNamespace(v1)
}

func main() {
    logrus.Infoln("项目启动")
    if strings.ToUpper(commons.MqSend) == "MQTT" {
        go plugins.MqttStart()
    }
    execs.Starts()
    mt := services.MainTime{}
    mt.Start()
    beego.SetStaticPath("/public", "public")
    go connectors.RabbitConsume()
    beego.Run(":" + commons.Port)
}

相关文章

网友评论

      本文标题:分时间段生成日志文件

      本文链接:https://www.haomeiwen.com/subject/tpohnxtx.html