Merge pull request #351 from harshavardhana/pr_out_implement_log_package_as_drop_in_replacement_for_handling_debug_log_level

This commit is contained in:
Harshavardhana 2015-03-23 18:55:14 -07:00
commit 7c1c4b8a02
11 changed files with 169 additions and 21 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

44
pkg/utils/log/log.go Normal file
View File

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

19
pkg/utils/log/log_test.go Normal file
View File

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