main_linux.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. "github.com/gin-contrib/pprof"
  12. log "github.com/sirupsen/logrus"
  13. )
  14. func setupLogger(logName string, logLevel log.Level) {
  15. writer, err := rotatelogs.New(
  16. logName+".%Y%m%d",
  17. // WithLinkName为最新的日志建立软连接,以方便随着找到当前日志文件
  18. rotatelogs.WithLinkName(logName),
  19. // WithRotationTime设置日志分割的时间,这里设置为一小时分割一次
  20. rotatelogs.WithRotationTime(time.Hour),
  21. // WithMaxAge和WithRotationCount二者只能设置一个,
  22. // WithMaxAge设置文件清理前的最长保存时间,
  23. // WithRotationCount设置文件清理前最多保存的个数。
  24. rotatelogs.WithMaxAge(time.Hour*24*3),
  25. )
  26. if err != nil {
  27. log.Errorf("config local file system for logger error: %v", err)
  28. }
  29. log.SetReportCaller(true)
  30. log.SetLevel(logLevel)
  31. log.SetOutput(io.MultiWriter(writer, os.Stdout))
  32. }
  33. func setupRouter() *gin.Engine {
  34. r := gin.Default()
  35. pprof.Register(r) // 性能
  36. r.POST("/ads", adsHandler)
  37. return r
  38. }
  39. func main() {
  40. ads_redis.Setup()
  41. conf := adslib.GetConf()
  42. logLevel, err := log.ParseLevel(conf.LogLevel)
  43. if err != nil {
  44. log.Errorf("parse log level failed, use info level, log str: %s, err: %s", conf.LogLevel, err)
  45. logLevel = log.InfoLevel
  46. }
  47. setupLogger(conf.LogPath+"/ads.log", logLevel)
  48. r := setupRouter()
  49. endless.ListenAndServe(":1111", r)
  50. }