Switch on debug logs via env var flag (#260)

Co-authored-by: makeworld
This commit is contained in:
David Jimenez 2021-12-08 00:23:12 +00:00 committed by GitHub
parent 1c4d13b055
commit eb314f2a4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 13 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/makeworld-the-better-one/amfora/client"
"github.com/makeworld-the-better-one/amfora/config"
"github.com/makeworld-the-better-one/amfora/display"
"github.com/makeworld-the-better-one/amfora/logger"
"github.com/makeworld-the-better-one/amfora/subscriptions"
)
@ -20,10 +21,15 @@ var (
)
func main() {
// err := logger.Init()
// if err != nil {
// panic(err)
// }
log, err := logger.GetLogger()
if err != nil {
panic(err)
}
debugModeEnabled := os.Getenv("AMFORA_DEBUG") == "1"
if debugModeEnabled {
log.Println("Debug mode enabled")
}
if len(os.Args) > 1 {
if os.Args[1] == "--version" || os.Args[1] == "-v" {
@ -42,7 +48,7 @@ func main() {
}
}
err := config.Init()
err = config.Init()
if err != nil {
fmt.Fprintf(os.Stderr, "Config error: %v\n", err)
os.Exit(1)

View File

@ -3,18 +3,42 @@ package logger
// For debugging
import (
"io"
"io/ioutil"
"log"
"os"
)
var Log *log.Logger
var logger *log.Logger
func Init() error {
f, err := os.Create("debug.log")
func GetLogger() (*log.Logger, error) {
if logger != nil {
return logger, nil
}
var writer io.Writer
var err error
debugModeEnabled := os.Getenv("AMFORA_DEBUG") == "1"
if debugModeEnabled {
writer, err = os.Create("debug.log")
if err != nil {
return err
return nil, err
}
Log = log.New(f, "", log.LstdFlags)
Log.Println("Started Log")
return nil
} else {
// Suppress all logging output if debug mode is disabled
writer = ioutil.Discard
}
logger = log.New(writer, "", log.LstdFlags)
if !debugModeEnabled {
// Clear all flags to skip log output formatting step to increase
// performance somewhat if we're not logging anything
logger.SetFlags(0)
}
logger.Println("Started logger")
return logger, nil
}