mirror of https://github.com/gogits/gogs.git
Unknwon
11 years ago
16 changed files with 359 additions and 38 deletions
@ -0,0 +1,104 @@
|
||||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package log |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"io" |
||||
"log" |
||||
"net" |
||||
) |
||||
|
||||
// ConnWriter implements LoggerInterface.
|
||||
// it writes messages in keep-live tcp connection.
|
||||
type ConnWriter struct { |
||||
lg *log.Logger |
||||
innerWriter io.WriteCloser |
||||
ReconnectOnMsg bool `json:"reconnectOnMsg"` |
||||
Reconnect bool `json:"reconnect"` |
||||
Net string `json:"net"` |
||||
Addr string `json:"addr"` |
||||
Level int `json:"level"` |
||||
} |
||||
|
||||
// create new ConnWrite returning as LoggerInterface.
|
||||
func NewConn() LoggerInterface { |
||||
conn := new(ConnWriter) |
||||
conn.Level = TRACE |
||||
return conn |
||||
} |
||||
|
||||
// init connection writer with json config.
|
||||
// json config only need key "level".
|
||||
func (cw *ConnWriter) Init(jsonconfig string) error { |
||||
return json.Unmarshal([]byte(jsonconfig), cw) |
||||
} |
||||
|
||||
// write message in connection.
|
||||
// if connection is down, try to re-connect.
|
||||
func (cw *ConnWriter) WriteMsg(msg string, skip, level int) error { |
||||
if cw.Level > level { |
||||
return nil |
||||
} |
||||
if cw.neddedConnectOnMsg() { |
||||
if err := cw.connect(); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
|
||||
if cw.ReconnectOnMsg { |
||||
defer cw.innerWriter.Close() |
||||
} |
||||
cw.lg.Println(msg) |
||||
return nil |
||||
} |
||||
|
||||
func (_ *ConnWriter) Flush() { |
||||
} |
||||
|
||||
// destroy connection writer and close tcp listener.
|
||||
func (cw *ConnWriter) Destroy() { |
||||
if cw.innerWriter == nil { |
||||
return |
||||
} |
||||
cw.innerWriter.Close() |
||||
} |
||||
|
||||
func (cw *ConnWriter) connect() error { |
||||
if cw.innerWriter != nil { |
||||
cw.innerWriter.Close() |
||||
cw.innerWriter = nil |
||||
} |
||||
|
||||
conn, err := net.Dial(cw.Net, cw.Addr) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
if tcpConn, ok := conn.(*net.TCPConn); ok { |
||||
tcpConn.SetKeepAlive(true) |
||||
} |
||||
|
||||
cw.innerWriter = conn |
||||
cw.lg = log.New(conn, "", log.Ldate|log.Ltime) |
||||
return nil |
||||
} |
||||
|
||||
func (cw *ConnWriter) neddedConnectOnMsg() bool { |
||||
if cw.Reconnect { |
||||
cw.Reconnect = false |
||||
return true |
||||
} |
||||
|
||||
if cw.innerWriter == nil { |
||||
return true |
||||
} |
||||
|
||||
return cw.ReconnectOnMsg |
||||
} |
||||
|
||||
func init() { |
||||
Register("conn", NewConn) |
||||
} |
@ -0,0 +1,68 @@
|
||||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package log |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
|
||||
"github.com/go-xorm/xorm" |
||||
) |
||||
|
||||
type Log struct { |
||||
Id int64 |
||||
Level int |
||||
Msg string `xorm:"TEXT"` |
||||
} |
||||
|
||||
// DatabaseWriter implements LoggerInterface and is used to log into database.
|
||||
type DatabaseWriter struct { |
||||
Driver string `json:"driver"` |
||||
Conn string `json:"conn"` |
||||
Level int `json:"level"` |
||||
x *xorm.Engine |
||||
} |
||||
|
||||
func NewDatabase() LoggerInterface { |
||||
return &DatabaseWriter{Level: TRACE} |
||||
} |
||||
|
||||
// init database writer with json config.
|
||||
// config like:
|
||||
// {
|
||||
// "driver": "mysql"
|
||||
// "conn":"root:root@tcp(127.0.0.1:3306)/gogs?charset=utf8",
|
||||
// "level": 0
|
||||
// }
|
||||
// connection string is based on xorm.
|
||||
func (d *DatabaseWriter) Init(jsonconfig string) (err error) { |
||||
if err = json.Unmarshal([]byte(jsonconfig), d); err != nil { |
||||
return err |
||||
} |
||||
d.x, err = xorm.NewEngine(d.Driver, d.Conn) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
return d.x.Sync(new(Log)) |
||||
} |
||||
|
||||
// write message in database writer.
|
||||
func (d *DatabaseWriter) WriteMsg(msg string, skip, level int) error { |
||||
if level < d.Level { |
||||
return nil |
||||
} |
||||
|
||||
_, err := d.x.Insert(&Log{Level: level, Msg: msg}) |
||||
return err |
||||
} |
||||
|
||||
func (_ *DatabaseWriter) Flush() { |
||||
} |
||||
|
||||
func (_ *DatabaseWriter) Destroy() { |
||||
} |
||||
|
||||
func init() { |
||||
Register("database", NewDatabase) |
||||
} |
@ -0,0 +1,87 @@
|
||||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package log |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"fmt" |
||||
"net/smtp" |
||||
"strings" |
||||
"time" |
||||
) |
||||
|
||||
const ( |
||||
subjectPhrase = "Diagnostic message from server" |
||||
) |
||||
|
||||
// smtpWriter implements LoggerInterface and is used to send emails via given SMTP-server.
|
||||
type SmtpWriter struct { |
||||
Username string `json:"Username"` |
||||
Password string `json:"password"` |
||||
Host string `json:"Host"` |
||||
Subject string `json:"subject"` |
||||
RecipientAddresses []string `json:"sendTos"` |
||||
Level int `json:"level"` |
||||
} |
||||
|
||||
// create smtp writer.
|
||||
func NewSmtpWriter() LoggerInterface { |
||||
return &SmtpWriter{Level: TRACE} |
||||
} |
||||
|
||||
// init smtp writer with json config.
|
||||
// config like:
|
||||
// {
|
||||
// "Username":"example@gmail.com",
|
||||
// "password:"password",
|
||||
// "host":"smtp.gmail.com:465",
|
||||
// "subject":"email title",
|
||||
// "sendTos":["email1","email2"],
|
||||
// "level":LevelError
|
||||
// }
|
||||
func (sw *SmtpWriter) Init(jsonconfig string) error { |
||||
return json.Unmarshal([]byte(jsonconfig), sw) |
||||
} |
||||
|
||||
// write message in smtp writer.
|
||||
// it will send an email with subject and only this message.
|
||||
func (s *SmtpWriter) WriteMsg(msg string, skip, level int) error { |
||||
if level < s.Level { |
||||
return nil |
||||
} |
||||
|
||||
hp := strings.Split(s.Host, ":") |
||||
|
||||
// Set up authentication information.
|
||||
auth := smtp.PlainAuth( |
||||
"", |
||||
s.Username, |
||||
s.Password, |
||||
hp[0], |
||||
) |
||||
// Connect to the server, authenticate, set the sender and recipient,
|
||||
// and send the email all in one step.
|
||||
content_type := "Content-Type: text/plain" + "; charset=UTF-8" |
||||
mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.Username + "<" + s.Username + |
||||
">\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) |
||||
|
||||
return smtp.SendMail( |
||||
s.Host, |
||||
auth, |
||||
s.Username, |
||||
s.RecipientAddresses, |
||||
mailmsg, |
||||
) |
||||
} |
||||
|
||||
func (_ *SmtpWriter) Flush() { |
||||
} |
||||
|
||||
func (_ *SmtpWriter) Destroy() { |
||||
} |
||||
|
||||
func init() { |
||||
Register("smtp", NewSmtpWriter) |
||||
} |
@ -1,12 +0,0 @@
|
||||
<li {{if .IsPrivate}}class="private"{{end}}> |
||||
<a href="{{.Owner.Name}}/{{.Name}}"> |
||||
<i class="octicon octicon-{{if .IsPrivate}}lock{{else if .IsFork}}repo-forked{{else if .IsMirror}}repo-clone{{else}}repo{{end}}"></i> |
||||
<span class="repo-name"> |
||||
<!-- <span class="repo-name-prefix">gogits / </span> --> |
||||
<strong class="repo">{{.Name}}</strong> |
||||
</span> |
||||
<span class="right repo-star"> |
||||
<i class="octicon octicon-star"></i>{{.NumStars}} |
||||
</span> |
||||
</a> |
||||
</li> |
Loading…
Reference in new issue