Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add slog logging #20

Merged
merged 2 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions example.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
package main

import (
"fmt"
"log/slog"
"os"
"time"

"github.com/stym06/rebuf/rebuf"
)

func writeToStdout(data []byte) error {
fmt.Println(string(data))
slog.Info(string(data))
return nil
}

func main() {

logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

//Init the RebufOptions
rebufOptions := &rebuf.RebufOptions{
LogDir: "/Users/satyamraj/personal/rebuf/data",
FsyncTime: 5 * time.Second,
MaxLogSize: 50,
MaxSegments: 5,
Logger: logger,
}

//Init Rebuf
rebuf, err := rebuf.Init(rebufOptions)
if err != nil {
fmt.Println("Error during Rebuf creation: " + err.Error())
logger.Info("Error during Rebuf creation: " + err.Error())
}

defer rebuf.Close()

// Write Bytes
for i := 0; i < 30; i++ {
fmt.Printf("Writing data iter#%d \n", i)
logger.Info("Writing data: ", "iter", i)
go rebuf.Write([]byte("Hello world"))
time.Sleep(300 * time.Millisecond)
}
Expand All @@ -41,7 +45,7 @@ func main() {
rebuf.Replay(writeToStdout)

if err != nil {
fmt.Println(err.Error())
logger.Info(err.Error())
}

time.Sleep(30 * time.Second)
Expand Down
11 changes: 7 additions & 4 deletions rebuf/rebuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package rebuf
import (
"bufio"
"encoding/binary"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"strconv"
Expand All @@ -19,6 +19,7 @@ type RebufOptions struct {
MaxLogSize int64
FsyncTime time.Duration
MaxSegments int
Logger *slog.Logger
}

type Rebuf struct {
Expand All @@ -32,6 +33,7 @@ type Rebuf struct {
tmpLogFile *os.File
ticker time.Ticker
mu sync.Mutex
log *slog.Logger
}

func Init(options *RebufOptions) (*Rebuf, error) {
Expand All @@ -56,6 +58,7 @@ func Init(options *RebufOptions) (*Rebuf, error) {
maxSegments: options.MaxSegments,
tmpLogFile: tmpLogFile,
ticker: *time.NewTicker(options.FsyncTime),
log: options.Logger,
}

err = rebuf.openExistingOrCreateNew(options.LogDir)
Expand Down Expand Up @@ -84,20 +87,20 @@ func (rebuf *Rebuf) Write(data []byte) error {
if rebuf.logSize+int64(len(data))+8 > rebuf.maxLogSize {

if rebuf.segmentCount > rebuf.maxSegments {
fmt.Printf("Reached maxSegments %d", rebuf.maxSegments)
rebuf.log.Info("Reached maxSegments", "segments", rebuf.maxSegments)

//delete the oldest log file
oldestLogFileName, err := utils.GetOldestSegmentFile(rebuf.logDir)
if err != nil {
return err
}
fmt.Printf("Would have deleted %s", oldestLogFileName)
rebuf.log.Info("Would have deleted ", "oldestLogFileName", oldestLogFileName)
os.Remove(filepath.Join(rebuf.logDir, oldestLogFileName))

rebuf.segmentCount--
}

fmt.Printf("Log size will be greater than %d. Moving to segment %d \n", rebuf.logSize, rebuf.currentSegmentId+1)
rebuf.log.Info("Log size will be greater than", "logsize", rebuf.logSize, "Moving to", rebuf.currentSegmentId+1)
rebuf.bufWriter.Flush()
rebuf.tmpLogFile.Sync()

Expand Down
3 changes: 0 additions & 3 deletions utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"fmt"
"io"
"os"
"strconv"
Expand All @@ -24,7 +23,6 @@ func IsDirectoryEmpty(dirPath string) (bool, error) {
if !strings.HasSuffix(file.Name(), ".tmp") {
filteredFiles = append(filteredFiles, file)
}
fmt.Printf("File name: %s", file.Name())
}
if err != nil && err != io.EOF {
return false, err
Expand Down Expand Up @@ -58,7 +56,6 @@ func GetLatestSegmentId(logDir string) (int, error) {
latestFileName = file.Name()
}
}
fmt.Println(latestFileName)
segmentCount, err := strconv.Atoi(strings.Split(latestFileName, "-")[1])
if err != nil {
return 0, err
Expand Down
Loading