added config
This commit is contained in:
parent
83a3743104
commit
8bb29d4b6e
|
@ -19,3 +19,9 @@ lightningTip
|
||||||
|
|
||||||
# Log file
|
# Log file
|
||||||
lightningTip.log
|
lightningTip.log
|
||||||
|
|
||||||
|
# Config file
|
||||||
|
lightningTip.conf
|
||||||
|
|
||||||
|
# Dep vendor directory
|
||||||
|
vendor
|
|
@ -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
|
|
@ -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
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,4 +2,6 @@ package main
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
initLog()
|
initLog()
|
||||||
|
|
||||||
|
initConfig()
|
||||||
}
|
}
|
||||||
|
|
32
log.go
32
log.go
|
@ -1,34 +1,40 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/op/go-logging"
|
"github.com/op/go-logging"
|
||||||
"os"
|
"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 logFormat = logging.MustStringFormatter("%{time:2006-01-02 15:04:05.000} [%{level}] %{message}")
|
||||||
|
|
||||||
|
var backendConsole = logging.NewLogBackend(os.Stdout, "", 0)
|
||||||
|
|
||||||
func initLog() {
|
func initLog() {
|
||||||
logging.SetFormatter(logFormat)
|
logging.SetFormatter(logFormat)
|
||||||
logging.SetLevel(logging.DEBUG, "")
|
|
||||||
|
|
||||||
backendConsole := logging.NewLogBackend(os.Stdout, "", 0)
|
|
||||||
|
|
||||||
logging.SetBackend(backendConsole)
|
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 {
|
if err != nil {
|
||||||
defer file.Close()
|
log.Error("Failed to initialize log file: " + fmt.Sprint(err))
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
backendFile := logging.NewLogBackend(file, "", 0)
|
backendFile := logging.NewLogBackend(file, "", 0)
|
||||||
|
|
||||||
logging.SetBackend(backendConsole, backendFile)
|
backendFileLeveled := logging.AddModuleLevel(backendFile)
|
||||||
|
backendFileLeveled.SetLevel(level, "")
|
||||||
|
|
||||||
|
backendConsoleLeveled := logging.AddModuleLevel(backendConsole)
|
||||||
|
backendConsoleLeveled.SetLevel(level, "")
|
||||||
|
|
||||||
|
logging.SetBackend(backendConsoleLeveled, backendFileLeveled)
|
||||||
|
|
||||||
log.Debug("Successfully initialized log file")
|
log.Debug("Successfully initialized log file")
|
||||||
|
|
||||||
} else {
|
|
||||||
log.Critical("Failed to initialize log file: ", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue