Implement log package as drop-in replacement for handling Debug log-level

master
Harshavardhana 10 years ago
parent 80892c5c9b
commit 914962bd93
  1. 2
      main.go
  2. 10
      pkg/api/api_bucket_handlers.go
  3. 14
      pkg/api/api_object_handlers.go
  4. 10
      pkg/api/api_policy_handlers.go
  5. 3
      pkg/server/httpserver/httpserver.go
  6. 2
      pkg/storage/file/file_test.go
  7. 34
      pkg/utils/log/definitions.go
  8. 26
      pkg/utils/log/log-debug.go
  9. 26
      pkg/utils/log/log-normal.go
  10. 44
      pkg/utils/log/log.go
  11. 19
      pkg/utils/log/log_test.go

@ -17,11 +17,11 @@
package main package main
import ( import (
"log"
"os" "os"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/minio-io/minio/pkg/server" "github.com/minio-io/minio/pkg/server"
"github.com/minio-io/minio/pkg/utils/log"
) )
func getStorageType(input string) server.StorageType { func getStorageType(input string) server.StorageType {

@ -17,11 +17,11 @@
package api package api
import ( import (
"log"
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
mstorage "github.com/minio-io/minio/pkg/storage" mstorage "github.com/minio-io/minio/pkg/storage"
"github.com/minio-io/minio/pkg/utils/log"
) )
// GET Bucket (List Objects) // GET Bucket (List Objects)
@ -62,7 +62,7 @@ func (server *minioAPI) listObjectsHandler(w http.ResponseWriter, req *http.Requ
case mstorage.ImplementationError: case mstorage.ImplementationError:
{ {
// Embed error log on server side // Embed error log on server side
log.Println(err) log.Errorln(err)
error := errorCodeError(InternalError) error := errorCodeError(InternalError)
errorResponse := getErrorResponse(error, bucket) errorResponse := getErrorResponse(error, bucket)
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)
@ -100,7 +100,7 @@ func (server *minioAPI) listBucketsHandler(w http.ResponseWriter, req *http.Requ
} }
case mstorage.ImplementationError: case mstorage.ImplementationError:
{ {
log.Println(err) log.Errorln(err)
error := errorCodeError(InternalError) error := errorCodeError(InternalError)
errorResponse := getErrorResponse(error, "") errorResponse := getErrorResponse(error, "")
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)
@ -108,7 +108,7 @@ func (server *minioAPI) listBucketsHandler(w http.ResponseWriter, req *http.Requ
} }
case mstorage.BackendCorrupted: case mstorage.BackendCorrupted:
{ {
log.Println(err) log.Errorln(err)
error := errorCodeError(InternalError) error := errorCodeError(InternalError)
errorResponse := getErrorResponse(error, "") errorResponse := getErrorResponse(error, "")
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)
@ -155,7 +155,7 @@ func (server *minioAPI) putBucketHandler(w http.ResponseWriter, req *http.Reques
case mstorage.ImplementationError: case mstorage.ImplementationError:
{ {
// Embed errors log on server side // Embed errors log on server side
log.Println(err) log.Errorln(err)
error := errorCodeError(InternalError) error := errorCodeError(InternalError)
errorResponse := getErrorResponse(error, bucket) errorResponse := getErrorResponse(error, bucket)
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)

@ -17,11 +17,11 @@
package api package api
import ( import (
"log"
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
mstorage "github.com/minio-io/minio/pkg/storage" mstorage "github.com/minio-io/minio/pkg/storage"
"github.com/minio-io/minio/pkg/utils/log"
) )
// GET Object // GET Object
@ -42,7 +42,7 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques
log.Println("Found: " + bucket + "#" + object) log.Println("Found: " + bucket + "#" + object)
httpRange, err := newRange(req, metadata.Size) httpRange, err := newRange(req, metadata.Size)
if err != nil { if err != nil {
log.Println(err) log.Errorln(err)
error := errorCodeError(InvalidRange) error := errorCodeError(InvalidRange)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)
@ -53,7 +53,7 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques
case true: case true:
writeObjectHeaders(w, metadata) writeObjectHeaders(w, metadata)
if _, err := server.storage.GetObject(w, bucket, object); err != nil { if _, err := server.storage.GetObject(w, bucket, object); err != nil {
log.Println(err) log.Errorln(err)
error := errorCodeError(InternalError) error := errorCodeError(InternalError)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)
@ -66,7 +66,7 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques
w.WriteHeader(http.StatusPartialContent) w.WriteHeader(http.StatusPartialContent)
_, err := server.storage.GetPartialObject(w, bucket, object, httpRange.start, httpRange.length) _, err := server.storage.GetPartialObject(w, bucket, object, httpRange.start, httpRange.length)
if err != nil { if err != nil {
log.Println(err) log.Errorln(err)
error := errorCodeError(InternalError) error := errorCodeError(InternalError)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)
@ -100,7 +100,7 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques
case mstorage.ImplementationError: case mstorage.ImplementationError:
{ {
// Embed errors log on serve side // Embed errors log on serve side
log.Println(err) log.Errorln(err)
error := errorCodeError(InternalError) error := errorCodeError(InternalError)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)
@ -140,7 +140,7 @@ func (server *minioAPI) headObjectHandler(w http.ResponseWriter, req *http.Reque
case mstorage.ImplementationError: case mstorage.ImplementationError:
{ {
// Embed error log on server side // Embed error log on server side
log.Println(err) log.Errorln(err)
error := errorCodeError(InternalError) error := errorCodeError(InternalError)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)
@ -175,7 +175,7 @@ func (server *minioAPI) putObjectHandler(w http.ResponseWriter, req *http.Reques
case mstorage.ImplementationError: case mstorage.ImplementationError:
{ {
// Embed error log on server side // Embed error log on server side
log.Println(err) log.Errorln(err)
error := errorCodeError(InternalError) error := errorCodeError(InternalError)
errorResponse := getErrorResponse(error, "/"+bucket+"/"+object) errorResponse := getErrorResponse(error, "/"+bucket+"/"+object)
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)

@ -18,11 +18,11 @@ package api
import ( import (
"encoding/json" "encoding/json"
"log"
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
mstorage "github.com/minio-io/minio/pkg/storage" mstorage "github.com/minio-io/minio/pkg/storage"
"github.com/minio-io/minio/pkg/utils/log"
) )
// PUT Bucket policy // PUT Bucket policy
@ -67,7 +67,7 @@ func (server *minioAPI) putBucketPolicyHandler(w http.ResponseWriter, req *http.
} }
case mstorage.BackendCorrupted: case mstorage.BackendCorrupted:
{ {
log.Println(err) log.Errorln(err)
error := errorCodeError(InternalError) error := errorCodeError(InternalError)
errorResponse := getErrorResponse(error, bucket) errorResponse := getErrorResponse(error, bucket)
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)
@ -75,7 +75,7 @@ func (server *minioAPI) putBucketPolicyHandler(w http.ResponseWriter, req *http.
} }
case mstorage.ImplementationError: case mstorage.ImplementationError:
{ {
log.Println(err) log.Errorln(err)
error := errorCodeError(InternalError) error := errorCodeError(InternalError)
errorResponse := getErrorResponse(error, bucket) errorResponse := getErrorResponse(error, bucket)
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)
@ -131,7 +131,7 @@ func (server *minioAPI) getBucketPolicyHandler(w http.ResponseWriter, req *http.
} }
case mstorage.BackendCorrupted: case mstorage.BackendCorrupted:
{ {
log.Println(err) log.Errorln(err)
error := errorCodeError(InternalError) error := errorCodeError(InternalError)
errorResponse := getErrorResponse(error, bucket) errorResponse := getErrorResponse(error, bucket)
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)
@ -139,7 +139,7 @@ func (server *minioAPI) getBucketPolicyHandler(w http.ResponseWriter, req *http.
} }
case mstorage.ImplementationError: case mstorage.ImplementationError:
{ {
log.Println(err) log.Errorln(err)
error := errorCodeError(InternalError) error := errorCodeError(InternalError)
errorResponse := getErrorResponse(error, bucket) errorResponse := getErrorResponse(error, bucket)
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)

@ -17,8 +17,9 @@
package httpserver package httpserver
import ( import (
"log"
"net/http" "net/http"
"github.com/minio-io/minio/pkg/utils/log"
) )
// Config - http server config // Config - http server config

@ -24,7 +24,6 @@ import (
mstorage "github.com/minio-io/minio/pkg/storage" mstorage "github.com/minio-io/minio/pkg/storage"
. "gopkg.in/check.v1" . "gopkg.in/check.v1"
"log"
) )
func Test(t *testing.T) { TestingT(t) } func Test(t *testing.T) { TestingT(t) }
@ -47,7 +46,6 @@ func (s *MySuite) TestAPISuite(c *C) {
} }
func removeRoots(c *C, roots []string) { func removeRoots(c *C, roots []string) {
log.Println(roots)
for _, root := range roots { for _, root := range roots {
err := os.RemoveAll(root) err := os.RemoveAll(root)
c.Check(err, IsNil) c.Check(err, IsNil)

@ -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
)

@ -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...)
}
}

@ -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...)
}
}

@ -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...)
}
}

@ -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)
}
Loading…
Cancel
Save