Skip to content

Commit

Permalink
Merge pull request #34 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 1.7.1
  • Loading branch information
andyone committed Mar 16, 2016
2 parents f358a34 + 0b01e74 commit f41b12b
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 13 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

#### v1.7.1

* `[log]` Improved min level changing

#### v1.7.0

* `[fsutil]` Fixed major bug with closing file descriptor after directory listing
Expand Down
90 changes: 79 additions & 11 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"fmt"
"io"
"os"
"strings"
"time"
)

Expand Down Expand Up @@ -79,6 +80,18 @@ var TimeFormat = "2006/01/02 15:04:05.000"

// ////////////////////////////////////////////////////////////////////////////////// //

var logLevelsNames = map[string]int{
"debug": 0,
"info": 1,
"warn": 2,
"warning": 2,
"error": 3,
"crit": 4,
"critical": 4,
}

// ////////////////////////////////////////////////////////////////////////////////// //

// New creates new logger struct
func New(file string, perms os.FileMode) (*Logger, error) {
logger := &Logger{
Expand All @@ -104,9 +117,9 @@ func Reopen() error {
return Global.Reopen()
}

// MinLevel defines minimal logging level for global logger (1 by default)
func MinLevel(level int) {
Global.MinLevel(level)
// MinLevel defines minimal logging level
func MinLevel(level interface{}) error {
return Global.MinLevel(level)
}

// Set change global logger output target
Expand Down Expand Up @@ -181,20 +194,75 @@ func (l *Logger) Reopen() error {
return l.Set(l.file, l.perms)
}

// MinLevel defines minimal logging level for logger (1 by default)
func (l *Logger) MinLevel(level int) {
// MinLevel defines minimal logging level
func (l *Logger) MinLevel(level interface{}) error {
if l == nil {
return
return errors.New("Logger is nil")
}

levelCode := INFO

switch level.(type) {

case int:
levelCode = level.(int)

case int8:
levelCode = int(level.(int8))

case int16:
levelCode = int(level.(int16))

case int32:
levelCode = int(level.(int32))

case int64:
levelCode = int(level.(int64))

case uint:
levelCode = int(level.(uint))

case uint8:
levelCode = int(level.(uint8))

case uint16:
levelCode = int(level.(uint16))

case uint32:
levelCode = int(level.(uint32))

case uint64:
levelCode = int(level.(uint64))

case float32:
levelCode = int(level.(float32))

case float64:
levelCode = int(level.(float64))

case string:
code, ok := logLevelsNames[strings.ToLower(level.(string))]

if !ok {
return errors.New("Unknown level " + level.(string))
}

levelCode = code

default:
return errors.New("Unexpected level type")
}

switch {
case level < DEBUG:
level = DEBUG
case level > CRIT:
level = CRIT
case levelCode < DEBUG:
levelCode = DEBUG
case levelCode > CRIT:
levelCode = CRIT
}

l.level = level
l.level = levelCode

return nil
}

// EnableBufIO enable buffered I/O support
Expand Down
27 changes: 25 additions & 2 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,31 @@ func (ls *LogSuite) TestErrors(c *C) {
func (ls *LogSuite) TestLevel(c *C) {
l := &Logger{level: WARN}

l.MinLevel(-1)
l.MinLevel(6)
c.Assert(l.MinLevel(-1), IsNil)
c.Assert(l.MinLevel(6), IsNil)
c.Assert(l.MinLevel("debug"), IsNil)
c.Assert(l.MinLevel("info"), IsNil)
c.Assert(l.MinLevel("warn"), IsNil)
c.Assert(l.MinLevel("warning"), IsNil)
c.Assert(l.MinLevel("error"), IsNil)
c.Assert(l.MinLevel("crit"), IsNil)
c.Assert(l.MinLevel("critical"), IsNil)
c.Assert(l.MinLevel(int8(1)), IsNil)
c.Assert(l.MinLevel(int16(1)), IsNil)
c.Assert(l.MinLevel(int32(1)), IsNil)
c.Assert(l.MinLevel(int64(1)), IsNil)
c.Assert(l.MinLevel(uint(1)), IsNil)
c.Assert(l.MinLevel(uint8(1)), IsNil)
c.Assert(l.MinLevel(uint16(1)), IsNil)
c.Assert(l.MinLevel(uint32(1)), IsNil)
c.Assert(l.MinLevel(uint64(1)), IsNil)
c.Assert(l.MinLevel(float32(1)), IsNil)
c.Assert(l.MinLevel(float64(1)), IsNil)

c.Assert(l.MinLevel("abcd"), NotNil)
c.Assert(l.MinLevel(time.Now()), NotNil)

l.MinLevel("crit")

_, err := l.Print(ERROR, "error")

Expand Down

0 comments on commit f41b12b

Please sign in to comment.