From 914962bd93ac6cbd235f66a327c810cb39ec2a86 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 23 Mar 2015 18:12:20 -0700 Subject: [PATCH] Implement log package as drop-in replacement for handling Debug log-level --- main.go | 2 +- pkg/api/api_bucket_handlers.go | 10 +++---- pkg/api/api_object_handlers.go | 14 ++++----- pkg/api/api_policy_handlers.go | 10 +++---- pkg/server/httpserver/httpserver.go | 3 +- pkg/storage/file/file_test.go | 2 -- pkg/utils/log/definitions.go | 34 ++++++++++++++++++++++ pkg/utils/log/log-debug.go | 26 +++++++++++++++++ pkg/utils/log/log-normal.go | 26 +++++++++++++++++ pkg/utils/log/log.go | 44 +++++++++++++++++++++++++++++ pkg/utils/log/log_test.go | 19 +++++++++++++ 11 files changed, 169 insertions(+), 21 deletions(-) create mode 100644 pkg/utils/log/definitions.go create mode 100644 pkg/utils/log/log-debug.go create mode 100644 pkg/utils/log/log-normal.go create mode 100644 pkg/utils/log/log.go create mode 100644 pkg/utils/log/log_test.go diff --git a/main.go b/main.go index 9b1349eea..0ea8e2c3e 100644 --- a/main.go +++ b/main.go @@ -17,11 +17,11 @@ package main import ( - "log" "os" "github.com/codegangsta/cli" "github.com/minio-io/minio/pkg/server" + "github.com/minio-io/minio/pkg/utils/log" ) func getStorageType(input string) server.StorageType { diff --git a/pkg/api/api_bucket_handlers.go b/pkg/api/api_bucket_handlers.go index bf38db716..095687d2d 100644 --- a/pkg/api/api_bucket_handlers.go +++ b/pkg/api/api_bucket_handlers.go @@ -17,11 +17,11 @@ package api import ( - "log" "net/http" "github.com/gorilla/mux" mstorage "github.com/minio-io/minio/pkg/storage" + "github.com/minio-io/minio/pkg/utils/log" ) // GET Bucket (List Objects) @@ -62,7 +62,7 @@ func (server *minioAPI) listObjectsHandler(w http.ResponseWriter, req *http.Requ case mstorage.ImplementationError: { // Embed error log on server side - log.Println(err) + log.Errorln(err) error := errorCodeError(InternalError) errorResponse := getErrorResponse(error, bucket) w.WriteHeader(error.HTTPStatusCode) @@ -100,7 +100,7 @@ func (server *minioAPI) listBucketsHandler(w http.ResponseWriter, req *http.Requ } case mstorage.ImplementationError: { - log.Println(err) + log.Errorln(err) error := errorCodeError(InternalError) errorResponse := getErrorResponse(error, "") w.WriteHeader(error.HTTPStatusCode) @@ -108,7 +108,7 @@ func (server *minioAPI) listBucketsHandler(w http.ResponseWriter, req *http.Requ } case mstorage.BackendCorrupted: { - log.Println(err) + log.Errorln(err) error := errorCodeError(InternalError) errorResponse := getErrorResponse(error, "") w.WriteHeader(error.HTTPStatusCode) @@ -155,7 +155,7 @@ func (server *minioAPI) putBucketHandler(w http.ResponseWriter, req *http.Reques case mstorage.ImplementationError: { // Embed errors log on server side - log.Println(err) + log.Errorln(err) error := errorCodeError(InternalError) errorResponse := getErrorResponse(error, bucket) w.WriteHeader(error.HTTPStatusCode) diff --git a/pkg/api/api_object_handlers.go b/pkg/api/api_object_handlers.go index 5976ea08a..7a949b236 100644 --- a/pkg/api/api_object_handlers.go +++ b/pkg/api/api_object_handlers.go @@ -17,11 +17,11 @@ package api import ( - "log" "net/http" "github.com/gorilla/mux" mstorage "github.com/minio-io/minio/pkg/storage" + "github.com/minio-io/minio/pkg/utils/log" ) // GET Object @@ -42,7 +42,7 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques log.Println("Found: " + bucket + "#" + object) httpRange, err := newRange(req, metadata.Size) if err != nil { - log.Println(err) + log.Errorln(err) error := errorCodeError(InvalidRange) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) w.WriteHeader(error.HTTPStatusCode) @@ -53,7 +53,7 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques case true: writeObjectHeaders(w, metadata) if _, err := server.storage.GetObject(w, bucket, object); err != nil { - log.Println(err) + log.Errorln(err) error := errorCodeError(InternalError) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) w.WriteHeader(error.HTTPStatusCode) @@ -66,7 +66,7 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques w.WriteHeader(http.StatusPartialContent) _, err := server.storage.GetPartialObject(w, bucket, object, httpRange.start, httpRange.length) if err != nil { - log.Println(err) + log.Errorln(err) error := errorCodeError(InternalError) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) w.WriteHeader(error.HTTPStatusCode) @@ -100,7 +100,7 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques case mstorage.ImplementationError: { // Embed errors log on serve side - log.Println(err) + log.Errorln(err) error := errorCodeError(InternalError) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) w.WriteHeader(error.HTTPStatusCode) @@ -140,7 +140,7 @@ func (server *minioAPI) headObjectHandler(w http.ResponseWriter, req *http.Reque case mstorage.ImplementationError: { // Embed error log on server side - log.Println(err) + log.Errorln(err) error := errorCodeError(InternalError) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) w.WriteHeader(error.HTTPStatusCode) @@ -175,7 +175,7 @@ func (server *minioAPI) putObjectHandler(w http.ResponseWriter, req *http.Reques case mstorage.ImplementationError: { // Embed error log on server side - log.Println(err) + log.Errorln(err) error := errorCodeError(InternalError) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) w.WriteHeader(error.HTTPStatusCode) diff --git a/pkg/api/api_policy_handlers.go b/pkg/api/api_policy_handlers.go index d7d60f033..c50b8c5c1 100644 --- a/pkg/api/api_policy_handlers.go +++ b/pkg/api/api_policy_handlers.go @@ -18,11 +18,11 @@ package api import ( "encoding/json" - "log" "net/http" "github.com/gorilla/mux" mstorage "github.com/minio-io/minio/pkg/storage" + "github.com/minio-io/minio/pkg/utils/log" ) // PUT Bucket policy @@ -67,7 +67,7 @@ func (server *minioAPI) putBucketPolicyHandler(w http.ResponseWriter, req *http. } case mstorage.BackendCorrupted: { - log.Println(err) + log.Errorln(err) error := errorCodeError(InternalError) errorResponse := getErrorResponse(error, bucket) w.WriteHeader(error.HTTPStatusCode) @@ -75,7 +75,7 @@ func (server *minioAPI) putBucketPolicyHandler(w http.ResponseWriter, req *http. } case mstorage.ImplementationError: { - log.Println(err) + log.Errorln(err) error := errorCodeError(InternalError) errorResponse := getErrorResponse(error, bucket) w.WriteHeader(error.HTTPStatusCode) @@ -131,7 +131,7 @@ func (server *minioAPI) getBucketPolicyHandler(w http.ResponseWriter, req *http. } case mstorage.BackendCorrupted: { - log.Println(err) + log.Errorln(err) error := errorCodeError(InternalError) errorResponse := getErrorResponse(error, bucket) w.WriteHeader(error.HTTPStatusCode) @@ -139,7 +139,7 @@ func (server *minioAPI) getBucketPolicyHandler(w http.ResponseWriter, req *http. } case mstorage.ImplementationError: { - log.Println(err) + log.Errorln(err) error := errorCodeError(InternalError) errorResponse := getErrorResponse(error, bucket) w.WriteHeader(error.HTTPStatusCode) diff --git a/pkg/server/httpserver/httpserver.go b/pkg/server/httpserver/httpserver.go index bace3b404..aa0bdad6f 100644 --- a/pkg/server/httpserver/httpserver.go +++ b/pkg/server/httpserver/httpserver.go @@ -17,8 +17,9 @@ package httpserver import ( - "log" "net/http" + + "github.com/minio-io/minio/pkg/utils/log" ) // Config - http server config diff --git a/pkg/storage/file/file_test.go b/pkg/storage/file/file_test.go index 372dc31b1..ca9bbb088 100644 --- a/pkg/storage/file/file_test.go +++ b/pkg/storage/file/file_test.go @@ -24,7 +24,6 @@ import ( mstorage "github.com/minio-io/minio/pkg/storage" . "gopkg.in/check.v1" - "log" ) func Test(t *testing.T) { TestingT(t) } @@ -47,7 +46,6 @@ func (s *MySuite) TestAPISuite(c *C) { } func removeRoots(c *C, roots []string) { - log.Println(roots) for _, root := range roots { err := os.RemoveAll(root) c.Check(err, IsNil) diff --git a/pkg/utils/log/definitions.go b/pkg/utils/log/definitions.go new file mode 100644 index 000000000..f68ec38cb --- /dev/null +++ b/pkg/utils/log/definitions.go @@ -0,0 +1,34 @@ +package log + +import ( + internalLog "log" +) + +// Fatal - prints string and terminates +var Fatal = internalLog.Fatal + +// Fatalf - prints formatted string and terminates +var Fatalf = internalLog.Fatalf + +// Fatalln - prints with newline and terminates +var Fatalln = internalLog.Fatalln + +// Error - prints string but does not terminate +func Error(args ...interface{}) { internalLog.Print(args...) } + +// Errorf - prints formatted string but does not terminate +func Errorf(s string, args ...interface{}) { internalLog.Printf(s, args...) } + +// Errorln - prints string with newline but does not terminate +func Errorln(args ...interface{}) { internalLog.Println(args...) } + +// Level - logging level +type Level int + +// Different log levels +const ( + QuietLOG Level = iota + NormalLOG + DebugLOG + TraceLOG +) diff --git a/pkg/utils/log/log-debug.go b/pkg/utils/log/log-debug.go new file mode 100644 index 000000000..1d8c329ea --- /dev/null +++ b/pkg/utils/log/log-debug.go @@ -0,0 +1,26 @@ +package log + +import ( + internalLog "log" +) + +// Debug logging +func Debug(args ...interface{}) { + if verify(DebugLOG) { + internalLog.Print(args...) + } +} + +// Debugf formatted debug logging +func Debugf(s string, args ...interface{}) { + if verify(DebugLOG) { + internalLog.Printf(s, args...) + } +} + +// Debugln logging with newline +func Debugln(args ...interface{}) { + if verify(DebugLOG) { + internalLog.Println(args...) + } +} diff --git a/pkg/utils/log/log-normal.go b/pkg/utils/log/log-normal.go new file mode 100644 index 000000000..f90a704cc --- /dev/null +++ b/pkg/utils/log/log-normal.go @@ -0,0 +1,26 @@ +package log + +import ( + internalLog "log" +) + +// Print - Normal logging +func Print(args ...interface{}) { + if verify(NormalLOG) { + internalLog.Print(args...) + } +} + +// Printf - Normal formatted logging +func Printf(s string, args ...interface{}) { + if verify(NormalLOG) { + internalLog.Printf(s, args...) + } +} + +// Println - Normal logging with newline +func Println(args ...interface{}) { + if verify(NormalLOG) { + internalLog.Println(args...) + } +} diff --git a/pkg/utils/log/log.go b/pkg/utils/log/log.go new file mode 100644 index 000000000..8dfcd060f --- /dev/null +++ b/pkg/utils/log/log.go @@ -0,0 +1,44 @@ +package log + +import ( + internalLog "log" + "sync" +) + +// Global variable to be set +var verbosity = NormalLOG +var mutex sync.RWMutex + +func verify(level Level) bool { + mutex.RLock() + defer mutex.RUnlock() + return Level(verbosity) >= level +} + +// SetVerbosity - set log level value globally +func SetVerbosity(level Level) { + mutex.Lock() + defer mutex.Unlock() + verbosity = level +} + +// Log - variable logging against global verbosity +func Log(level Level, args ...interface{}) { + if verify(level) { + internalLog.Print(args...) + } +} + +// Logf - variable formatted logging against global verbosity +func Logf(level Level, s string, args ...interface{}) { + if verify(level) { + internalLog.Printf(s, args...) + } +} + +// Logln - variable logging with newline against global verbosity +func Logln(level Level, args ...interface{}) { + if verify(level) { + internalLog.Println(args...) + } +} diff --git a/pkg/utils/log/log_test.go b/pkg/utils/log/log_test.go new file mode 100644 index 000000000..c244927c6 --- /dev/null +++ b/pkg/utils/log/log_test.go @@ -0,0 +1,19 @@ +package log + +import ( + "testing" + + . "gopkg.in/check.v1" +) + +type MySuite struct{} + +var _ = Suite(&MySuite{}) + +func Test(t *testing.T) { TestingT(t) } + +func (s *MySuite) TestLogLevel(c *C) { + c.Assert(verbosity, Equals, NormalLOG) + SetVerbosity(QuietLOG) + c.Assert(verbosity, Equals, QuietLOG) +}