Skip to content

Instantly share code, notes, and snippets.

@widnyana
Last active November 15, 2019 12:15
Show Gist options
  • Save widnyana/3f4fa3f8d4263a62995660ffd8e6d1a5 to your computer and use it in GitHub Desktop.
Save widnyana/3f4fa3f8d4263a62995660ffd8e6d1a5 to your computer and use it in GitHub Desktop.
logrus log to file
package becak
import (
"fmt"
"github.com/sirupsen/logrus"
"os"
)
type x struct {
outfile *os.File
}
var (
outfile *os.File
logger *logrus.Logger
)
func xhandler(x int) {
fmt.Println("exit handler called")
err := outfile.Close()
if err != nil {
fmt.Printf("error at xhandler: %s\n", err)
} else {
fmt.Println("file closed")
}
fmt.Printf("wololo %d", x)
}
func Initlogging(filename string) *logrus.Logger {
var err error
outfile, err = os.OpenFile(filename, os.O_WRONLY | os.O_APPEND | os.O_CREATE, 0755)
logger = logrus.New()
logger.ExitFunc = xhandler
if err != nil {
panic(err)
}
logger.SetOutput(outfile)
return logger
}
package main
import "becak"
func main() {
l := becak.Initlogging("./all.log")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Exit(1)
}
package main
import (
"becak"
)
func main() {
l := becak.Initlogging("./all.log")
defer l.Exit(1)
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
l.Info("abcde")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment