mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 14:20:54 +00:00
code simplify for package logs
This commit is contained in:
parent
29d4823866
commit
77c1109134
11
logs/conn.go
11
logs/conn.go
@ -43,11 +43,7 @@ func NewConn() LoggerInterface {
|
|||||||
// init connection writer with json config.
|
// init connection writer with json config.
|
||||||
// json config only need key "level".
|
// json config only need key "level".
|
||||||
func (c *ConnWriter) Init(jsonconfig string) error {
|
func (c *ConnWriter) Init(jsonconfig string) error {
|
||||||
err := json.Unmarshal([]byte(jsonconfig), c)
|
return json.Unmarshal([]byte(jsonconfig), c)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// write message in connection.
|
// write message in connection.
|
||||||
@ -77,10 +73,9 @@ func (c *ConnWriter) Flush() {
|
|||||||
|
|
||||||
// destroy connection writer and close tcp listener.
|
// destroy connection writer and close tcp listener.
|
||||||
func (c *ConnWriter) Destroy() {
|
func (c *ConnWriter) Destroy() {
|
||||||
if c.innerWriter == nil {
|
if c.innerWriter != nil {
|
||||||
return
|
c.innerWriter.Close()
|
||||||
}
|
}
|
||||||
c.innerWriter.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConnWriter) connect() error {
|
func (c *ConnWriter) connect() error {
|
||||||
|
@ -50,9 +50,10 @@ type ConsoleWriter struct {
|
|||||||
|
|
||||||
// create ConsoleWriter returning as LoggerInterface.
|
// create ConsoleWriter returning as LoggerInterface.
|
||||||
func NewConsole() LoggerInterface {
|
func NewConsole() LoggerInterface {
|
||||||
cw := new(ConsoleWriter)
|
cw := &ConsoleWriter{
|
||||||
cw.lg = log.New(os.Stdout, "", log.Ldate|log.Ltime)
|
lg: log.New(os.Stdout, "", log.Ldate|log.Ltime),
|
||||||
cw.Level = LevelDebug
|
Level: LevelDebug,
|
||||||
|
}
|
||||||
return cw
|
return cw
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,11 +63,7 @@ func (c *ConsoleWriter) Init(jsonconfig string) error {
|
|||||||
if len(jsonconfig) == 0 {
|
if len(jsonconfig) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
err := json.Unmarshal([]byte(jsonconfig), c)
|
return json.Unmarshal([]byte(jsonconfig), c)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// write message in console.
|
// write message in console.
|
||||||
@ -76,9 +73,10 @@ func (c *ConsoleWriter) WriteMsg(msg string, level int) error {
|
|||||||
}
|
}
|
||||||
if goos := runtime.GOOS; goos == "windows" {
|
if goos := runtime.GOOS; goos == "windows" {
|
||||||
c.lg.Println(msg)
|
c.lg.Println(msg)
|
||||||
} else {
|
return nil
|
||||||
c.lg.Println(colors[level](msg))
|
|
||||||
}
|
}
|
||||||
|
c.lg.Println(colors[level](msg))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,11 +123,7 @@ func (w *FileLogWriter) startLogger() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
w.mw.SetFd(fd)
|
w.mw.SetFd(fd)
|
||||||
err = w.initFd()
|
return w.initFd()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *FileLogWriter) docheck(size int) {
|
func (w *FileLogWriter) docheck(size int) {
|
||||||
@ -170,14 +166,13 @@ func (w *FileLogWriter) initFd() error {
|
|||||||
}
|
}
|
||||||
w.maxsize_cursize = int(finfo.Size())
|
w.maxsize_cursize = int(finfo.Size())
|
||||||
w.daily_opendate = time.Now().Day()
|
w.daily_opendate = time.Now().Day()
|
||||||
|
w.maxlines_curlines = 0
|
||||||
if finfo.Size() > 0 {
|
if finfo.Size() > 0 {
|
||||||
count, err := w.lines()
|
count, err := w.lines()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
w.maxlines_curlines = count
|
w.maxlines_curlines = count
|
||||||
} else {
|
|
||||||
w.maxlines_curlines = 0
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -292,9 +292,9 @@ func (bl *BeeLogger) Close() {
|
|||||||
fmt.Println("ERROR, unable to WriteMsg (while closing logger):", err)
|
fmt.Println("ERROR, unable to WriteMsg (while closing logger):", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
continue
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
for _, l := range bl.outputs {
|
for _, l := range bl.outputs {
|
||||||
l.Flush()
|
l.Flush()
|
||||||
|
@ -25,7 +25,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
subjectPhrase = "Diagnostic message from server"
|
// no usage
|
||||||
|
// subjectPhrase = "Diagnostic message from server"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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.
|
||||||
@ -146,9 +147,7 @@ func (s *SmtpWriter) WriteMsg(msg string, level int) error {
|
|||||||
mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.FromAddress + "<" + s.FromAddress +
|
mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.FromAddress + "<" + s.FromAddress +
|
||||||
">\r\nSubject: " + s.Subject + "\r\n" + content_type + "\r\n\r\n" + fmt.Sprintf(".%s", time.Now().Format("2006-01-02 15:04:05")) + msg)
|
">\r\nSubject: " + s.Subject + "\r\n" + content_type + "\r\n\r\n" + fmt.Sprintf(".%s", time.Now().Format("2006-01-02 15:04:05")) + msg)
|
||||||
|
|
||||||
err := s.sendMail(s.Host, auth, s.FromAddress, s.RecipientAddresses, mailmsg)
|
return s.sendMail(s.Host, auth, s.FromAddress, s.RecipientAddresses, mailmsg)
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// implementing method. empty.
|
// implementing method. empty.
|
||||||
|
Loading…
Reference in New Issue
Block a user