main_linux.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "github.com/gin-gonic/gin"
  4. rotatelogs "github.com/lestrrat-go/file-rotatelogs"
  5. "io"
  6. "miads/adslib"
  7. "miads/adslib/ads_redis"
  8. "os"
  9. "time"
  10. "github.com/fvbock/endless"
  11. log "github.com/sirupsen/logrus"
  12. )
  13. func setupLogger(logName string, logLevel log.Level) {
  14. writer, err := rotatelogs.New(
  15. logName+".%Y%m%d",
  16. // WithLinkName为最新的日志建立软连接,以方便随着找到当前日志文件
  17. rotatelogs.WithLinkName(logName),
  18. // WithRotationTime设置日志分割的时间,这里设置为一小时分割一次
  19. rotatelogs.WithRotationTime(time.Hour),
  20. // WithMaxAge和WithRotationCount二者只能设置一个,
  21. // WithMaxAge设置文件清理前的最长保存时间,
  22. // WithRotationCount设置文件清理前最多保存的个数。
  23. rotatelogs.WithMaxAge(time.Hour*24*3),
  24. )
  25. if err != nil {
  26. log.Errorf("config local file system for logger error: %v", err)
  27. }
  28. log.SetReportCaller(true)
  29. log.SetLevel(logLevel)
  30. log.SetOutput(io.MultiWriter(writer, os.Stdout))
  31. }
  32. func setupRouter() *gin.Engine {
  33. r := gin.Default()
  34. r.POST("/ads", adsHandler)
  35. return r
  36. }
  37. func main() {
  38. ads_redis.Setup()
  39. adslib.GetConf()
  40. setupLogger(adslib.GetConf().LogPath+"/ads.log", log.TraceLevel)
  41. r := setupRouter()
  42. endless.ListenAndServe(":8080", r)
  43. }