mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 07:40:54 +00:00
New opts formatter working for console
This commit is contained in:
parent
d24f861629
commit
2b39ff7837
@ -18,6 +18,8 @@ import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/astaxie/beego/pkg/common"
|
||||
)
|
||||
|
||||
// connWriter implements LoggerInterface.
|
||||
@ -25,8 +27,6 @@ import (
|
||||
type connWriter struct {
|
||||
lg *logWriter
|
||||
innerWriter io.WriteCloser
|
||||
UseCustomFormatter bool
|
||||
CustomFormatter func(*LogMsg) string
|
||||
ReconnectOnMsg bool `json:"reconnectOnMsg"`
|
||||
Reconnect bool `json:"reconnect"`
|
||||
Net string `json:"net"`
|
||||
@ -47,13 +47,13 @@ func (c *connWriter) Format(lm *LogMsg) string {
|
||||
|
||||
// Init initializes a connection writer with json config.
|
||||
// json config only needs they "level" key
|
||||
func (c *connWriter) Init(jsonConfig string, LogFormatter ...func(*LogMsg) string) error {
|
||||
for _, elem := range LogFormatter {
|
||||
if elem != nil {
|
||||
c.UseCustomFormatter = true
|
||||
c.CustomFormatter = elem
|
||||
}
|
||||
}
|
||||
func (c *connWriter) Init(jsonConfig string, opts ...common.SimpleKV) error {
|
||||
// for _, elem := range LogFormatter {
|
||||
// if elem != nil {
|
||||
// c.UseCustomFormatter = true
|
||||
// c.CustomFormatter = elem
|
||||
// }
|
||||
// }
|
||||
|
||||
return json.Unmarshal([]byte(jsonConfig), c)
|
||||
}
|
||||
|
@ -19,6 +19,8 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego/pkg/common"
|
||||
|
||||
"github.com/shiena/ansicolor"
|
||||
)
|
||||
|
||||
@ -48,8 +50,7 @@ var colors = []brush{
|
||||
// consoleWriter implements LoggerInterface and writes messages to terminal.
|
||||
type consoleWriter struct {
|
||||
lg *logWriter
|
||||
UseCustomFormatter bool
|
||||
CustomFormatter func(*LogMsg) string
|
||||
customFormatter func(*LogMsg) string
|
||||
Level int `json:"level"`
|
||||
Colorful bool `json:"color"` //this filed is useful only when system's terminal supports color
|
||||
}
|
||||
@ -80,11 +81,16 @@ func NewConsole() Logger {
|
||||
|
||||
// Init initianlizes the console logger.
|
||||
// jsonConfig must be in the format '{"level":LevelTrace}'
|
||||
func (c *consoleWriter) Init(jsonConfig string, LogFormatter ...func(*LogMsg) string) error {
|
||||
for _, elem := range LogFormatter {
|
||||
if elem != nil {
|
||||
c.UseCustomFormatter = true
|
||||
c.CustomFormatter = elem
|
||||
// func (c *consoleWriter) Init(jsonConfig string, LogFormatter ...func(*LogMsg) string) error {
|
||||
func (c *consoleWriter) Init(jsonConfig string, opts ...common.SimpleKV) error {
|
||||
|
||||
for _, elem := range opts {
|
||||
if elem.Key == "formatter" {
|
||||
formatter, err := GetFormatter(elem)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.customFormatter = formatter
|
||||
}
|
||||
}
|
||||
|
||||
@ -107,10 +113,11 @@ func (c *consoleWriter) WriteMsg(lm *LogMsg) error {
|
||||
|
||||
msg := ""
|
||||
|
||||
if c.UseCustomFormatter {
|
||||
msg = c.CustomFormatter(lm)
|
||||
if c.customFormatter != nil {
|
||||
msg = c.customFormatter(lm)
|
||||
} else {
|
||||
msg = c.Format(lm)
|
||||
|
||||
}
|
||||
|
||||
c.lg.writeln(msg)
|
||||
|
@ -27,6 +27,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/pkg/common"
|
||||
)
|
||||
|
||||
// fileLogWriter implements LoggerInterface.
|
||||
@ -107,13 +109,13 @@ func (w *fileLogWriter) Format(lm *LogMsg) string {
|
||||
// "rotate":true,
|
||||
// "perm":"0600"
|
||||
// }
|
||||
func (w *fileLogWriter) Init(jsonConfig string, LogFormatter ...func(*LogMsg) string) error {
|
||||
for _, elem := range LogFormatter {
|
||||
if elem != nil {
|
||||
w.UseCustomFormatter = true
|
||||
w.CustomFormatter = elem
|
||||
}
|
||||
}
|
||||
func (w *fileLogWriter) Init(jsonConfig string, opts ...common.SimpleKV) error {
|
||||
// for _, elem := range LogFormatter {
|
||||
// if elem != nil {
|
||||
// w.UseCustomFormatter = true
|
||||
// w.CustomFormatter = elem
|
||||
// }
|
||||
// }
|
||||
|
||||
err := json.Unmarshal([]byte(jsonConfig), w)
|
||||
if err != nil {
|
||||
|
@ -5,6 +5,8 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/astaxie/beego/pkg/common"
|
||||
)
|
||||
|
||||
// JLWriter implements beego LoggerInterface and is used to send jiaoliao webhook
|
||||
@ -25,15 +27,15 @@ func newJLWriter() Logger {
|
||||
}
|
||||
|
||||
// Init JLWriter with json config string
|
||||
func (s *JLWriter) Init(jsonconfig string, LogFormatter ...func(*LogMsg) string) error {
|
||||
for _, elem := range LogFormatter {
|
||||
if elem != nil {
|
||||
s.UseCustomFormatter = true
|
||||
s.CustomFormatter = elem
|
||||
}
|
||||
}
|
||||
func (s *JLWriter) Init(jsonConfig string, opts ...common.SimpleKV) error {
|
||||
// for _, elem := range LogFormatter {
|
||||
// if elem != nil {
|
||||
// s.UseCustomFormatter = true
|
||||
// s.CustomFormatter = elem
|
||||
// }
|
||||
// }
|
||||
|
||||
return json.Unmarshal([]byte(jsonconfig), s)
|
||||
return json.Unmarshal([]byte(jsonConfig), s)
|
||||
}
|
||||
|
||||
func (s *JLWriter) Format(lm *LogMsg) string {
|
||||
|
@ -38,10 +38,13 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/pkg/common"
|
||||
)
|
||||
|
||||
// RFC5424 log message levels.
|
||||
@ -84,7 +87,7 @@ type newLoggerFunc func() Logger
|
||||
|
||||
// Logger defines the behavior of a log provider.
|
||||
type Logger interface {
|
||||
Init(config string, LogFormatter ...func(*LogMsg) string) error
|
||||
Init(config string, opts ...common.SimpleKV) error
|
||||
WriteMsg(lm *LogMsg) error
|
||||
Format(lm *LogMsg) string
|
||||
Destroy()
|
||||
@ -210,7 +213,7 @@ func (bl *BeeLogger) setLogger(adapterName string, configs ...string) error {
|
||||
// Global formatter overrides the default set formatter
|
||||
// but not adapter specific formatters set with logs.SetLoggerWithOpts()
|
||||
if bl.globalFormatter != nil {
|
||||
err = lg.Init(config, bl.globalFormatter)
|
||||
err = lg.Init(config)
|
||||
} else {
|
||||
err = lg.Init(config)
|
||||
}
|
||||
@ -401,9 +404,21 @@ func (bl *BeeLogger) startLogger() {
|
||||
}
|
||||
}
|
||||
|
||||
// Get the formatter from the opts common.SimpleKV structure
|
||||
// Looks for a key: "formatter" with value: func(*LogMsg) string
|
||||
func GetFormatter(opts common.SimpleKV) (func(*LogMsg) string, error) {
|
||||
if strings.ToLower(opts.Key.(string)) == "formatter" {
|
||||
formatterInterface := reflect.ValueOf(opts.Value).Interface()
|
||||
formatterFunc := formatterInterface.(func(*LogMsg) string)
|
||||
return formatterFunc, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("no \"formatter\" key given in simpleKV")
|
||||
}
|
||||
|
||||
// SetLoggerWithOpts sets a log adapter with a user defined logging format. Config must be valid JSON
|
||||
// such as: {"interval":360}
|
||||
func (bl *BeeLogger) setLoggerWithOpts(adapterName string, formatterFunc func(*LogMsg) string, configs ...string) error {
|
||||
func (bl *BeeLogger) setLoggerWithOpts(adapterName string, opts common.SimpleKV, configs ...string) error {
|
||||
config := append(configs, "{}")[0]
|
||||
for _, l := range bl.outputs {
|
||||
if l.name == adapterName {
|
||||
@ -416,12 +431,12 @@ func (bl *BeeLogger) setLoggerWithOpts(adapterName string, formatterFunc func(*L
|
||||
return fmt.Errorf("logs: unknown adaptername %q (forgotten Register?)", adapterName)
|
||||
}
|
||||
|
||||
if formatterFunc == nil {
|
||||
return fmt.Errorf("No formatter set for %s log adapter", adapterName)
|
||||
if opts.Key == nil {
|
||||
return fmt.Errorf("No SimpleKV struct set for %s log adapter", adapterName)
|
||||
}
|
||||
|
||||
lg := logAdapter()
|
||||
err := lg.Init(config, formatterFunc)
|
||||
err := lg.Init(config, opts)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "logs.BeeLogger.SetLogger: "+err.Error())
|
||||
return err
|
||||
@ -436,21 +451,22 @@ func (bl *BeeLogger) setLoggerWithOpts(adapterName string, formatterFunc func(*L
|
||||
}
|
||||
|
||||
// SetLogger provides a given logger adapter into BeeLogger with config string.
|
||||
func (bl *BeeLogger) SetLoggerWithOpts(adapterName string, formatterFunc func(*LogMsg) string, configs ...string) error {
|
||||
func (bl *BeeLogger) SetLoggerWithOpts(adapterName string, opts common.SimpleKV, configs ...string) error {
|
||||
bl.lock.Lock()
|
||||
defer bl.lock.Unlock()
|
||||
if !bl.init {
|
||||
bl.outputs = []*nameLogger{}
|
||||
bl.init = true
|
||||
}
|
||||
return bl.setLoggerWithOpts(adapterName, formatterFunc, configs...)
|
||||
return bl.setLoggerWithOpts(adapterName, opts, configs...)
|
||||
}
|
||||
|
||||
// SetLoggerWIthOpts sets a given log adapter with a custom log adapter.
|
||||
// Log Adapter must be given in the form common.SimpleKV{Key: "formatter": Value: struct.FormatFunc}
|
||||
// where FormatFunc has the signature func(*LogMsg) string
|
||||
func SetLoggerWithOpts(adapter string, config []string, formatterFunc func(*LogMsg) string) error {
|
||||
err := beeLogger.SetLoggerWithOpts(adapter, formatterFunc, config...)
|
||||
// func SetLoggerWithOpts(adapter string, config []string, formatterFunc func(*LogMsg) string) error {
|
||||
func SetLoggerWithOpts(adapter string, config []string, opts common.SimpleKV) error {
|
||||
err := beeLogger.SetLoggerWithOpts(adapter, opts, config...)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -16,6 +16,8 @@ package logs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/astaxie/beego/pkg/common"
|
||||
)
|
||||
|
||||
// A filesLogWriter manages several fileLogWriter
|
||||
@ -46,16 +48,16 @@ var levelNames = [...]string{"emergency", "alert", "critical", "error", "warning
|
||||
// "separate":["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"],
|
||||
// }
|
||||
|
||||
func (f *multiFileLogWriter) Init(config string, LogFormatter ...func(*LogMsg) string) error {
|
||||
for _, elem := range LogFormatter {
|
||||
if elem != nil {
|
||||
f.UseCustomFormatter = true
|
||||
f.CustomFormatter = elem
|
||||
}
|
||||
}
|
||||
func (f *multiFileLogWriter) Init(jsonConfig string, opts ...common.SimpleKV) error {
|
||||
// for _, elem := range LogFormatter {
|
||||
// if elem != nil {
|
||||
// f.UseCustomFormatter = true
|
||||
// f.CustomFormatter = elem
|
||||
// }
|
||||
// }
|
||||
|
||||
writer := newFileWriter().(*fileLogWriter)
|
||||
err := writer.Init(config)
|
||||
err := writer.Init(jsonConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -63,10 +65,10 @@ func (f *multiFileLogWriter) Init(config string, LogFormatter ...func(*LogMsg) s
|
||||
f.writers[LevelDebug+1] = writer
|
||||
|
||||
//unmarshal "separate" field to f.Separate
|
||||
json.Unmarshal([]byte(config), f)
|
||||
json.Unmarshal([]byte(jsonConfig), f)
|
||||
|
||||
jsonMap := map[string]interface{}{}
|
||||
json.Unmarshal([]byte(config), &jsonMap)
|
||||
json.Unmarshal([]byte(jsonConfig), &jsonMap)
|
||||
|
||||
for i := LevelEmergency; i < LevelDebug+1; i++ {
|
||||
for _, v := range f.Separate {
|
||||
|
@ -5,6 +5,8 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/astaxie/beego/pkg/common"
|
||||
)
|
||||
|
||||
// SLACKWriter implements beego LoggerInterface and is used to send jiaoliao webhook
|
||||
@ -25,15 +27,14 @@ func (s *SLACKWriter) Format(lm *LogMsg) string {
|
||||
}
|
||||
|
||||
// Init SLACKWriter with json config string
|
||||
func (s *SLACKWriter) Init(jsonconfig string, LogFormatter ...func(*LogMsg) string) error {
|
||||
for _, elem := range LogFormatter {
|
||||
if elem != nil {
|
||||
s.UseCustomFormatter = true
|
||||
s.CustomFormatter = elem
|
||||
}
|
||||
}
|
||||
func (s *SLACKWriter) Init(jsonConfig string, opts ...common.SimpleKV) error {
|
||||
// if elem != nil {
|
||||
// s.UseCustomFormatter = true
|
||||
// s.CustomFormatter = elem
|
||||
// }
|
||||
// }
|
||||
|
||||
return json.Unmarshal([]byte(jsonconfig), s)
|
||||
return json.Unmarshal([]byte(jsonConfig), s)
|
||||
}
|
||||
|
||||
// WriteMsg write message in smtp writer.
|
||||
|
@ -21,6 +21,8 @@ import (
|
||||
"net"
|
||||
"net/smtp"
|
||||
"strings"
|
||||
|
||||
"github.com/astaxie/beego/pkg/common"
|
||||
)
|
||||
|
||||
// SMTPWriter implements LoggerInterface and is used to send emails via given SMTP-server.
|
||||
@ -52,15 +54,15 @@ func newSMTPWriter() Logger {
|
||||
// "sendTos":["email1","email2"],
|
||||
// "level":LevelError
|
||||
// }
|
||||
func (s *SMTPWriter) Init(jsonconfig string, LogFormatter ...func(*LogMsg) string) error {
|
||||
for _, elem := range LogFormatter {
|
||||
if elem != nil {
|
||||
s.UseCustomFormatter = true
|
||||
s.CustomFormatter = elem
|
||||
}
|
||||
}
|
||||
func (s *SMTPWriter) Init(jsonConfig string, opts ...common.SimpleKV) error {
|
||||
// for _, elem := range LogFormatter {
|
||||
// if elem != nil {
|
||||
// s.UseCustomFormatter = true
|
||||
// s.CustomFormatter = elem
|
||||
// }
|
||||
// }
|
||||
|
||||
return json.Unmarshal([]byte(jsonconfig), s)
|
||||
return json.Unmarshal([]byte(jsonConfig), s)
|
||||
}
|
||||
|
||||
func (s *SMTPWriter) getSMTPAuth(host string) smtp.Auth {
|
||||
|
Loading…
Reference in New Issue
Block a user