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,31 +1,23 @@
package database
import (
"io"
logger "github.com/apsdehal/go-logger"
"github.com/asdine/storm"
"github.com/rs/zerolog"
)
type DB struct {
*storm.DB
*logger.Logger
*zerolog.Logger
}
// NewDB returns a new database object,
// it configures the database for you.
func NewDB(dbPath, format string, logLevel int, logWriter io.WriteCloser) (*DB, error) {
//Note! Do not use logger as you have no idea if logWriter has been configured for output yet
func NewDB(dbPath string, log *zerolog.Logger) (*DB, error) {
var db DB
log, err := logger.New("db logger", 1, logWriter)
if err != nil {
return &db, err
}
log.SetLogLevel(logger.LogLevel(logLevel))
log.SetFormat(format)
db.Logger = log
databaseLogger := log.With().Str("module", "database").Logger() // Setting up a sub logger for the database module
db.Logger = &databaseLogger
if err := db.ConfigureDB(dbPath); err != nil {
log.ErrorF("Error configuring the database ", err)
db.Err(err).Msg("unable to configure database")
return &db, err
}
return &db, nil

View File

@@ -16,14 +16,14 @@ func (db *DB) ConfigureDB(dbPath string) error {
var file File
if err := db.One("Name", "-- All Files --", &file); err != nil {
if err.Error() != "not found" {
db.ErrorF("Error finding file by path %s", err)
db.Err(err).Msg("Error finding file by path")
return err
}
db.WarningF("No existing databse found. initialising new database")
db.Warn().Msg("No existing databse found. initialising new database")
file.Name = "-- All Files --"
//file.Ignore = true //this is currently not used however could result in this file being ignored when file watching (etc) starts
if err := db.Save(&file); err != nil {
db.ErrorF("Error storing the diff %s", err)
db.Err(err).Msg("Error storing the diff")
return err
}
}
@@ -39,26 +39,24 @@ func (db *DB) CheckIfFileCurrentlyMonitored(src string, hash [16]byte) (File, er
//TODO: check this actually works still (don't need hash passed to this anymore)
if err := db.One("Path", src, &file); err != nil {
if err.Error() != "not found" {
db.ErrorF("Error finding file by path %s", err)
db.Err(err).Msg("Error finding file by path")
return File{}, err
}
db.WarningF("no file found, %s", err)
db.Warn().Msg("no file found")
return File{}, err
} else {
}
return file, nil
}
}
// RetrieveWatchedFiles all files that are in the database as "watched files"
// This can be used to trigger the same files to be watched again
func (db *DB) RetrieveWatchedFiles() ([]File, error) {
var files []File
if err := db.All(&files); err != nil {
db.ErrorF("Error retrieving all watched files %s", err)
db.Err(err).Msg("Error retrieving all watched files")
return []File{}, err
} else {
return files, nil
}
return files, nil
}
// RetrieveAllDiffs retrieves all files that are in the database as "watched files"
@@ -66,7 +64,7 @@ func (db *DB) RetrieveWatchedFiles() ([]File, error) {
// func (db *DB) RetrieveAllDiffs() ([]DiffObject, error) {
// var diffs []DiffObject
// if err := db.All(&diffs); err != nil {
// db.ErrorF("Error retrieving all diffs %s", err)
// db.Err(err).Msg("Error retrieving all diffs ")
// return []DiffObject{}, err
// } else {
// return diffs, nil
@@ -78,7 +76,7 @@ func (db *DB) RetrieveWatchedFiles() ([]File, error) {
// of losing references
func (db *DB) InitialiseFileInDatabase(file File) (int, error) {
if err := db.Save(&file); err != nil {
db.ErrorF("Error initialising file in database %s", err)
db.Err(err).Msg("Error initialising file in database")
return file.ID, err
}
return file.ID, nil
@@ -88,7 +86,7 @@ func (db *DB) InitialiseFileInDatabase(file File) (int, error) {
func (db *DB) FindFileByPath(filePath string) (File, error) {
var file File
if err := db.One("Path", filePath, &file); err != nil {
db.ErrorF("Error finding file by path %s", err)
db.Err(err).Msg("Error finding file by path")
return File{}, err
}
return file, nil
@@ -98,7 +96,7 @@ func (db *DB) FindFileByPath(filePath string) (File, error) {
func (db *DB) FindFileByID(ID int) (File, error) {
var file File
if err := db.One("ID", ID, &file); err != nil {
db.ErrorF("Error finding file by path %s", err)
db.Err(err).Msg("Error finding file by path")
return File{}, err
}
return file, nil
@@ -107,7 +105,7 @@ func (db *DB) FindFileByID(ID int) (File, error) {
// UpdateFileData updates the current base file that diffs will compare to
func (db *DB) UpdateFileData(filePath, basePath string, hash [16]byte) error {
if file, err := db.FindFileByPath(filePath); err != nil {
db.ErrorF("Error updating the file base %s", err)
db.Err(err).Msg("Error updating the file base")
return err
} else {
err := db.Update(&File{ID: file.ID, CurrentBase: basePath, CurrentHash: hash})
@@ -137,11 +135,11 @@ func (db *DB) RetrieveDiffsForFileByPath(filePath string) ([]DiffObject, error)
var objDiffs []DiffObject
var subDiffs []DiffObject
if err := db.Find("Object", filePath, &objDiffs); err != nil && err.Error() != "not found" {
db.ErrorF("Error finding diff by object %s", err)
db.Err(err).Msg("Error finding diff by object")
return []DiffObject{}, err
}
if err := db.Find("Subject", filePath, &subDiffs); err != nil && err.Error() != "not found" {
db.ErrorF("Error finding diff by subject %s", err)
db.Err(err).Msg("Error finding diff by subject")
return []DiffObject{}, err
}
return append(objDiffs, subDiffs...), nil
@@ -152,7 +150,7 @@ func (db *DB) RetrieveDiffsForFileByPath(filePath string) ([]DiffObject, error)
// TODO: decide what to do with diffs in memory
// func (db *DB) StoreDiff(diff DiffObject) error {
// if err := db.Save(&diff); err != nil {
// db.ErrorF("Error storing the diff %s", err)
// db.Err(err).Msg("Error storing the diff")
// return err
// }
// return nil
@@ -162,7 +160,7 @@ func (db *DB) RetrieveDiffsForFileByPath(filePath string) ([]DiffObject, error)
func (db *DB) FindDiffByPath(patchPath string) (DiffObject, error) {
var diff DiffObject
if err := db.One("DiffPath", patchPath, &diff); err != nil {
db.ErrorF("Error finding diff by path %s", err)
db.Err(err).Msg("Error finding diff by path")
return DiffObject{}, err
}
return diff, nil
@@ -172,7 +170,7 @@ func (db *DB) FindDiffByPath(patchPath string) (DiffObject, error) {
func (db *DB) RetrieveDiffsByID(ID int) (DiffObject, error) {
var diff DiffObject
if err := db.One("ID", ID, &diff); err != nil {
db.ErrorF("Error finding diff by ID %s", err)
db.Err(err).Msg("Error finding diff by ID")
return DiffObject{}, err
}
return diff, nil
@@ -182,7 +180,7 @@ func (db *DB) RetrieveDiffsByID(ID int) (DiffObject, error) {
func (db *DB) UpdateDescription(patchID int, description string) error {
fmt.Println("attempting to path with id ", patchID, " description ", description)
if err := db.Update(&DiffObject{ID: patchID, Description: description}); err != nil {
db.ErrorF("Error changing diff label %s", err)
db.Err(err).Msg("Error changing diff label")
return err
}
return nil

1
go.mod
View File

@@ -20,6 +20,7 @@ require (
github.com/labstack/gommon v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/radovskyb/watcher v1.0.7
github.com/rs/zerolog v1.19.0
github.com/valyala/fasttemplate v1.1.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
go.etcd.io/bbolt v1.3.4 // indirect

9
go.sum
View File

@@ -10,6 +10,7 @@ github.com/apsdehal/go-logger v0.0.0-20190515212710-b0d6ccfee0e6 h1:qISSdUEX4sjD
github.com/apsdehal/go-logger v0.0.0-20190515212710-b0d6ccfee0e6/go.mod h1:U3/8D6R9+bVpX0ORZjV+3mU9pQ86m7h1lESgJbXNvXA=
github.com/asdine/storm v2.1.2+incompatible h1:dczuIkyqwY2LrtXPz8ixMrU/OFgZp71kbKTHGrXYt/Q=
github.com/asdine/storm v2.1.2+incompatible/go.mod h1:RarYDc9hq1UPLImuiXK3BIWPJLdIygvV3PsInK0FbVQ=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deranjer/clir v1.0.5 h1:tEunZj5qJLYNBtzMQ/vH8hEAIv4NptWFmTldsP9U2qY=
@@ -49,10 +50,14 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE=
github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.19.0 h1:hYz4ZVdUgjXTBUmrkrw55j1nHx68LfOKIQk5IYtyScg=
github.com/rs/zerolog v1.19.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -72,8 +77,10 @@ golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d h1:1ZiEyfaQIg3Qh0EoqpwAak
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200513185701-a91f0712d120 h1:EZ3cVSzKOlJxAd8e8YAJ7no8nNypTxexh/YE/xW3ZEY=
golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -88,6 +95,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.6 h1:lMO5rYAqUxkmaj76jAkRUvt5JZgFymx/+Q5Mzfivuhc=

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