Permalink
Browse files

GetLogLevel helper to convert string to LogLevel

Useful for configuring log level (from .yml for example)

Makes things a bit more consistent internally too
w/o using the LEVELS map directly

Change-Id: Ie36dc6fd5d0c6f0a17d35df9b0673e28b8792f62
  • Loading branch information...
dougm committed Oct 6, 2012
1 parent ae828d5 commit 42bba176e53e7bc6d20587ee253f7d6af7ae4331
Showing with 49 additions and 47 deletions.
  1. +6 −6 http_handler.go
  2. +1 −1 http_handler_test.go
  3. +0 −5 json_prettifier.go
  4. +9 −1 json_prettifier_test.go
  5. +25 −28 log_level.go
  6. +8 −6 log_level_test.go
View
@@ -34,9 +34,9 @@ func loggerHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Can't parse the parameters", http.StatusBadRequest)
return
}
- level, ok := LEVELS[levelParams.Level]
- if !ok {
- http.Error(w, fmt.Sprintf("No level with that name exists : %s", levelParams.Level), http.StatusBadRequest)
+ level, err := GetLogLevel(levelParams.Level)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
return
}
logger.level = level
@@ -74,9 +74,9 @@ func regExpHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Can't parse the parameters", http.StatusBadRequest)
return
}
- level, ok := LEVELS[params.Level]
- if !ok {
- http.Error(w, fmt.Sprintf("No level with the name exists : %s", params.Level), http.StatusBadRequest)
+ level, err := GetLogLevel(params.Level)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = SetLoggerRegexp(params.RegExp, level)
View
@@ -127,7 +127,7 @@ func (s *HttpHandlerSuite) TestPutRegexpWithWrongParams2(c *C) {
m, _ := ioutil.ReadAll(r.Body)
c.Assert(r.StatusCode, Equals, http.StatusBadRequest)
- c.Assert(string(m), Equals, "No level with the name exists : NotExistingLevel\n")
+ c.Assert(string(m), Equals, "No level with that name exists : NotExistingLevel\n")
}
func (s *HttpHandlerSuite) TestPutRegexpWithWrongParams3(c *C) {
View
@@ -63,11 +63,6 @@ func NewJsonPrettifier(flag int) *JsonPrettifier {
func (p *JsonPrettifier) DecodeJsonLogEntry(logEntry string) (*Record, error) {
record := new(Record)
err := json.Unmarshal([]byte(logEntry), record)
-
- if err == nil {
- record.Level = LEVELS[record.Level.name]
- }
-
return record, err
}
View
@@ -72,9 +72,17 @@ func (s *JsonPrettifierSuite) TestDecodeLogEntry(c *C) {
c.Assert(err, IsNil)
c.Assert(record.Timestamp, Equals, t)
c.Assert(record.Line, Equals, l)
- c.Assert(record.Level, Equals, LOG_INFO)
+ c.Assert(record.Level.name, Equals, LOG_INFO.name)
+ c.Assert(record.Level.priority, Equals, LOG_INFO.priority)
c.Assert(record.Method, Matches, ".*TestDecodeLogEntry$")
c.Assert(record.Message, Equals, "Hello, world")
c.Assert(record.File, Matches, ".*json_prettifier_test.go")
c.Assert(record.Data["foo"], Equals, "bar")
+
+ // test err with invalid log level
+ entry = `{"Message":"hi","Level":"enoent"}`
+ record, err = prettifier.DecodeJsonLogEntry(entry)
+
+ c.Assert(err, NotNil)
+ c.Assert(err.Error(), Matches, ".*enoent.*")
}
View
@@ -2,7 +2,6 @@ package steno
import (
"encoding/json"
- "errors"
"fmt"
)
@@ -11,39 +10,37 @@ type LogLevel struct {
priority int
}
-var LOG_OFF = NewLogLevel("off", 0)
-var LOG_FATAL = NewLogLevel("fatal", 1)
-var LOG_ERROR = NewLogLevel("error", 5)
-var LOG_WARN = NewLogLevel("warn", 10)
-var LOG_INFO = NewLogLevel("info", 15)
-var LOG_DEBUG = NewLogLevel("debug", 16)
-var LOG_DEBUG1 = NewLogLevel("debug1", 17)
-var LOG_DEBUG2 = NewLogLevel("debug2", 18)
-var LOG_ALL = NewLogLevel("all", 30)
+var (
+ LOG_OFF = newLogLevel("off", 0)
+ LOG_FATAL = newLogLevel("fatal", 1)
+ LOG_ERROR = newLogLevel("error", 5)
+ LOG_WARN = newLogLevel("warn", 10)
+ LOG_INFO = newLogLevel("info", 15)
+ LOG_DEBUG = newLogLevel("debug", 16)
+ LOG_DEBUG1 = newLogLevel("debug1", 17)
+ LOG_DEBUG2 = newLogLevel("debug2", 18)
+ LOG_ALL = newLogLevel("all", 30)
+)
-var LEVELS = map[string]*LogLevel{
- "off": LOG_OFF,
- "fatal": LOG_FATAL,
- "error": LOG_ERROR,
- "warn": LOG_WARN,
- "info": LOG_INFO,
- "debug": LOG_DEBUG,
- "debug1": LOG_DEBUG1,
- "debug2": LOG_DEBUG2,
- "all": LOG_ALL,
-}
+var levels = map[string]*LogLevel{}
-func NewLogLevel(name string, priority int) *LogLevel {
+func newLogLevel(name string, priority int) *LogLevel {
level := new(LogLevel)
level.name = name
level.priority = priority
+ levels[name] = level
+
return level
}
-func lookupLevel(name string) *LogLevel {
- return LEVELS[name]
+func GetLogLevel(name string) (*LogLevel, error) {
+ if level, ok := levels[name]; ok {
+ return level, nil
+ }
+ err := fmt.Errorf("No level with that name exists : %s", name)
+ return nil, err
}
func (level *LogLevel) MarshalJSON() ([]byte, error) {
@@ -56,11 +53,11 @@ func (level *LogLevel) UnmarshalJSON(data []byte) error {
if err != nil {
return err
}
- if _, ok := LEVELS[n]; !ok {
- return errors.New(fmt.Sprintf("No level with the name exists: %s", n))
+ if l, err := GetLogLevel(n); err != nil {
+ return err
+ } else {
+ *level = *l
}
- level.name = n
- level.priority = LEVELS[n].priority
return nil
}
View
@@ -11,24 +11,26 @@ type LogLevelSuite struct {
var _ = Suite(&LogLevelSuite{})
func (s *LogLevelSuite) TestNewLogLevel(c *C) {
- level := NewLogLevel("foobar", 100)
+ level := newLogLevel("foobar", 100)
c.Assert(level, NotNil)
c.Assert(level.name, Equals, "foobar")
c.Assert(level.priority, Equals, 100)
}
-func (s *LogLevelSuite) TestLookupLevel(c *C) {
- infoLevel := lookupLevel("info")
+func (s *LogLevelSuite) TestGetLevel(c *C) {
+ infoLevel, err := GetLogLevel("info")
c.Assert(infoLevel, Equals, LOG_INFO)
+ c.Assert(err, IsNil)
}
-func (s *LogLevelSuite) TestLookupNotExistLevel(c *C) {
- notExistLevel := lookupLevel("foobar")
+func (s *LogLevelSuite) TestGetNotExistLevel(c *C) {
+ notExistLevel, err := GetLogLevel("foobar")
c.Assert(notExistLevel, IsNil)
+ c.Assert(err, NotNil)
}
func (s *LogLevelSuite) TestImplementedInterfaces(c *C) {
- var v interface{} = NewLogLevel("foobar", 1)
+ var v interface{} = newLogLevel("foobar", 1)
// *LogLevel should implement json.Marshaler interface
_, ok := v.(json.Marshaler)

0 comments on commit 42bba17

Please sign in to comment.