moving logging function to common setting up client logging and db
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
|
loglevel = 2
|
||||||
rootpath = ""
|
rootpath = ""
|
||||||
reponame = "gvc"
|
reponame = "gvc"
|
||||||
currentbranch = "master"
|
currentbranch = "master"
|
||||||
|
@@ -10,7 +10,10 @@ import (
|
|||||||
clientcmd "github.com/deranjer/gvc/client/clientcmd"
|
clientcmd "github.com/deranjer/gvc/client/clientcmd"
|
||||||
clientconfig "github.com/deranjer/gvc/client/clientconfig"
|
clientconfig "github.com/deranjer/gvc/client/clientconfig"
|
||||||
config "github.com/deranjer/gvc/client/clientconfig"
|
config "github.com/deranjer/gvc/client/clientconfig"
|
||||||
|
"github.com/deranjer/gvc/common"
|
||||||
|
"github.com/deranjer/gvc/common/database"
|
||||||
"github.com/deranjer/store"
|
"github.com/deranjer/store"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
var version = "0.1.5"
|
var version = "0.1.5"
|
||||||
@@ -31,7 +34,7 @@ func main() {
|
|||||||
// Setting up a blank config to read the .toml file in if one exists
|
// Setting up a blank config to read the .toml file in if one exists
|
||||||
var conf clientconfig.Gvcconfig
|
var conf clientconfig.Gvcconfig
|
||||||
isRepo := validateRepo()
|
isRepo := validateRepo()
|
||||||
if isRepo {
|
if isRepo { // If repo folder exists, treat it like a repo and setup logging and database. If not a repo will not need any of this
|
||||||
err := store.Load(configPath, &conf)
|
err := store.Load(configPath, &conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error loading config file into struct, please fix config, panic! \n%s", err)
|
fmt.Printf("Error loading config file into struct, please fix config, panic! \n%s", err)
|
||||||
@@ -42,7 +45,24 @@ func main() {
|
|||||||
fmt.Println("Error validating config, your config file is corrupt! ", err)
|
fmt.Println("Error validating config, your config file is corrupt! ", err)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
fmt.Println("Attempting to start logger...")
|
||||||
|
// Setting up the logger to file output
|
||||||
|
logFile, err := os.OpenFile(".gvc/gvclog.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("unable to open log file at: %s, exiting with error: %s", ".gvc/gvclog.log", err)
|
||||||
|
}
|
||||||
|
defer logFile.Close()
|
||||||
|
// Setup non-http logging (using middleware for Echo http logging)
|
||||||
|
logLevel, err := common.SetLogLevel(conf.LogLevel)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("invalid log level set in config, setting to info")
|
||||||
|
}
|
||||||
|
clientlog := zerolog.New(logFile)
|
||||||
|
clientlog.WithLevel(logLevel)
|
||||||
|
// Check/Setup the database with logging
|
||||||
|
database.NewDB(".gvc/gvc.db", &clientlog)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize our new cli
|
// Initialize our new cli
|
||||||
cli := clir.NewCli("gvcc", "Version control client for GVC", version)
|
cli := clir.NewCli("gvcc", "Version control client for GVC", version)
|
||||||
|
|
||||||
|
@@ -5,6 +5,7 @@ import "github.com/deranjer/gvc/common"
|
|||||||
//Gvcconfig will be the struct that holds the entire client settings
|
//Gvcconfig will be the struct that holds the entire client settings
|
||||||
type Gvcconfig struct {
|
type Gvcconfig struct {
|
||||||
Version string `toml:"version"`
|
Version string `toml:"version"`
|
||||||
|
LogLevel int `toml:"loglevel"` // Panic: 5, Fatal: 4, Error: 3, Warn: 2, Info: 1, debug: 0, trace: -1
|
||||||
RootPath string `toml:"rootpath"`
|
RootPath string `toml:"rootpath"`
|
||||||
RepoName string `toml:"reponame"`
|
RepoName string `toml:"reponame"`
|
||||||
Remotes []Remote `toml:"remote"` //The remote servers for the repo
|
Remotes []Remote `toml:"remote"` //The remote servers for the repo
|
||||||
|
29
common/logging.go
Normal file
29
common/logging.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SetLogLevel takes in the integer supplied and turns it into a log level
|
||||||
|
func SetLogLevel(loglevel int) (level zerolog.Level, err error) {
|
||||||
|
switch loglevel {
|
||||||
|
case -1:
|
||||||
|
return zerolog.TraceLevel, nil
|
||||||
|
case 0:
|
||||||
|
return zerolog.DebugLevel, nil
|
||||||
|
case 1:
|
||||||
|
return zerolog.InfoLevel, nil
|
||||||
|
case 2:
|
||||||
|
return zerolog.WarnLevel, nil
|
||||||
|
case 3:
|
||||||
|
return zerolog.ErrorLevel, nil
|
||||||
|
case 4:
|
||||||
|
return zerolog.FatalLevel, nil
|
||||||
|
case 5:
|
||||||
|
return zerolog.PanicLevel, nil
|
||||||
|
default:
|
||||||
|
return zerolog.InfoLevel, fmt.Errorf("incorrect log level set, setting it to info level")
|
||||||
|
}
|
||||||
|
}
|
@@ -5,6 +5,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/deranjer/gvc/common"
|
||||||
"github.com/deranjer/gvc/common/database"
|
"github.com/deranjer/gvc/common/database"
|
||||||
"github.com/deranjer/gvc/server/engine"
|
"github.com/deranjer/gvc/server/engine"
|
||||||
serverconfig "github.com/deranjer/gvc/server/serverconfig"
|
serverconfig "github.com/deranjer/gvc/server/serverconfig"
|
||||||
@@ -58,7 +59,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer logFile.Close()
|
defer logFile.Close()
|
||||||
// Setup non-http logging (using middleware for Echo http logging)
|
// Setup non-http logging (using middleware for Echo http logging)
|
||||||
logLevel, err := serverconfig.SetLogLevel(conf.LogLevel)
|
logLevel, err := common.SetLogLevel(conf.LogLevel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("invalid log level set in config, setting to info")
|
fmt.Println("invalid log level set in config, setting to info")
|
||||||
}
|
}
|
||||||
|
@@ -7,7 +7,6 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/deranjer/store"
|
"github.com/deranjer/store"
|
||||||
"github.com/rs/zerolog"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ConfigPath is the global path to the config that is injected from the main server package.
|
// ConfigPath is the global path to the config that is injected from the main server package.
|
||||||
@@ -35,28 +34,6 @@ func FindConfig() (string, error) {
|
|||||||
return "", fmt.Errorf("serverConfig.toml does not appear to be in the correct file format")
|
return "", fmt.Errorf("serverConfig.toml does not appear to be in the correct file format")
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLogLevel takes in the integer supplied and turns it into a log level
|
|
||||||
func SetLogLevel(loglevel int) (level zerolog.Level, err error) {
|
|
||||||
switch loglevel {
|
|
||||||
case -1:
|
|
||||||
return zerolog.TraceLevel, nil
|
|
||||||
case 0:
|
|
||||||
return zerolog.DebugLevel, nil
|
|
||||||
case 1:
|
|
||||||
return zerolog.InfoLevel, nil
|
|
||||||
case 2:
|
|
||||||
return zerolog.WarnLevel, nil
|
|
||||||
case 3:
|
|
||||||
return zerolog.ErrorLevel, nil
|
|
||||||
case 4:
|
|
||||||
return zerolog.FatalLevel, nil
|
|
||||||
case 5:
|
|
||||||
return zerolog.PanicLevel, nil
|
|
||||||
default:
|
|
||||||
return zerolog.InfoLevel, fmt.Errorf("incorrect log level set, setting it to info level")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValidateConfig will go through the entire config and do basic sanity checks and write valid values to the server struct if some are missing
|
// ValidateConfig will go through the entire config and do basic sanity checks and write valid values to the server struct if some are missing
|
||||||
func ValidateConfig(conf *GvcServerConfig, configPath string, version string) error {
|
func ValidateConfig(conf *GvcServerConfig, configPath string, version string) error {
|
||||||
if conf.Version == "" { // No version found, should we update it?
|
if conf.Version == "" { // No version found, should we update it?
|
||||||
|
Reference in New Issue
Block a user