Fix creating log file with logging disabled (#148)

This commit is contained in:
Kacper Betański 2019-01-21 02:51:46 +01:00 committed by Alex Goodman
parent 54979ae1eb
commit 2301a34403

View File

@ -2,8 +2,6 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/wagoodman/dive/filetree"
"github.com/wagoodman/dive/utils"
"io/ioutil" "io/ioutil"
"os" "os"
@ -12,6 +10,8 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/wagoodman/dive/filetree"
"github.com/wagoodman/dive/utils"
) )
var cfgFile string var cfgFile string
@ -122,12 +122,15 @@ func initConfig() {
// initLogging sets up the logging object with a formatter and location // initLogging sets up the logging object with a formatter and location
func initLogging() { func initLogging() {
var logFileObj *os.File
var err error
if viper.GetBool("log.enabled") == false { if viper.GetBool("log.enabled") {
logFileObj, err = os.OpenFile(viper.GetString("log.path"), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
} else {
log.SetOutput(ioutil.Discard) log.SetOutput(ioutil.Discard)
} }
logFileObj, err := os.OpenFile(viper.GetString("log.path"), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
} }
@ -140,13 +143,8 @@ func initLogging() {
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
} }
log.SetLevel(level) log.SetLevel(level)
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
log.SetOutput(logFileObj) log.SetOutput(logFileObj)
}
log.Debug("Starting Dive...") log.Debug("Starting Dive...")
} }