1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-28 09:45:47 +00:00
Beego/logs/console.go

102 lines
2.5 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"
"os"
"runtime"
2016-01-23 08:24:58 +00:00
"time"
2013-08-27 15:48:58 +00:00
)
2015-09-11 15:08:24 +00:00
// brush is a color join function
type brush func(string) string
2015-09-11 15:08:24 +00:00
// newBrush return a fix color Brush
func newBrush(color string) brush {
pre := "\033["
reset := "\033[0m"
return func(text string) string {
return pre + color + "m" + text + reset
}
}
2015-09-11 15:08:24 +00:00
var colors = []brush{
2016-01-26 01:35:39 +00:00
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
newBrush("1;34"), // Informational blue
2017-03-21 15:55:40 +00:00
newBrush("1;44"), // Debug Background blue
}
2015-09-11 15:08:24 +00:00
// consoleWriter implements LoggerInterface and writes messages to terminal.
type consoleWriter struct {
2016-02-03 06:42:38 +00:00
lg *logWriter
Level int `json:"level"`
Colorful bool `json:"color"` //this filed is useful only when system's terminal supports color
2013-08-27 15:48:58 +00:00
}
2015-09-11 15:08:24 +00:00
// NewConsole create ConsoleWriter returning as LoggerInterface.
func NewConsole() Logger {
cw := &consoleWriter{
2016-02-03 06:42:38 +00:00
lg: newLogWriter(os.Stdout),
Level: LevelDebug,
2016-03-11 07:29:52 +00:00
Colorful: runtime.GOOS != "windows",
2015-02-23 03:42:46 +00:00
}
2013-08-27 15:48:58 +00:00
return cw
}
2015-09-11 15:08:24 +00:00
// Init init console logger.
2016-01-26 01:29:04 +00:00
// jsonConfig like '{"level":LevelTrace}'.
func (c *consoleWriter) Init(jsonConfig string) error {
if len(jsonConfig) == 0 {
return nil
}
err := json.Unmarshal([]byte(jsonConfig), c)
if runtime.GOOS == "windows" {
c.Colorful = false
}
return err
2013-08-27 15:48:58 +00:00
}
2015-09-11 15:08:24 +00:00
// WriteMsg write message in console.
2016-01-23 08:24:58 +00:00
func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int) error {
if level > c.Level {
2013-08-27 15:48:58 +00:00
return nil
}
if c.Colorful {
2016-02-03 06:42:38 +00:00
msg = colors[level](msg)
}
2016-02-03 06:42:38 +00:00
c.lg.println(when, msg)
2013-08-27 15:48:58 +00:00
return nil
}
2015-09-11 15:08:24 +00:00
// Destroy implementing method. empty.
func (c *consoleWriter) Destroy() {
2013-08-27 15:48:58 +00:00
}
2015-09-11 15:08:24 +00:00
// Flush implementing method. empty.
func (c *consoleWriter) Flush() {
2013-11-27 09:50:10 +00:00
}
2013-08-27 15:48:58 +00:00
func init() {
2016-03-24 09:38:26 +00:00
Register(AdapterConsole, NewConsole)
2013-08-27 15:48:58 +00:00
}