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