...
1 package commoncli
2
3 import (
4 "flag"
5 "fmt"
6 "os"
7 "path/filepath"
8
9 "github.com/sirupsen/logrus"
10 )
11
12
13 func SetFlagsForLogging() []*string {
14 basename := filepath.Base(os.Args[0]) + ".log"
15 logFile := flag.String("logfile", filepath.Join("/tmp", basename), "logging file location")
16 logLevel := flag.String("loglevel", "debug", "Logging Level: debug, info, warning, error, fatal, panic.")
17 return []*string{logFile, logLevel}
18 }
19
20
21 func SetupLogging(args ...*string) error {
22 if len(args) < 1 {
23 return fmt.Errorf("invalid log params")
24 }
25 level, err := logrus.ParseLevel(*args[1])
26 if err != nil {
27 logrus.Fatal(err)
28 return err
29 }
30 logrus.SetLevel(level)
31
32 filename := *args[0]
33 outputTarget, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
34 if err != nil {
35 return err
36 }
37
38 logrus.SetOutput(outputTarget)
39 return nil
40 }
41
View as plain text