frp/vendor/github.com/fatedier/beego/logs/console.go

103 lines
2.5 KiB
Go
Raw Normal View History

2016-01-27 21:24:36 +08:00
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
package logs
import (
"encoding/json"
"os"
"runtime"
2016-08-11 16:32:05 +08:00
"time"
2016-01-27 21:24:36 +08:00
)
2016-08-11 16:32:05 +08:00
// brush is a color join function
type brush func(string) string
2016-01-27 21:24:36 +08:00
2016-08-11 16:32:05 +08:00
// newBrush return a fix color Brush
func newBrush(color string) brush {
2016-01-27 21:24:36 +08:00
pre := "\033["
reset := "\033[0m"
return func(text string) string {
return pre + color + "m" + text + reset
}
}
2016-08-11 16:32:05 +08:00
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
newBrush("1;34"), // Informational blue
newBrush("1;34"), // Debug blue
2017-10-24 22:53:20 +08:00
newBrush("1;34"), // Trace blue
2016-01-27 21:24:36 +08:00
}
2016-08-11 16:32:05 +08:00
// consoleWriter implements LoggerInterface and writes messages to terminal.
type consoleWriter struct {
lg *logWriter
Level int `json:"level"`
Colorful bool `json:"color"` //this filed is useful only when system's terminal supports color
2016-01-27 21:24:36 +08:00
}
2016-08-11 16:32:05 +08:00
// NewConsole create ConsoleWriter returning as LoggerInterface.
func NewConsole() Logger {
cw := &consoleWriter{
lg: newLogWriter(os.Stdout),
2017-10-24 22:53:20 +08:00
Level: LevelTrace,
2016-08-11 16:32:05 +08:00
Colorful: runtime.GOOS != "windows",
2016-01-27 21:24:36 +08:00
}
return cw
}
2016-08-11 16:32:05 +08:00
// Init init console logger.
// jsonConfig like '{"level":LevelTrace}'.
func (c *consoleWriter) Init(jsonConfig string) error {
if len(jsonConfig) == 0 {
2016-01-27 21:24:36 +08:00
return nil
}
2016-08-11 16:32:05 +08:00
err := json.Unmarshal([]byte(jsonConfig), c)
if runtime.GOOS == "windows" {
c.Colorful = false
}
return err
2016-01-27 21:24:36 +08:00
}
2016-08-11 16:32:05 +08:00
// WriteMsg write message in console.
func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int) error {
2016-01-27 21:24:36 +08:00
if level > c.Level {
return nil
}
2016-08-11 16:32:05 +08:00
if c.Colorful {
msg = colors[level](msg)
2016-01-27 21:24:36 +08:00
}
2016-08-11 16:32:05 +08:00
c.lg.println(when, msg)
2016-01-27 21:24:36 +08:00
return nil
}
2016-08-11 16:32:05 +08:00
// Destroy implementing method. empty.
func (c *consoleWriter) Destroy() {
2016-01-27 21:24:36 +08:00
}
2016-08-11 16:32:05 +08:00
// Flush implementing method. empty.
func (c *consoleWriter) Flush() {
2016-01-27 21:24:36 +08:00
}
func init() {
Register(AdapterConsole, NewConsole)
2016-01-27 21:24:36 +08:00
}