1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +02:00

Add debug logger (#604)

Signed-off-by: David Jimenez <dvejmz@sgfault.com>
This commit is contained in:
David Jimenez 2021-10-20 18:32:31 +01:00 committed by GitHub
parent 0ee7866c45
commit 8a3653ca49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 0 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ revive
vendor
*.swp
dist/
*.log

44
logging/logger.go Normal file
View File

@ -0,0 +1,44 @@
package logging
import (
"io"
"io/ioutil"
"log"
"os"
)
var logger *log.Logger
// GetLogger retrieves an instance of an application logger which outputs
// to a file if the debug flag is enabled
func GetLogger() (*log.Logger, error) {
if logger != nil {
return logger, nil
}
var writer io.Writer
var err error
debugModeEnabled := os.Getenv("DEBUG") == "1"
if debugModeEnabled {
writer, err = os.Create("revive.log")
if err != nil {
return nil, err
}
} 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("Logger initialised")
return logger, nil
}

View File

@ -13,6 +13,7 @@ import (
"github.com/mgechev/dots"
"github.com/mgechev/revive/config"
"github.com/mgechev/revive/lint"
"github.com/mgechev/revive/logging"
"github.com/mitchellh/go-homedir"
)
@ -29,6 +30,11 @@ func fail(err string) {
}
func main() {
log, err := logging.GetLogger()
if err != nil {
fail(err.Error())
}
conf, err := config.GetConfig(configPath)
if err != nil {
fail(err.Error())
@ -60,6 +66,8 @@ func main() {
fail(err.Error())
}
log.Println("Config loaded")
failures, err := revive.Lint(packages, lintingRules, *conf)
if err != nil {
fail(err.Error())