From 6eba150ed1c6935771060be34faafe3ddc12fedf Mon Sep 17 00:00:00 2001 From: Abeshek <49567412+abesheknarayan@users.noreply.github.com> Date: Thu, 2 Jun 2022 18:14:08 +0530 Subject: [PATCH] [Add]: logs with logrus (#2) --- .gitignore | 4 +++- README.md | 2 +- go.mod | 4 +++- go.sum | 7 +++++++ main.go | 4 +++- run_tests.sh | 0 stores/disk_store.go | 50 +++++++++++++++++++++++++++++++++++++------- utils/log.go | 24 +++++++++++++++++++++ 8 files changed, 84 insertions(+), 11 deletions(-) mode change 100644 => 100755 run_tests.sh create mode 100644 utils/log.go diff --git a/.gitignore b/.gitignore index 43c96a6..bc2a830 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ +.env + data/ -.env \ No newline at end of file +logs/ \ No newline at end of file diff --git a/README.md b/README.md index 3cc8b0a..4bebc9c 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ Go-CaskDB is a disk-based, embedded, persistent, key-value store based on the [R - [x] Get, Set KV using disk as store - [x] Loading data from disk onto memory - [x] testing +- [x] Proper logging - [ ] Support for generic key and values (right now only for strings) -- [ ] Proper logging - [ ] need better way of handling bytes (very bad rn) - [ ] Benchmarking - [ ] Crash Safety diff --git a/go.mod b/go.mod index 3f1d0f4..2b43853 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,11 @@ module github.com/abesheknarayan/go-caskdb go 1.18 require ( - github.com/davecgh/go-spew v1.1.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/joho/godotenv v1.4.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/sirupsen/logrus v1.8.1 // indirect github.com/stretchr/testify v1.7.1 // indirect + golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) diff --git a/go.sum b/go.sum index da72bf5..a260c08 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,19 @@ 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/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= 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/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 04ea185..dc8dc2d 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "os" "github.com/abesheknarayan/go-caskdb/stores" + "github.com/abesheknarayan/go-caskdb/utils" "github.com/joho/godotenv" ) @@ -14,6 +15,7 @@ func main() { if err != nil { log.Fatalf("failed to load env file") } + utils.InitLogger() path := os.Getenv("DB_PATH") booksDb, err := stores.InitDb("test", path) if err != nil { @@ -24,5 +26,5 @@ func main() { booksDb.Set("movie", "top gun maverick") fmt.Println(booksDb.Get("movie")) booksDb.CloseDB() - // booksDb.Cleanup() + booksDb.Cleanup() } diff --git a/run_tests.sh b/run_tests.sh old mode 100644 new mode 100755 diff --git a/stores/disk_store.go b/stores/disk_store.go index ef6cb5b..60a2f77 100644 --- a/stores/disk_store.go +++ b/stores/disk_store.go @@ -9,6 +9,8 @@ import ( "time" "github.com/abesheknarayan/go-caskdb/format" + "github.com/sirupsen/logrus" + log "github.com/sirupsen/logrus" ) // for now, support keys and values only with string type @@ -31,12 +33,16 @@ type DiskStore struct { // creates a new db and returns the object ref func InitDb(dbName string, path string) (*DiskStore, error) { + var l = log.WithFields(logrus.Fields{ + "method": "InitDb", + }) + // if db is already present load it or else create new db fileName := fmt.Sprintf("%s/%s.db", path, dbName) if _, err := os.Stat(fileName); errors.Is(err, os.ErrNotExist) { - fmt.Println("file doesn't exist !!") - return createDB(dbName, path) + l.Infoln("file doesn't exist !!") + return createDb(dbName, path) } // open file in binary + append mode @@ -58,14 +64,20 @@ func InitDb(dbName string, path string) (*DiskStore, error) { } // create new file -func createDB(dbName string, path string) (*DiskStore, error) { - // path := +func createDb(dbName string, path string) (*DiskStore, error) { + + var l = log.WithFields(logrus.Fields{ + "method": "createDb", + "param_dbName": dbName, + "param_path": path, + }) + l.Infoln("Attempting to create a database") + filename := fmt.Sprintf("%s/%s.db", path, dbName) - fmt.Printf("creating new file %s\n", filename) + l.Infof("creating new file %s\n", filename) f, err := os.Create(filename) if err != nil { - fmt.Println("here") return nil, err } @@ -121,6 +133,14 @@ func (d *DiskStore) loadHashIndex() error { } func (d *DiskStore) Set(key string, value string) { + + var l = log.WithFields(logrus.Fields{ + "method": "Set", + "param_key": key, + "param_value": value, + }) + l.Infof("Attempting to set a key") + // store to disk timestamp := time.Now().Unix() sz, data := format.EncodeKeyValue(timestamp, key, value) @@ -141,6 +161,12 @@ func (d *DiskStore) writeWithSync(data []byte) { func (d *DiskStore) Get(key string) string { // get key from db + var l = log.WithFields(logrus.Fields{ + "method": "Get", + "param_key": key, + }) + l.Infoln("Attempting to get value for key") + kv, ok := d.hashIndex[key] if !ok { return "" @@ -150,7 +176,7 @@ func (d *DiskStore) Get(key string) string { n, err := d.file.Read(dataByte) if err != nil || int32(n) != kv.size { - fmt.Println(err) + l.Errorln(err) } _, _, value := format.DecodeKeyValue(dataByte) @@ -159,6 +185,11 @@ func (d *DiskStore) Get(key string) string { // clears the db file and hash index func (d *DiskStore) Cleanup() { + var l = log.WithFields(logrus.Fields{ + "method": "Cleanup", + }) + l.Infoln("Cleaning up the database") + d.currentByteOffsetPosition = 0 for k := range d.hashIndex { delete(d.hashIndex, k) @@ -167,6 +198,11 @@ func (d *DiskStore) Cleanup() { } func (d *DiskStore) CloseDB() { + var l = log.WithFields(logrus.Fields{ + "method": "CloseDB", + }) + l.Infoln("Closing the database") + d.file.Sync() d.file.Close() } diff --git a/utils/log.go b/utils/log.go new file mode 100644 index 0000000..697e624 --- /dev/null +++ b/utils/log.go @@ -0,0 +1,24 @@ +package utils + +import ( + "os" + + "github.com/sirupsen/logrus" + log "github.com/sirupsen/logrus" +) + +// instance +var Logger *logrus.Logger + +func InitLogger() { + var ( + filename = "./logs/dblogs.log" + loglevel = log.DebugLevel + ) + log.SetLevel(loglevel) + f, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0666) + if err != nil { + log.Fatalf(err.Error()) + } + log.SetOutput(f) +}