1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-15 08:30:55 +00:00
Beego/core/logs/console.go

130 lines
3.2 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00: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"
2020-09-11 13:10:12 +00:00
"fmt"
2020-07-22 14:50:08 +00:00
"os"
"strings"
2020-09-11 13:10:12 +00:00
"github.com/pkg/errors"
2020-07-22 14:50:08 +00:00
"github.com/shiena/ansicolor"
)
// brush is a color join function
type brush func(string) string
2020-08-06 15:07:18 +00:00
// newBrush returns a fix color Brush
2020-07-22 14:50:08 +00:00
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
newBrush("1;34"), // Informational blue
newBrush("1;44"), // Debug Background blue
}
// consoleWriter implements LoggerInterface and writes messages to terminal.
type consoleWriter struct {
2020-09-11 13:10:12 +00:00
lg *logWriter
formatter LogFormatter
Formatter string `json:"formatter"`
Level int `json:"level"`
Colorful bool `json:"color"` // this filed is useful only when system's terminal supports color
2020-07-22 14:50:08 +00:00
}
2020-08-20 18:00:35 +00:00
func (c *consoleWriter) Format(lm *LogMsg) string {
2020-09-11 13:10:12 +00:00
msg := lm.OldStyleFormat()
if c.Colorful {
2020-09-19 15:04:40 +00:00
msg = strings.Replace(msg, levelPrefix[lm.Level], colors[lm.Level](levelPrefix[lm.Level]), 1)
2020-09-11 13:10:12 +00:00
}
2020-08-20 18:00:35 +00:00
h, _, _ := formatTimeHeader(lm.When)
bytes := append(append(h, msg...), '\n')
return string(bytes)
2020-09-11 13:10:12 +00:00
}
2020-08-20 18:00:35 +00:00
2020-09-11 13:10:12 +00:00
func (c *consoleWriter) SetFormatter(f LogFormatter) {
c.formatter = f
2020-08-20 18:00:35 +00:00
}
2020-08-06 15:07:18 +00:00
// NewConsole creates ConsoleWriter returning as LoggerInterface.
2020-07-22 14:50:08 +00:00
func NewConsole() Logger {
2020-09-19 15:04:40 +00:00
return newConsole()
}
func newConsole() *consoleWriter {
2020-07-22 14:50:08 +00:00
cw := &consoleWriter{
lg: newLogWriter(ansicolor.NewAnsiColorWriter(os.Stdout)),
Level: LevelDebug,
Colorful: true,
}
2020-09-11 13:10:12 +00:00
cw.formatter = cw
2020-07-22 14:50:08 +00:00
return cw
}
2020-08-06 15:07:18 +00:00
// Init initianlizes the console logger.
// jsonConfig must be in the format '{"level":LevelTrace}'
2020-09-11 13:10:12 +00:00
func (c *consoleWriter) Init(config string) error {
2020-09-11 13:10:12 +00:00
if len(config) == 0 {
2020-07-22 14:50:08 +00:00
return nil
}
2020-09-11 13:10:12 +00:00
res := json.Unmarshal([]byte(config), c)
if res == nil && len(c.Formatter) > 0 {
fmtr, ok := GetFormatter(c.Formatter)
if !ok {
return errors.New(fmt.Sprintf("the formatter with name: %s not found", c.Formatter))
}
c.formatter = fmtr
}
return res
2020-07-22 14:50:08 +00:00
}
2020-08-06 15:07:18 +00:00
// WriteMsg writes message in console.
func (c *consoleWriter) WriteMsg(lm *LogMsg) error {
if lm.Level > c.Level {
2020-07-22 14:50:08 +00:00
return nil
}
2020-09-11 13:10:12 +00:00
msg := c.formatter.Format(lm)
2020-08-20 18:00:35 +00:00
c.lg.writeln(msg)
2020-07-22 14:50:08 +00:00
return nil
}
// Destroy implementing method. empty.
func (c *consoleWriter) Destroy() {
}
// Flush implementing method. empty.
func (c *consoleWriter) Flush() {
}
func init() {
Register(AdapterConsole, NewConsole)
}