added config

This commit is contained in:
michael1011 2018-03-22 21:52:16 +01:00
parent 83a3743104
commit 8bb29d4b6e
No known key found for this signature in database
GPG Key ID: 84D249BA71685D46
6 changed files with 153 additions and 16 deletions

8
.gitignore vendored
View File

@ -18,4 +18,10 @@ lightningTip
.idea
# Log file
lightningTip.log
lightningTip.log
# Config file
lightningTip.conf
# Dep vendor directory
vendor

21
Gopkg.lock generated Normal file
View File

@ -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

38
Gopkg.toml Normal file
View File

@ -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

64
config.go Normal file
View File

@ -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)
}
}

View File

@ -2,4 +2,6 @@ package main
func main() {
initLog()
initConfig()
}

36
log.go
View File

@ -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")
}