123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package main
- import (
- "github.com/gin-gonic/gin"
- rotatelogs "github.com/lestrrat-go/file-rotatelogs"
- "io"
- "miads/adslib"
- "miads/adslib/ads_redis"
- "os"
- "time"
- "github.com/fvbock/endless"
- "github.com/gin-contrib/pprof"
- log "github.com/sirupsen/logrus"
- )
- func setupLogger(logName string, logLevel log.Level) {
- writer, err := rotatelogs.New(
- logName+".%Y%m%d",
- // WithLinkName为最新的日志建立软连接,以方便随着找到当前日志文件
- rotatelogs.WithLinkName(logName),
- // WithRotationTime设置日志分割的时间,这里设置为一小时分割一次
- rotatelogs.WithRotationTime(time.Hour),
- // WithMaxAge和WithRotationCount二者只能设置一个,
- // WithMaxAge设置文件清理前的最长保存时间,
- // WithRotationCount设置文件清理前最多保存的个数。
- rotatelogs.WithMaxAge(time.Hour*24*3),
- )
- if err != nil {
- log.Errorf("config local file system for logger error: %v", err)
- }
- log.SetReportCaller(true)
- log.SetLevel(logLevel)
- log.SetOutput(io.MultiWriter(writer, os.Stdout))
- }
- func setupRouter() *gin.Engine {
- r := gin.Default()
- pprof.Register(r) // 性能
- r.POST("/ads", adsHandler)
- return r
- }
- func main() {
- ads_redis.Setup()
- conf := adslib.GetConf()
- logLevel, err := log.ParseLevel(conf.LogLevel)
- if err != nil {
- log.Errorf("parse log level failed, use info level, log str: %s, err: %s", conf.LogLevel, err)
- logLevel = log.InfoLevel
- }
- setupLogger(conf.LogPath+"/ads.log", logLevel)
- r := setupRouter()
- endless.ListenAndServe(":1111", r)
- }
|