From 8bb29d4b6e3a969a91f83f86237fe2547ca6c47b Mon Sep 17 00:00:00 2001 From: michael1011 Date: Thu, 22 Mar 2018 21:52:16 +0100 Subject: [PATCH] added config --- .gitignore | 8 ++++++- Gopkg.lock | 21 ++++++++++++++++ Gopkg.toml | 38 +++++++++++++++++++++++++++++ config.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ lightningtip.go | 2 ++ log.go | 36 ++++++++++++++++------------ 6 files changed, 153 insertions(+), 16 deletions(-) create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml create mode 100644 config.go diff --git a/.gitignore b/.gitignore index b00fbaf..06ec7ce 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,10 @@ lightningTip .idea # Log file -lightningTip.log \ No newline at end of file +lightningTip.log + +# Config file +lightningTip.conf + +# Dep vendor directory +vendor \ No newline at end of file diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..bfecbeb --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,21 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + name = "github.com/jessevdk/go-flags" + packages = ["."] + revision = "96dc06278ce32a0e9d957d590bb987c81ee66407" + version = "v1.3.0" + +[[projects]] + name = "github.com/op/go-logging" + packages = ["."] + revision = "b2cb9fa56473e98db8caba80237377e83fe44db5" + version = "v1" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "25f60b14acb88f3ea9cb02b446b7ccc98239db04d24c0052202d694ff35ff33a" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..671cdfb --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,38 @@ +# Gopkg.toml example +# +# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" +# +# [prune] +# non-go = false +# go-tests = true +# unused-packages = true + + +[[constraint]] + name = "github.com/jessevdk/go-flags" + version = "1.3.0" + +[[constraint]] + name = "github.com/op/go-logging" + version = "1.0.0" + +[prune] + go-tests = true + unused-packages = true diff --git a/config.go b/config.go new file mode 100644 index 0000000..37617c4 --- /dev/null +++ b/config.go @@ -0,0 +1,64 @@ +package main + +import ( + "github.com/jessevdk/go-flags" + "github.com/op/go-logging" + "strings" +) + +const ( + defaultConfigFile = "lightningTip.conf" + + defaultLogFile = "lightningTip.log" + defaultLogLevel = "debug" +) + +type config struct { + ConfigFile string `long:"config" Description:"Config file location"` + + LogFile string `long:"logfile" Description:"Log file location"` + LogLevel string `long:"loglevel" Description:"Log level: debug, info, warning, error"` +} + +var cfg config + +func initConfig() { + cfg = config{ + ConfigFile: defaultConfigFile, + + LogFile: defaultLogFile, + LogLevel: defaultLogLevel, + } + + _, err := flags.Parse(&cfg) + + errFile := flags.IniParse(cfg.ConfigFile, &cfg) + + // Parse flags again to override config file + _, err = flags.Parse(&cfg) + + // Default log level + logLevel := logging.DEBUG + + switch strings.ToLower(cfg.LogLevel) { + case "info": + logLevel = logging.INFO + + case "warning": + logLevel = logging.WARNING + + case "error": + logLevel = logging.ERROR + } + + initLogFile(cfg.LogFile, logLevel) + + if err != nil { + log.Error("Failed to parse command line flags") + } + + if errFile != nil { + log.Infof("Could not parse config file: %v", errFile) + } + +} diff --git a/lightningtip.go b/lightningtip.go index d9afd38..d55030a 100644 --- a/lightningtip.go +++ b/lightningtip.go @@ -2,4 +2,6 @@ package main func main() { initLog() + + initConfig() } diff --git a/log.go b/log.go index d258138..e383cfa 100644 --- a/log.go +++ b/log.go @@ -1,34 +1,40 @@ package main import ( + "fmt" "github.com/op/go-logging" "os" ) -var log = logging.MustGetLogger("lightningTip") +var log = logging.MustGetLogger("") var logFormat = logging.MustStringFormatter("%{time:2006-01-02 15:04:05.000} [%{level}] %{message}") +var backendConsole = logging.NewLogBackend(os.Stdout, "", 0) + func initLog() { logging.SetFormatter(logFormat) - logging.SetLevel(logging.DEBUG, "") - - backendConsole := logging.NewLogBackend(os.Stdout, "", 0) logging.SetBackend(backendConsole) +} - file, err := os.OpenFile("lightningTip.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) +func initLogFile(logFile string, level logging.Level) { + file, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) - if err == nil { - defer file.Close() + if err != nil { + log.Error("Failed to initialize log file: " + fmt.Sprint(err)) - backendFile := logging.NewLogBackend(file, "", 0) - - logging.SetBackend(backendConsole, backendFile) - - log.Debug("Successfully initialized log file") - - } else { - log.Critical("Failed to initialize log file: ", err) + return } + backendFile := logging.NewLogBackend(file, "", 0) + + backendFileLeveled := logging.AddModuleLevel(backendFile) + backendFileLeveled.SetLevel(level, "") + + backendConsoleLeveled := logging.AddModuleLevel(backendConsole) + backendConsoleLeveled.SetLevel(level, "") + + logging.SetBackend(backendConsoleLeveled, backendFileLeveled) + + log.Debug("Successfully initialized log file") }