Skip to content

Commit

Permalink
Add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed Apr 29, 2020
1 parent b723a0a commit 94225af
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ module github.com/nakabonne/golintui
go 1.14

require (
github.com/shibukawa/configdir v0.0.0-20170330084843-e180dbdc8da0
github.com/sirupsen/logrus v1.5.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shibukawa/configdir v0.0.0-20170330084843-e180dbdc8da0 h1:Xuk8ma/ibJ1fOy4Ee11vHhUFHQNpHhrBneOCNHVXS5w=
github.com/shibukawa/configdir v0.0.0-20170330084843-e180dbdc8da0/go.mod h1:7AwjWCpdPhkSmNAgUv5C7EJ4AbmjEB3r047r3DXWu3Y=
github.com/sirupsen/logrus v1.5.0 h1:1N5EYkVAPEywqZRJd7cwnRtCb6xJx7NH3T3WUTF980Q=
github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand Down
10 changes: 5 additions & 5 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ type App struct {
Config *config.Config
Log *logrus.Entry
Gui *gui.Gui
//Tr *i18n.Localizer
//Updater *updates.Updater // may only need this on the Gui
//ClientContext string
// Tr *i18n.Localizer
// Updater *updates.Updater // may only need this on the Gui
// ClientContext string
}

func New(conf *config.Config) (*App, error) {
return &App{
closers: nil,
closers: []io.Closer{},
Config: conf,
Log: nil,
Log: newLogger(conf),
Gui: nil,
}, nil
}
Expand Down
62 changes: 62 additions & 0 deletions pkg/app/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package app

import (
"io/ioutil"
"os"
"path/filepath"

"github.com/shibukawa/configdir"
"github.com/sirupsen/logrus"

"github.com/nakabonne/golintui/pkg/config"
)

func newLogger(conf *config.Config) *logrus.Entry {
var log *logrus.Logger
if conf.GetDebug() || os.Getenv("DEBUG") == "TRUE" {
log = newDevelopmentLogger()
} else {
log = newProductionLogger()
}

log.Formatter = &logrus.JSONFormatter{}

return log.WithFields(logrus.Fields{
"debug": conf.GetDebug(),
"version": conf.GetVersion(),
"commit": conf.GetCommit(),
"buildDate": conf.GetBuildDate(),
})
}

func newDevelopmentLogger() *logrus.Logger {
log := logrus.New()
log.SetLevel(getLogLevel())
file, err := os.OpenFile(filepath.Join(globalConfigDir(), "development.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
panic("unable to log to file") // TODO: don't panic (also, remove this call to the `panic` function)
}
log.SetOutput(file)
return log
}
func newProductionLogger() *logrus.Logger {
log := logrus.New()
log.Out = ioutil.Discard
log.SetLevel(logrus.ErrorLevel)
return log
}

func getLogLevel() logrus.Level {
strLevel := os.Getenv("LOG_LEVEL")
level, err := logrus.ParseLevel(strLevel)
if err != nil {
return logrus.DebugLevel
}
return level
}

func globalConfigDir() string {
configDirs := configdir.New("nakabonne", "golintui")
configDir := configDirs.QueryFolders(configdir.Global)[0]
return configDir.Path
}
16 changes: 16 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ func New(name, version, commit, date, buildSource string, debuggingFlag bool) (*
BuildSource: "",
}, nil
}

func (c *Config) GetDebug() bool {
return c.Debug
}

func (c *Config) GetVersion() string {
return c.Version
}

func (c *Config) GetCommit() string {
return c.Commit
}

func (c *Config) GetBuildDate() string {
return c.BuildDate
}

0 comments on commit 94225af

Please sign in to comment.