Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug logger #604

Merged
merged 2 commits into from Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ revive
vendor
*.swp
dist/
*.log
44 changes: 44 additions & 0 deletions logging/logger.go
@@ -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
}
8 changes: 8 additions & 0 deletions main.go
Expand Up @@ -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"
)

Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand Down