func init() { // 初始化日志 logrus.SetFormatter(&logrus.JSONFormatter{}) log.SetOutput(logrus.StandardLogger().Out) // 初始化配置 Config = loadConfig() lv, err := logrus.ParseLevel(Config.LogLevel) if err != nil { logrus.WithError(err).Warn("解析日志等级出错") lv = logrus.DebugLevel } logrus.SetLevel(lv) if Config.ReportOn { hook, err := logrus_mail.NewMailAuthHook(Config.AppName, Config.MailerHost, Config.MailerPort, Config.MailerUser, Config.MailReceiver, Config.MailerUser, Config.MailerPass) if err == nil { logrus.AddHook(hook) } } exeRoot := filepath.Dir(os.Args[0]) f, err := os.OpenFile(filepath.Join(exeRoot, "retask.log"), os.O_APPEND|os.O_CREATE, os.ModePerm) if err != nil { logrus.WithError(err).Fatal("打开日志文件失败") } logrus.SetOutput(f) }
func NewMailHook(options logrus_mate.Options) (hook logrus.Hook, err error) { conf := MailHookConfig{} if err = options.ToObject(&conf); err != nil { return } hook, err = logrus_mail.NewMailAuthHook( conf.AppName, conf.Host, conf.Port, conf.From, conf.To, conf.Username, conf.Password) return }