Skip to content

Commit

Permalink
[Add]: logs with logrus (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
abesheknarayan authored Jun 2, 2022
1 parent aa05b11 commit 6eba150
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.env

data/
.env
logs/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/abesheknarayan/go-caskdb/stores"
"github.com/abesheknarayan/go-caskdb/utils"
"github.com/joho/godotenv"
)

Expand All @@ -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 {
Expand All @@ -24,5 +26,5 @@ func main() {
booksDb.Set("movie", "top gun maverick")
fmt.Println(booksDb.Get("movie"))
booksDb.CloseDB()
// booksDb.Cleanup()
booksDb.Cleanup()
}
Empty file modified run_tests.sh
100644 → 100755
Empty file.
50 changes: 43 additions & 7 deletions stores/disk_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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 ""
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()
}
24 changes: 24 additions & 0 deletions utils/log.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 6eba150

Please sign in to comment.