Skip to content

Commit

Permalink
Merge pull request beego#35 from odiel/master
Browse files Browse the repository at this point in the history
Log levels as constants, printing time with logs
  • Loading branch information
astaxie committed Sep 27, 2013
2 parents 5cfd876 + a10bc28 commit 0b5cef8
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"runtime"
"strings"
"time"
)

// Go is a basic promise implementation: it wraps calls a function in a goroutine
Expand Down Expand Up @@ -41,6 +42,11 @@ const (
Magenta
//NRed = uint8(31) // Normal
EndColor = "\033[0m"

TRAC = "TRAC"
ERRO = "ERRO"
WARN = "WARN"
SUCC = "SUCC"
)

// colorLog colors log and print to stdout.
Expand Down Expand Up @@ -80,20 +86,22 @@ func colorLog(format string, a ...interface{}) {
log = clog + log
}

fmt.Print(log)
var currentTime = time.Now()

fmt.Print(currentTime.Format("2006-01-02 03:04:05 "+log))
}

// getColorLevel returns colored level string by given level.
func getColorLevel(level string) string {
level = strings.ToUpper(level)
switch level {
case "TRAC":
case TRAC:
return fmt.Sprintf("\033[%dm%s\033[0m", Blue, level)
case "ERRO":
case ERRO:
return fmt.Sprintf("\033[%dm%s\033[0m", Red, level)
case "WARN":
case WARN:
return fmt.Sprintf("\033[%dm%s\033[0m", Magenta, level)
case "SUCC":
case SUCC:
return fmt.Sprintf("\033[%dm%s\033[0m", Green, level)
default:
return level
Expand Down

0 comments on commit 0b5cef8

Please sign in to comment.