Skip to content

Commit

Permalink
Merge pull request #10 from kuno1/issue/9
Browse files Browse the repository at this point in the history
Support log-path
Close #9.
  • Loading branch information
Harai Akihiro authored Jan 24, 2019
2 parents 608adcb + c494023 commit a4aa454
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
22 changes: 18 additions & 4 deletions immortalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,32 @@ func run(minLifetime uint, maxLifetime uint, command string) int {
return status
}

func configLog(level string) error {
func configLog(level string, logPath string) error {
var l logrus.Level
switch level {
case "trace":
l = logrus.TraceLevel
case "debug":
l = logrus.DebugLevel
case "info":
l = logrus.DebugLevel
l = logrus.InfoLevel
default:
return errors.New("log level must be one of 'info', 'debug', or 'trace'")
}

log.SetFormatter(&logrus.JSONFormatter{})
log.SetLevel(l)
log.Out = os.Stderr

if logPath == "" {
log.Out = os.Stderr
return nil
}

f, err := os.Create(logPath)
if err != nil {
return err
}
log.Out = f
return nil
}

Expand All @@ -100,9 +110,13 @@ func main() {
commandPtr := flag.String("command", "command", "command to immortalize")
levelPtr := flag.String(
"log-level", "info", "Log level: 'info', 'debug', or 'trace'")
logPathPtr := flag.String(
"log-path", "", "Log path: default to stderr")
flag.Parse()

configLog(*levelPtr)
if err := configLog(*levelPtr, *logPathPtr); err != nil {
panic(err)
}

log.Debugf("PID: %v", os.Getpid())

Expand Down
36 changes: 36 additions & 0 deletions test/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,39 @@ assert_exit_with_0 () {
assert_exit_with_nonzero
[ "$(result)" == 0 ]
}

@test "Log path" {
log=work/immortalize.log
rm -f "$log"
./work/immortalize \
-max-lifetime 2 \
-command test-bin/command-zero \
-log-path "$log" >&3 2>&1

[ 1 -lt "$(wc -l < "$log")" ]
}

@test "log level - default (info)" {
log=work/immortalize.log
rm -f "$log"
./work/immortalize \
-max-lifetime 2 \
-command test-bin/command-zero \
-log-path "$log" >&3 2>&1

[ 1 -lt "$(grep info < "$log" | wc -l)" ]
[ 0 -eq "$(grep debug < "$log" | wc -l)" ]
}

@test "log level - debug" {
log=work/immortalize.log
rm -f "$log"
./work/immortalize \
-max-lifetime 2 \
-command test-bin/command-zero \
-log-path "$log" \
-log-level debug >&3 2>&1

[ 1 -lt "$(grep info < "$log" | wc -l)" ]
[ 0 -lt "$(grep debug < "$log" | wc -l)" ]
}

0 comments on commit a4aa454

Please sign in to comment.