This repository has been archived by the owner on Aug 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Discussion: Review logging / console output for --log-level support
Fixes #64
- Loading branch information
1 parent
712084f
commit f892b5e
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package logger | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/aztfmod/rover/pkg/command" | ||
"github.com/aztfmod/rover/pkg/utils" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var LogEnvironment = "production" | ||
var logLevel = log.InfoLevel | ||
|
||
func init() { | ||
log.SetFormatter(&log.TextFormatter{ | ||
ForceColors: true, | ||
DisableColors: false, | ||
FullTimestamp: false, | ||
DisableTimestamp: true, | ||
}) | ||
|
||
log.SetLevel(logLevel) | ||
|
||
if LogEnvironment == "production" { | ||
log.SetFormatter(&log.JSONFormatter{}) | ||
roverHomeDir, _ := utils.GetRoverDirectory() | ||
roverHomeLogsDir := filepath.Join(roverHomeDir, "logs") | ||
command.EnsureDirectory(roverHomeLogsDir) | ||
roverLogFile := filepath.Join(roverHomeLogsDir, "rover.log") | ||
file, err := os.OpenFile(roverLogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) | ||
if err == nil { | ||
log.SetOutput(file) | ||
} else { | ||
log.Info("Failed to log to file, using default stderr") | ||
} | ||
} else { | ||
log.SetReportCaller(true) // has performance impact but useful in debugging | ||
} | ||
|
||
} | ||
|
||
func SetLogLevel(level string, err error) { | ||
lowerLevel := strings.ToLower(level) | ||
switch lowerLevel { | ||
case "trace": | ||
logLevel = log.TraceLevel | ||
case "debug": | ||
logLevel = log.DebugLevel | ||
case "info": | ||
logLevel = log.InfoLevel | ||
case "warn": | ||
logLevel = log.WarnLevel | ||
case "error": | ||
logLevel = log.ErrorLevel | ||
case "fatal": | ||
logLevel = log.FatalLevel | ||
case "panic": | ||
logLevel = log.PanicLevel | ||
default: | ||
logLevel = log.InfoLevel | ||
} | ||
} |