1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-05 06:40:54 +00:00
Beego/logs/console.go

96 lines
2.2 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-08-27 15:48:58 +00:00
package logs
import (
"encoding/json"
"log"
"os"
"runtime"
2013-08-27 15:48:58 +00:00
)
type Brush func(string) string
func NewBrush(color string) Brush {
pre := "\033["
reset := "\033[0m"
return func(text string) string {
return pre + color + "m" + text + reset
}
}
var colors = []Brush{
NewBrush("1;37"), // Emergency white
NewBrush("1;36"), // Alert cyan
NewBrush("1;35"), // Critical magenta
NewBrush("1;31"), // Error red
NewBrush("1;33"), // Warning yellow
NewBrush("1;32"), // Notice green
2014-08-20 03:54:25 +00:00
NewBrush("1;34"), // Informational blue
NewBrush("1;34"), // Debug blue
}
2013-12-30 15:32:57 +00:00
// ConsoleWriter implements LoggerInterface and writes messages to terminal.
2013-08-27 15:48:58 +00:00
type ConsoleWriter struct {
lg *log.Logger
Level int `json:"level"`
2013-08-27 15:48:58 +00:00
}
2013-12-30 15:32:57 +00:00
// create ConsoleWriter returning as LoggerInterface.
2013-08-27 15:48:58 +00:00
func NewConsole() LoggerInterface {
2015-02-23 03:42:46 +00:00
cw := &ConsoleWriter{
lg: log.New(os.Stdout, "", log.Ldate|log.Ltime),
Level: LevelDebug,
}
2013-08-27 15:48:58 +00:00
return cw
}
2013-12-30 15:32:57 +00:00
// init console logger.
// jsonconfig like '{"level":LevelTrace}'.
2013-08-27 15:48:58 +00:00
func (c *ConsoleWriter) Init(jsonconfig string) error {
if len(jsonconfig) == 0 {
return nil
}
2015-02-23 03:42:46 +00:00
return json.Unmarshal([]byte(jsonconfig), c)
2013-08-27 15:48:58 +00:00
}
2013-12-30 15:32:57 +00:00
// write message in console.
2013-08-27 15:48:58 +00:00
func (c *ConsoleWriter) WriteMsg(msg string, level int) error {
if level > c.Level {
2013-08-27 15:48:58 +00:00
return nil
}
if goos := runtime.GOOS; goos == "windows" {
c.lg.Println(msg)
2015-02-23 03:42:46 +00:00
return nil
}
2015-02-23 03:42:46 +00:00
c.lg.Println(colors[level](msg))
2013-08-27 15:48:58 +00:00
return nil
}
2013-12-30 15:32:57 +00:00
// implementing method. empty.
2013-08-27 15:48:58 +00:00
func (c *ConsoleWriter) Destroy() {
}
2013-12-30 15:32:57 +00:00
// implementing method. empty.
2013-11-27 09:50:10 +00:00
func (c *ConsoleWriter) Flush() {
}
2013-08-27 15:48:58 +00:00
func init() {
Register("console", NewConsole)
}