Last active
November 15, 2019 12:15
-
-
Save widnyana/3f4fa3f8d4263a62995660ffd8e6d1a5 to your computer and use it in GitHub Desktop.
logrus log to file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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