From 99bdf4e3208cbca271a54a63a85f270b32fe7e51 Mon Sep 17 00:00:00 2001 From: Caj Larsson Date: Wed, 4 May 2022 22:39:00 +0800 Subject: [PATCH] logger, implements #9 --- dataswamp/logging.go | 7 +++++++ dataswamp/services.go | 5 ++++- dataswamp/services_test.go | 10 ++++++++-- main.go | 4 ---- server/bog.go | 25 +++++++++++++++--------- server/bog_test.go | 1 + server/configuration.go | 5 +++++ server/logger.go | 40 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 81 insertions(+), 16 deletions(-) create mode 100644 dataswamp/logging.go create mode 100644 server/logger.go diff --git a/dataswamp/logging.go b/dataswamp/logging.go new file mode 100644 index 0000000..c28c75f --- /dev/null +++ b/dataswamp/logging.go @@ -0,0 +1,7 @@ +package dataswamp + +type Logger interface { + Debug(format string, a ...interface{}) + Info(format string, a ...interface{}) + Warn(format string, a ...interface{}) +} diff --git a/dataswamp/services.go b/dataswamp/services.go index 0455b3e..6ed4a10 100644 --- a/dataswamp/services.go +++ b/dataswamp/services.go @@ -15,6 +15,7 @@ type SwampFileService struct { swamp_file_repo swampfile.Repository default_allowance_bytes int64 default_allowance_duration time.Duration + logger Logger } func NewSwampFileService( @@ -22,8 +23,9 @@ func NewSwampFileService( swamp_file_repo swampfile.Repository, da_bytes int64, da_duration time.Duration, + logger Logger, ) SwampFileService { - return SwampFileService{namespace_repo, swamp_file_repo, da_bytes, da_duration} + return SwampFileService{namespace_repo, swamp_file_repo, da_bytes, da_duration, logger} } func (s SwampFileService) getOrCreateNs(namespace_in string) *namespace.Namespace { @@ -114,6 +116,7 @@ func (s SwampFileService) OpenOutFile(ref swampfile.FileReference) (swampfile.Sw } func (s SwampFileService) CleanUpExpiredFiles() error { + s.logger.Info("Cleaning up expired files") nss, err := s.namespace_repo.All() if err != nil { diff --git a/dataswamp/services_test.go b/dataswamp/services_test.go index c92bd6f..99a6654 100644 --- a/dataswamp/services_test.go +++ b/dataswamp/services_test.go @@ -12,6 +12,12 @@ import ( m_swampfile "caj-larsson/bog/infrastructure/memory/swampfile" ) +type TestLogger struct{} + +func (t TestLogger) Debug(format string, a ...interface{}) {} +func (t TestLogger) Info(format string, a ...interface{}) {} +func (t TestLogger) Warn(format string, a ...interface{}) {} + var file_ref1 = swampfile.FileReference{"/path1", "ns1"} var file_ref2 = swampfile.FileReference{"/path1", "ns2"} var file_ref3 = swampfile.FileReference{"/path2", "ns1"} @@ -19,7 +25,7 @@ var file_ref3 = swampfile.FileReference{"/path2", "ns1"} func NewTestSwampFileService() SwampFileService { file_repo := m_swampfile.NewRepository() ns_repo := m_namespace.NewRepository() - return NewSwampFileService(ns_repo, file_repo, 1024, time.Hour) + return NewSwampFileService(ns_repo, file_repo, 1024, time.Hour, TestLogger{}) } func TestFileDontExist(t *testing.T) { @@ -138,7 +144,7 @@ func TestCleanUpExpired(t *testing.T) { fs := afero.NewMemMapFs() file_repo := m_swampfile.Repository{fs} ns_repo := m_namespace.NewRepository() - s := NewSwampFileService(ns_repo, file_repo, 1024, time.Hour) + s := NewSwampFileService(ns_repo, file_repo, 1024, time.Hour, TestLogger{}) fakefile := bytes.NewBufferString("My bog data") err := s.SaveFile(file_ref1, fakefile, int64(fakefile.Len())) diff --git a/main.go b/main.go index 6b94eda..7d92fdf 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "caj-larsson/bog/server" - "fmt" "io/ioutil" ) @@ -17,9 +16,6 @@ func main() { if err != nil { panic(err) } - - fmt.Printf("Dataswamp running") - bog := server.New(config) bog.Run() } diff --git a/server/bog.go b/server/bog.go index fa3bd61..42742ac 100644 --- a/server/bog.go +++ b/server/bog.go @@ -1,15 +1,15 @@ package server import ( - "fmt" - "net/http" - "strconv" - "time" "caj-larsson/bog/dataswamp" "caj-larsson/bog/dataswamp/namespace" "caj-larsson/bog/dataswamp/swampfile" fs_swampfile "caj-larsson/bog/infrastructure/fs/swampfile" sql_namespace "caj-larsson/bog/infrastructure/sqlite/namespace" + "fmt" + "net/http" + "strconv" + "time" ) type Router interface { @@ -21,6 +21,7 @@ type Bog struct { router Router file_service dataswamp.SwampFileService address string + logger dataswamp.Logger } func buildFileDataRepository(config FileConfig) swampfile.Repository { @@ -58,7 +59,7 @@ func (b *Bog) fileHandler(w http.ResponseWriter, r *http.Request) { if err != nil { panic(err) } - + b.logger.Info("Serving file '%s' of size %d from '%s'", ref.Path, swamp_file.Size(), ref.UserAgent) http.ServeContent(w, r, swamp_file.Path(), swamp_file.Modified(), swamp_file) case "POST": fallthrough @@ -70,7 +71,7 @@ func (b *Bog) fileHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(422) return } - + b.logger.Info("Recieving file '%s' of size %d from '%s'", ref.Path, size, ref.UserAgent) err = b.file_service.SaveFile(ref, r.Body, size) if err != nil { @@ -84,7 +85,7 @@ func (b *Bog) routes() { b.router.HandleFunc("/", b.fileHandler) } -func (b *Bog) cleanNamespaces(){ +func (b *Bog) cleanNamespaces() { for true { b.file_service.CleanUpExpiredFiles() time.Sleep(time.Minute * 10) @@ -98,16 +99,22 @@ func New(config *Configuration) *Bog { fsSwampRepo := buildFileDataRepository(config.File) nsRepo := buildNamespaceRepository(config.Database) + logger := ServerLogger{Debug} b.file_service = dataswamp.NewSwampFileService( - nsRepo, fsSwampRepo, config.Quota.ParsedSizeBytes(), config.Quota.ParsedDuration(), + nsRepo, + fsSwampRepo, + config.Quota.ParsedSizeBytes(), + config.Quota.ParsedDuration(), + logger, ) - + b.logger = logger b.router = new(http.ServeMux) b.routes() return b } func (b *Bog) Run() { + b.logger.Info("Starting bog on address: %s", b.address) go b.cleanNamespaces() http.ListenAndServe(b.address, b.router) } diff --git a/server/bog_test.go b/server/bog_test.go index be04b8e..bb73150 100644 --- a/server/bog_test.go +++ b/server/bog_test.go @@ -20,6 +20,7 @@ func TestApplication(t *testing.T) { swampfile.NewRepository(), 1000, time.Hour, + ServerLogger{Debug}, ) bog := Bog{ diff --git a/server/configuration.go b/server/configuration.go index 508fd3f..ead6ee9 100644 --- a/server/configuration.go +++ b/server/configuration.go @@ -54,11 +54,16 @@ type DatabaseConfig struct { Connection string } +type LoggingConfig struct { + Level string +} + type Configuration struct { Server ServerConfig File FileConfig Database DatabaseConfig Quota QuotaConfig + Logging LoggingConfig } func (c *Configuration) bindAddress() string { diff --git a/server/logger.go b/server/logger.go new file mode 100644 index 0000000..09157f9 --- /dev/null +++ b/server/logger.go @@ -0,0 +1,40 @@ +package server + +import ( + "fmt" + "time" +) + +const ( + Debug int = 0 + Info = 1 + Warn = 2 + None = 3 +) + +type ServerLogger struct { + level int +} + +func logf(level string, format string, a ...interface{}) { + head := fmt.Sprintf("%s - [%s]: ", time.Now().Format(time.RFC3339), level) + fmt.Printf(head+format+"\n", a...) +} + +func (t ServerLogger) Debug(format string, a ...interface{}) { + if t.level <= Debug { + logf("DEBUG", format, a...) + } +} + +func (t ServerLogger) Info(format string, a ...interface{}) { + if t.level <= Info { + logf("INFO", format, a...) + } +} + +func (t ServerLogger) Warn(format string, a ...interface{}) { + if t.level <= Warn { + logf("WARN", format, a...) + } +}