adding logging to server

This commit is contained in:
2020-06-15 15:24:56 -04:00
parent d335549fd5
commit 88333417d4
10 changed files with 72 additions and 37 deletions

View File

@@ -1,10 +1,11 @@
logfile = "gvclog.log"
loglevel = 2
databaselocation = "gvc.db"
version = "0.1"
port = 80
ip = ""
rawport = 0
reporootpath = "F:\\repos"
databaselocation = "gvc.db"
[[repo]]
rootpath = ""

BIN
server/gvc.db Normal file

Binary file not shown.

View File

@@ -18,3 +18,4 @@
{"time":"2020-06-10T16:01:25.3086487-04:00","id":"","remote_ip":"::1","host":"localhost:80","method":"GET","uri":"/refresh/gvc","user_agent":"go-resty/2.3.0 (https://github.com/go-resty/resty)","status":200,"error":"","latency":1000600,"latency_human":"1.0006ms","bytes_in":0,"bytes_out":331}
{"time":"2020-06-10T16:03:51.1803332-04:00","id":"","remote_ip":"::1","host":"localhost:80","method":"GET","uri":"/refresh/gvc","user_agent":"go-resty/2.3.0 (https://github.com/go-resty/resty)","status":200,"error":"","latency":0,"latency_human":"0s","bytes_in":0,"bytes_out":358}
{"time":"2020-06-10T16:05:39.6232811-04:00","id":"","remote_ip":"::1","host":"localhost:80","method":"GET","uri":"/refresh/gvc","user_agent":"go-resty/2.3.0 (https://github.com/go-resty/resty)","status":200,"error":"","latency":0,"latency_human":"0s","bytes_in":0,"bytes_out":394}
{"level":"warn","module":"database","message":"No existing databse found. initialising new database"}

View File

@@ -11,6 +11,7 @@ import (
"github.com/deranjer/store"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/rs/zerolog"
)
var version = "0.1"
@@ -26,6 +27,7 @@ func main() {
fmt.Println("Since no config found, creating a default config to use...")
conf = serverconfig.GvcServerConfig{
LogFile: "gvclog.log",
LogLevel: 2,
Version: "0.1",
Port: 80,
RepoRootPath: "repos", //default repos directory will be cwd\repos
@@ -55,8 +57,15 @@ func main() {
log.Fatalf("unable to open log file at: %s, exiting with error: %s", conf.LogFile, err)
}
defer logFile.Close()
// Setup non-http logging (using middleware for Echo http logging)
logLevel, err := serverconfig.SetLogLevel(conf.LogLevel)
if err != nil {
fmt.Println("invalid log level set in config, setting to info")
}
serverlog := zerolog.New(logFile)
serverlog.WithLevel(logLevel)
// Check/Setup the database
database.NewDB(conf.DatabaseLocation)
database.NewDB(conf.DatabaseLocation, &serverlog)
// Setup the web server
e := echo.New()
// Setup the logger to print to the file specified in config

View File

@@ -7,6 +7,7 @@ import (
"path/filepath"
"github.com/deranjer/store"
"github.com/rs/zerolog"
)
// ConfigPath is the global path to the config that is injected from the main server package.
@@ -34,6 +35,28 @@ func FindConfig() (string, error) {
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
func ValidateConfig(conf *GvcServerConfig, configPath string, version string) error {
if conf.Version == "" { // No version found, should we update it?

View File

@@ -8,6 +8,7 @@ import (
// GvcServerConfig will hold the base server settings
type GvcServerConfig struct {
LogFile string `toml:"logfile"` // Where to store the echo logs
LogLevel int `toml:"loglevel"` // Panic: 5, Fatal: 4, Error: 3, Warn: 2, Info: 1, debug: 0, trace: -1
DatabaseLocation string `toml:"databaselocation"` // Location of the database
Database *storm.DB // DB Handle for passing around
Version string `toml:"version"` // The server version