func (hook *LogrusHook) openCreate(dest string) (err error) {
	fd, err := os.OpenFile(dest, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
	if err != nil {
		log.WithError(err).Error("Could not create log file")
		hook.w = os.Stderr
		hook.fd = nil
		return
	}
	hook.w = bufio.NewWriter(fd)
	hook.fd = fd
	return
}
openCreate creates a new dest file for appending. Default to os.Stderr if it can't open dest
package log
import (
	"bufio"
	log "github.com/sirupsen/logrus"
	"io"
	"io/ioutil"
	"os"
	"strings"
	"sync"
)
var hookMu sync.Mutex
type LoggerHook interface {
	log.Hook
	Reopen() error
}
type LogrusHook struct {
	w io.Writer
	fd *os.File
	fname string
	plainTxtFormatter *log.TextFormatter
	mu sync.Mutex
}
func NewLogrusHook(dest string) (LoggerHook, error) {
	hookMu.Lock()
	defer hookMu.Unlock()
	hook := LogrusHook{fname: dest}
	err := hook.setup(dest)
	return &hook, err
}
type OutputOption int
const (
	OutputStderr OutputOption = 1 + iota
	OutputStdout
	OutputOff
	OutputNull
	OutputFile
)
var outputOptions = [...]string{
	"stderr",
	"stdout",
	"off",
	"",
	"file",
}
func (o OutputOption) String() string {
	return outputOptions[o-1]
}
func parseOutputOption(str string) OutputOption {
	switch str {
	case "stderr":
		return OutputStderr
	case "stdout":
		return OutputStdout
	case "off":
		return OutputOff
	case "":
		return OutputNull
	}
	return OutputFile
}
func (hook *LogrusHook) setup(dest string) error {
	out := parseOutputOption(dest)
	if out == OutputNull || out == OutputStderr {
		hook.w = os.Stderr
	} else if out == OutputStdout {
		hook.w = os.Stdout
	} else if out == OutputOff {
		hook.w = ioutil.Discard
	} else {
		if _, err := os.Stat(dest); err == nil {
			if err := hook.openAppend(dest); err != nil {
				return err
			}
		} else {
			if err := hook.openCreate(dest); err != nil {
				return err
			}
		}
	}
	if hook.fd != nil {
		hook.plainTxtFormatter = &log.TextFormatter{DisableColors: true}
	}
	return nil
}
func (hook *LogrusHook) openAppend(dest string) (err error) {
	fd, err := os.OpenFile(dest, os.O_APPEND|os.O_WRONLY, 0644)
	if err != nil {
		log.WithError(err).Error("Could not open log file for appending")
		hook.w = os.Stderr
		hook.fd = nil
		return
	}
	hook.w = bufio.NewWriter(fd)
	hook.fd = fd
	return
}