Merge branch 'master' of https://github.com/astaxie/beego
Showing
6 changed files
with
71 additions
and
140 deletions
| ... | @@ -14,7 +14,7 @@ import ( | ... | @@ -14,7 +14,7 @@ import ( |
| 14 | "fmt" | 14 | "fmt" |
| 15 | "io" | 15 | "io" |
| 16 | "os" | 16 | "os" |
| 17 | "path" | 17 | "path/filepath" |
| 18 | "reflect" | 18 | "reflect" |
| 19 | "strconv" | 19 | "strconv" |
| 20 | "time" | 20 | "time" |
| ... | @@ -79,8 +79,8 @@ func (this *FileCache) StartAndGC(config string) error { | ... | @@ -79,8 +79,8 @@ func (this *FileCache) StartAndGC(config string) error { |
| 79 | } | 79 | } |
| 80 | 80 | ||
| 81 | func (this *FileCache) Init() { | 81 | func (this *FileCache) Init() { |
| 82 | app := path.Dir(os.Args[0]) | 82 | app := filepath.Dir(os.Args[0]) |
| 83 | this.CachePath = path.Join(app, this.CachePath) | 83 | this.CachePath = filepath.Join(app, this.CachePath) |
| 84 | ok, err := exists(this.CachePath) | 84 | ok, err := exists(this.CachePath) |
| 85 | if err != nil { // print error | 85 | if err != nil { // print error |
| 86 | //fmt.Println(err) | 86 | //fmt.Println(err) |
| ... | @@ -102,9 +102,9 @@ func (this *FileCache) getCacheFileName(key string) string { | ... | @@ -102,9 +102,9 @@ func (this *FileCache) getCacheFileName(key string) string { |
| 102 | //fmt.Println("md5" , keyMd5); | 102 | //fmt.Println("md5" , keyMd5); |
| 103 | switch this.DirectoryLevel { | 103 | switch this.DirectoryLevel { |
| 104 | case 2: | 104 | case 2: |
| 105 | cachePath = path.Join(cachePath, keyMd5[0:2], keyMd5[2:4]) | 105 | cachePath = filepath.Join(cachePath, keyMd5[0:2], keyMd5[2:4]) |
| 106 | case 1: | 106 | case 1: |
| 107 | cachePath = path.Join(cachePath, keyMd5[0:2]) | 107 | cachePath = filepath.Join(cachePath, keyMd5[0:2]) |
| 108 | } | 108 | } |
| 109 | 109 | ||
| 110 | ok, err := exists(cachePath) | 110 | ok, err := exists(cachePath) |
| ... | @@ -116,7 +116,7 @@ func (this *FileCache) getCacheFileName(key string) string { | ... | @@ -116,7 +116,7 @@ func (this *FileCache) getCacheFileName(key string) string { |
| 116 | //fmt.Println(err); | 116 | //fmt.Println(err); |
| 117 | } | 117 | } |
| 118 | } | 118 | } |
| 119 | return path.Join(cachePath, fmt.Sprintf("%s%s", keyMd5, this.FileSuffix)) | 119 | return filepath.Join(cachePath, fmt.Sprintf("%s%s", keyMd5, this.FileSuffix)) |
| 120 | } | 120 | } |
| 121 | 121 | ||
| 122 | func (this *FileCache) Get(key string) interface{} { | 122 | func (this *FileCache) Get(key string) interface{} { | ... | ... |
| ... | @@ -90,7 +90,7 @@ func (b *BeegoHttpRequest) Header(key, value string) *BeegoHttpRequest { | ... | @@ -90,7 +90,7 @@ func (b *BeegoHttpRequest) Header(key, value string) *BeegoHttpRequest { |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | func (b *BeegoHttpRequest) SetCookie(cookie *http.Cookie) *BeegoHttpRequest { | 92 | func (b *BeegoHttpRequest) SetCookie(cookie *http.Cookie) *BeegoHttpRequest { |
| 93 | b.req.Header.Add("Set-Cookie", cookie.String()) | 93 | b.req.Header.Add("Cookie", cookie.String()) |
| 94 | return b | 94 | return b |
| 95 | } | 95 | } |
| 96 | 96 | ... | ... |
| ... | @@ -10,45 +10,29 @@ import ( | ... | @@ -10,45 +10,29 @@ import ( |
| 10 | type ConnWriter struct { | 10 | type ConnWriter struct { |
| 11 | lg *log.Logger | 11 | lg *log.Logger |
| 12 | innerWriter io.WriteCloser | 12 | innerWriter io.WriteCloser |
| 13 | reconnectOnMsg bool | 13 | ReconnectOnMsg bool `json:"reconnectOnMsg"` |
| 14 | reconnect bool | 14 | Reconnect bool `json:"reconnect"` |
| 15 | net string | 15 | Net string `json:"net"` |
| 16 | addr string | 16 | Addr string `json:"addr"` |
| 17 | level int | 17 | Level int `json:"level"` |
| 18 | } | 18 | } |
| 19 | 19 | ||
| 20 | func NewConn() LoggerInterface { | 20 | func NewConn() LoggerInterface { |
| 21 | conn := new(ConnWriter) | 21 | conn := new(ConnWriter) |
| 22 | conn.level = LevelTrace | 22 | conn.Level = LevelTrace |
| 23 | return conn | 23 | return conn |
| 24 | } | 24 | } |
| 25 | 25 | ||
| 26 | func (c *ConnWriter) Init(jsonconfig string) error { | 26 | func (c *ConnWriter) Init(jsonconfig string) error { |
| 27 | var m map[string]interface{} | 27 | err := json.Unmarshal([]byte(jsonconfig), c) |
| 28 | err := json.Unmarshal([]byte(jsonconfig), &m) | ||
| 29 | if err != nil { | 28 | if err != nil { |
| 30 | return err | 29 | return err |
| 31 | } | 30 | } |
| 32 | if rom, ok := m["reconnectOnMsg"]; ok { | ||
| 33 | c.reconnectOnMsg = rom.(bool) | ||
| 34 | } | ||
| 35 | if rc, ok := m["reconnect"]; ok { | ||
| 36 | c.reconnect = rc.(bool) | ||
| 37 | } | ||
| 38 | if nt, ok := m["net"]; ok { | ||
| 39 | c.net = nt.(string) | ||
| 40 | } | ||
| 41 | if addr, ok := m["addr"]; ok { | ||
| 42 | c.addr = addr.(string) | ||
| 43 | } | ||
| 44 | if lv, ok := m["level"]; ok { | ||
| 45 | c.level = int(lv.(float64)) | ||
| 46 | } | ||
| 47 | return nil | 31 | return nil |
| 48 | } | 32 | } |
| 49 | 33 | ||
| 50 | func (c *ConnWriter) WriteMsg(msg string, level int) error { | 34 | func (c *ConnWriter) WriteMsg(msg string, level int) error { |
| 51 | if level < c.level { | 35 | if level < c.Level { |
| 52 | return nil | 36 | return nil |
| 53 | } | 37 | } |
| 54 | if c.neddedConnectOnMsg() { | 38 | if c.neddedConnectOnMsg() { |
| ... | @@ -58,7 +42,7 @@ func (c *ConnWriter) WriteMsg(msg string, level int) error { | ... | @@ -58,7 +42,7 @@ func (c *ConnWriter) WriteMsg(msg string, level int) error { |
| 58 | } | 42 | } |
| 59 | } | 43 | } |
| 60 | 44 | ||
| 61 | if c.reconnectOnMsg { | 45 | if c.ReconnectOnMsg { |
| 62 | defer c.innerWriter.Close() | 46 | defer c.innerWriter.Close() |
| 63 | } | 47 | } |
| 64 | c.lg.Println(msg) | 48 | c.lg.Println(msg) |
| ... | @@ -82,7 +66,7 @@ func (c *ConnWriter) connect() error { | ... | @@ -82,7 +66,7 @@ func (c *ConnWriter) connect() error { |
| 82 | c.innerWriter = nil | 66 | c.innerWriter = nil |
| 83 | } | 67 | } |
| 84 | 68 | ||
| 85 | conn, err := net.Dial(c.net, c.addr) | 69 | conn, err := net.Dial(c.Net, c.Addr) |
| 86 | if err != nil { | 70 | if err != nil { |
| 87 | return err | 71 | return err |
| 88 | } | 72 | } |
| ... | @@ -97,8 +81,8 @@ func (c *ConnWriter) connect() error { | ... | @@ -97,8 +81,8 @@ func (c *ConnWriter) connect() error { |
| 97 | } | 81 | } |
| 98 | 82 | ||
| 99 | func (c *ConnWriter) neddedConnectOnMsg() bool { | 83 | func (c *ConnWriter) neddedConnectOnMsg() bool { |
| 100 | if c.reconnect { | 84 | if c.Reconnect { |
| 101 | c.reconnect = false | 85 | c.Reconnect = false |
| 102 | return true | 86 | return true |
| 103 | } | 87 | } |
| 104 | 88 | ||
| ... | @@ -106,7 +90,7 @@ func (c *ConnWriter) neddedConnectOnMsg() bool { | ... | @@ -106,7 +90,7 @@ func (c *ConnWriter) neddedConnectOnMsg() bool { |
| 106 | return true | 90 | return true |
| 107 | } | 91 | } |
| 108 | 92 | ||
| 109 | return c.reconnectOnMsg | 93 | return c.ReconnectOnMsg |
| 110 | } | 94 | } |
| 111 | 95 | ||
| 112 | func init() { | 96 | func init() { | ... | ... |
| ... | @@ -8,30 +8,26 @@ import ( | ... | @@ -8,30 +8,26 @@ import ( |
| 8 | 8 | ||
| 9 | type ConsoleWriter struct { | 9 | type ConsoleWriter struct { |
| 10 | lg *log.Logger | 10 | lg *log.Logger |
| 11 | level int | 11 | Level int `json:"level"` |
| 12 | } | 12 | } |
| 13 | 13 | ||
| 14 | func NewConsole() LoggerInterface { | 14 | func NewConsole() LoggerInterface { |
| 15 | cw := new(ConsoleWriter) | 15 | cw := new(ConsoleWriter) |
| 16 | cw.lg = log.New(os.Stdout, "", log.Ldate|log.Ltime) | 16 | cw.lg = log.New(os.Stdout, "", log.Ldate|log.Ltime) |
| 17 | cw.level = LevelTrace | 17 | cw.Level = LevelTrace |
| 18 | return cw | 18 | return cw |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | func (c *ConsoleWriter) Init(jsonconfig string) error { | 21 | func (c *ConsoleWriter) Init(jsonconfig string) error { |
| 22 | var m map[string]interface{} | 22 | err := json.Unmarshal([]byte(jsonconfig), c) |
| 23 | err := json.Unmarshal([]byte(jsonconfig), &m) | ||
| 24 | if err != nil { | 23 | if err != nil { |
| 25 | return err | 24 | return err |
| 26 | } | 25 | } |
| 27 | if lv, ok := m["level"]; ok { | ||
| 28 | c.level = int(lv.(float64)) | ||
| 29 | } | ||
| 30 | return nil | 26 | return nil |
| 31 | } | 27 | } |
| 32 | 28 | ||
| 33 | func (c *ConsoleWriter) WriteMsg(msg string, level int) error { | 29 | func (c *ConsoleWriter) WriteMsg(msg string, level int) error { |
| 34 | if level < c.level { | 30 | if level < c.Level { |
| 35 | return nil | 31 | return nil |
| 36 | } | 32 | } |
| 37 | c.lg.Println(msg) | 33 | c.lg.Println(msg) | ... | ... |
| ... | @@ -7,7 +7,6 @@ import ( | ... | @@ -7,7 +7,6 @@ import ( |
| 7 | "io/ioutil" | 7 | "io/ioutil" |
| 8 | "log" | 8 | "log" |
| 9 | "os" | 9 | "os" |
| 10 | "path" | ||
| 11 | "path/filepath" | 10 | "path/filepath" |
| 12 | "strings" | 11 | "strings" |
| 13 | "sync" | 12 | "sync" |
| ... | @@ -18,25 +17,25 @@ type FileLogWriter struct { | ... | @@ -18,25 +17,25 @@ type FileLogWriter struct { |
| 18 | *log.Logger | 17 | *log.Logger |
| 19 | mw *MuxWriter | 18 | mw *MuxWriter |
| 20 | // The opened file | 19 | // The opened file |
| 21 | filename string | 20 | Filename string `json:"filename"` |
| 22 | 21 | ||
| 23 | maxlines int | 22 | Maxlines int `json:"maxlines"` |
| 24 | maxlines_curlines int | 23 | maxlines_curlines int |
| 25 | 24 | ||
| 26 | // Rotate at size | 25 | // Rotate at size |
| 27 | maxsize int | 26 | Maxsize int `json:"maxsize"` |
| 28 | maxsize_cursize int | 27 | maxsize_cursize int |
| 29 | 28 | ||
| 30 | // Rotate daily | 29 | // Rotate daily |
| 31 | daily bool | 30 | Daily bool `json:"daily"` |
| 32 | maxdays int64 | 31 | Maxdays int64 `json:"maxdays` |
| 33 | daily_opendate int | 32 | daily_opendate int |
| 34 | 33 | ||
| 35 | rotate bool | 34 | Rotate bool `json:"rotate"` |
| 36 | 35 | ||
| 37 | startLock sync.Mutex // Only one log can write to the file | 36 | startLock sync.Mutex // Only one log can write to the file |
| 38 | 37 | ||
| 39 | level int | 38 | Level int `json:"level"` |
| 40 | } | 39 | } |
| 41 | 40 | ||
| 42 | type MuxWriter struct { | 41 | type MuxWriter struct { |
| ... | @@ -59,13 +58,13 @@ func (l *MuxWriter) SetFd(fd *os.File) { | ... | @@ -59,13 +58,13 @@ func (l *MuxWriter) SetFd(fd *os.File) { |
| 59 | 58 | ||
| 60 | func NewFileWriter() LoggerInterface { | 59 | func NewFileWriter() LoggerInterface { |
| 61 | w := &FileLogWriter{ | 60 | w := &FileLogWriter{ |
| 62 | filename: "", | 61 | Filename: "", |
| 63 | maxlines: 1000000, | 62 | Maxlines: 1000000, |
| 64 | maxsize: 1 << 28, //256 MB | 63 | Maxsize: 1 << 28, //256 MB |
| 65 | daily: true, | 64 | Daily: true, |
| 66 | maxdays: 7, | 65 | Maxdays: 7, |
| 67 | rotate: true, | 66 | Rotate: true, |
| 68 | level: LevelTrace, | 67 | Level: LevelTrace, |
| 69 | } | 68 | } |
| 70 | // use MuxWriter instead direct use os.File for lock write when rotate | 69 | // use MuxWriter instead direct use os.File for lock write when rotate |
| 71 | w.mw = new(MuxWriter) | 70 | w.mw = new(MuxWriter) |
| ... | @@ -84,33 +83,12 @@ func NewFileWriter() LoggerInterface { | ... | @@ -84,33 +83,12 @@ func NewFileWriter() LoggerInterface { |
| 84 | // "rotate":true | 83 | // "rotate":true |
| 85 | //} | 84 | //} |
| 86 | func (w *FileLogWriter) Init(jsonconfig string) error { | 85 | func (w *FileLogWriter) Init(jsonconfig string) error { |
| 87 | var m map[string]interface{} | 86 | err := json.Unmarshal([]byte(jsonconfig), w) |
| 88 | err := json.Unmarshal([]byte(jsonconfig), &m) | ||
| 89 | if err != nil { | 87 | if err != nil { |
| 90 | return err | 88 | return err |
| 91 | } | 89 | } |
| 92 | if fn, ok := m["filename"]; !ok { | 90 | if len(w.Filename) == 0 { |
| 93 | return errors.New("jsonconfig must have filename") | 91 | return errors.New("jsonconfig must have filename") |
| 94 | } else { | ||
| 95 | w.filename = fn.(string) | ||
| 96 | } | ||
| 97 | if ml, ok := m["maxlines"]; ok { | ||
| 98 | w.maxlines = int(ml.(float64)) | ||
| 99 | } | ||
| 100 | if ms, ok := m["maxsize"]; ok { | ||
| 101 | w.maxsize = int(ms.(float64)) | ||
| 102 | } | ||
| 103 | if dl, ok := m["daily"]; ok { | ||
| 104 | w.daily = dl.(bool) | ||
| 105 | } | ||
| 106 | if md, ok := m["maxdays"]; ok { | ||
| 107 | w.maxdays = int64(md.(float64)) | ||
| 108 | } | ||
| 109 | if rt, ok := m["rotate"]; ok { | ||
| 110 | w.rotate = rt.(bool) | ||
| 111 | } | ||
| 112 | if lv, ok := m["level"]; ok { | ||
| 113 | w.level = int(lv.(float64)) | ||
| 114 | } | 92 | } |
| 115 | err = w.StartLogger() | 93 | err = w.StartLogger() |
| 116 | return err | 94 | return err |
| ... | @@ -132,11 +110,11 @@ func (w *FileLogWriter) StartLogger() error { | ... | @@ -132,11 +110,11 @@ func (w *FileLogWriter) StartLogger() error { |
| 132 | func (w *FileLogWriter) docheck(size int) { | 110 | func (w *FileLogWriter) docheck(size int) { |
| 133 | w.startLock.Lock() | 111 | w.startLock.Lock() |
| 134 | defer w.startLock.Unlock() | 112 | defer w.startLock.Unlock() |
| 135 | if (w.maxlines > 0 && w.maxlines_curlines >= w.maxlines) || | 113 | if (w.Maxlines > 0 && w.maxlines_curlines >= w.Maxlines) || |
| 136 | (w.maxsize > 0 && w.maxsize_cursize >= w.maxsize) || | 114 | (w.Maxsize > 0 && w.maxsize_cursize >= w.Maxsize) || |
| 137 | (w.daily && time.Now().Day() != w.daily_opendate) { | 115 | (w.Daily && time.Now().Day() != w.daily_opendate) { |
| 138 | if err := w.DoRotate(); err != nil { | 116 | if err := w.DoRotate(); err != nil { |
| 139 | fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.filename, err) | 117 | fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err) |
| 140 | return | 118 | return |
| 141 | } | 119 | } |
| 142 | } | 120 | } |
| ... | @@ -145,7 +123,7 @@ func (w *FileLogWriter) docheck(size int) { | ... | @@ -145,7 +123,7 @@ func (w *FileLogWriter) docheck(size int) { |
| 145 | } | 123 | } |
| 146 | 124 | ||
| 147 | func (w *FileLogWriter) WriteMsg(msg string, level int) error { | 125 | func (w *FileLogWriter) WriteMsg(msg string, level int) error { |
| 148 | if level < w.level { | 126 | if level < w.Level { |
| 149 | return nil | 127 | return nil |
| 150 | } | 128 | } |
| 151 | n := 24 + len(msg) // 24 stand for the length "2013/06/23 21:00:22 [T] " | 129 | n := 24 + len(msg) // 24 stand for the length "2013/06/23 21:00:22 [T] " |
| ... | @@ -156,7 +134,7 @@ func (w *FileLogWriter) WriteMsg(msg string, level int) error { | ... | @@ -156,7 +134,7 @@ func (w *FileLogWriter) WriteMsg(msg string, level int) error { |
| 156 | 134 | ||
| 157 | func (w *FileLogWriter) createLogFile() (*os.File, error) { | 135 | func (w *FileLogWriter) createLogFile() (*os.File, error) { |
| 158 | // Open the log file | 136 | // Open the log file |
| 159 | fd, err := os.OpenFile(w.filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660) | 137 | fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660) |
| 160 | return fd, err | 138 | return fd, err |
| 161 | } | 139 | } |
| 162 | 140 | ||
| ... | @@ -169,7 +147,7 @@ func (w *FileLogWriter) initFd() error { | ... | @@ -169,7 +147,7 @@ func (w *FileLogWriter) initFd() error { |
| 169 | w.maxsize_cursize = int(finfo.Size()) | 147 | w.maxsize_cursize = int(finfo.Size()) |
| 170 | w.daily_opendate = time.Now().Day() | 148 | w.daily_opendate = time.Now().Day() |
| 171 | if finfo.Size() > 0 { | 149 | if finfo.Size() > 0 { |
| 172 | content, err := ioutil.ReadFile(w.filename) | 150 | content, err := ioutil.ReadFile(w.Filename) |
| 173 | if err != nil { | 151 | if err != nil { |
| 174 | return err | 152 | return err |
| 175 | } | 153 | } |
| ... | @@ -181,18 +159,18 @@ func (w *FileLogWriter) initFd() error { | ... | @@ -181,18 +159,18 @@ func (w *FileLogWriter) initFd() error { |
| 181 | } | 159 | } |
| 182 | 160 | ||
| 183 | func (w *FileLogWriter) DoRotate() error { | 161 | func (w *FileLogWriter) DoRotate() error { |
| 184 | _, err := os.Lstat(w.filename) | 162 | _, err := os.Lstat(w.Filename) |
| 185 | if err == nil { // file exists | 163 | if err == nil { // file exists |
| 186 | // Find the next available number | 164 | // Find the next available number |
| 187 | num := 1 | 165 | num := 1 |
| 188 | fname := "" | 166 | fname := "" |
| 189 | for ; err == nil && num <= 999; num++ { | 167 | for ; err == nil && num <= 999; num++ { |
| 190 | fname = w.filename + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), num) | 168 | fname = w.Filename + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), num) |
| 191 | _, err = os.Lstat(fname) | 169 | _, err = os.Lstat(fname) |
| 192 | } | 170 | } |
| 193 | // return error if the last file checked still existed | 171 | // return error if the last file checked still existed |
| 194 | if err == nil { | 172 | if err == nil { |
| 195 | return fmt.Errorf("Rotate: Cannot find free log number to rename %s\n", w.filename) | 173 | return fmt.Errorf("Rotate: Cannot find free log number to rename %s\n", w.Filename) |
| 196 | } | 174 | } |
| 197 | 175 | ||
| 198 | // block Logger's io.Writer | 176 | // block Logger's io.Writer |
| ... | @@ -204,7 +182,7 @@ func (w *FileLogWriter) DoRotate() error { | ... | @@ -204,7 +182,7 @@ func (w *FileLogWriter) DoRotate() error { |
| 204 | 182 | ||
| 205 | // close fd before rename | 183 | // close fd before rename |
| 206 | // Rename the file to its newfound home | 184 | // Rename the file to its newfound home |
| 207 | err = os.Rename(w.filename, fname) | 185 | err = os.Rename(w.Filename, fname) |
| 208 | if err != nil { | 186 | if err != nil { |
| 209 | return fmt.Errorf("Rotate: %s\n", err) | 187 | return fmt.Errorf("Rotate: %s\n", err) |
| 210 | } | 188 | } |
| ... | @@ -222,10 +200,10 @@ func (w *FileLogWriter) DoRotate() error { | ... | @@ -222,10 +200,10 @@ func (w *FileLogWriter) DoRotate() error { |
| 222 | } | 200 | } |
| 223 | 201 | ||
| 224 | func (w *FileLogWriter) deleteOldLog() { | 202 | func (w *FileLogWriter) deleteOldLog() { |
| 225 | dir := path.Dir(w.filename) | 203 | dir := filepath.Dir(w.Filename) |
| 226 | filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { | 204 | filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { |
| 227 | if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*w.maxdays) { | 205 | if !info.IsDir() && info.ModTime().Unix() < (time.Now().Unix()-60*60*24*w.Maxdays) { |
| 228 | if strings.HasPrefix(filepath.Base(path), filepath.Base(w.filename)) { | 206 | if strings.HasPrefix(filepath.Base(path), filepath.Base(w.Filename)) { |
| 229 | os.Remove(path) | 207 | os.Remove(path) |
| 230 | } | 208 | } |
| 231 | } | 209 | } | ... | ... |
| ... | @@ -2,7 +2,6 @@ package logs | ... | @@ -2,7 +2,6 @@ package logs |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "encoding/json" | 4 | "encoding/json" |
| 5 | "errors" | ||
| 6 | "fmt" | 5 | "fmt" |
| 7 | "net/smtp" | 6 | "net/smtp" |
| 8 | "strings" | 7 | "strings" |
| ... | @@ -15,77 +14,51 @@ const ( | ... | @@ -15,77 +14,51 @@ const ( |
| 15 | 14 | ||
| 16 | // smtpWriter is used to send emails via given SMTP-server. | 15 | // smtpWriter is used to send emails via given SMTP-server. |
| 17 | type SmtpWriter struct { | 16 | type SmtpWriter struct { |
| 18 | username string | 17 | Username string `json:"Username"` |
| 19 | password string | 18 | Password string `json:"password"` |
| 20 | host string | 19 | Host string `json:"Host"` |
| 21 | subject string | 20 | Subject string `json:"subject"` |
| 22 | recipientAddresses []string | 21 | RecipientAddresses []string `json:"sendTos"` |
| 23 | level int | 22 | Level int `json:"level"` |
| 24 | } | 23 | } |
| 25 | 24 | ||
| 26 | func NewSmtpWriter() LoggerInterface { | 25 | func NewSmtpWriter() LoggerInterface { |
| 27 | return &SmtpWriter{level: LevelTrace} | 26 | return &SmtpWriter{Level: LevelTrace} |
| 28 | } | 27 | } |
| 29 | 28 | ||
| 30 | func (s *SmtpWriter) Init(jsonconfig string) error { | 29 | func (s *SmtpWriter) Init(jsonconfig string) error { |
| 31 | var m map[string]interface{} | 30 | err := json.Unmarshal([]byte(jsonconfig), s) |
| 32 | err := json.Unmarshal([]byte(jsonconfig), &m) | ||
| 33 | if err != nil { | 31 | if err != nil { |
| 34 | return err | 32 | return err |
| 35 | } | 33 | } |
| 36 | if username, ok := m["username"]; !ok { | ||
| 37 | return errors.New("smtp config must have auth username") | ||
| 38 | } else if password, ok := m["password"]; !ok { | ||
| 39 | return errors.New("smtp config must have auth password") | ||
| 40 | } else if hostname, ok := m["host"]; !ok { | ||
| 41 | return errors.New("smtp config must have host like 'mail.example.com:25'") | ||
| 42 | } else if sendTos, ok := m["sendTos"]; !ok { | ||
| 43 | return errors.New("smtp config must have sendTos") | ||
| 44 | } else { | ||
| 45 | s.username = username.(string) | ||
| 46 | s.password = password.(string) | ||
| 47 | s.host = hostname.(string) | ||
| 48 | for _, v := range sendTos.([]interface{}) { | ||
| 49 | s.recipientAddresses = append(s.recipientAddresses, v.(string)) | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | if subject, ok := m["subject"]; ok { | ||
| 54 | s.subject = subject.(string) | ||
| 55 | } else { | ||
| 56 | s.subject = subjectPhrase | ||
| 57 | } | ||
| 58 | if lv, ok := m["level"]; ok { | ||
| 59 | s.level = int(lv.(float64)) | ||
| 60 | } | ||
| 61 | return nil | 34 | return nil |
| 62 | } | 35 | } |
| 63 | 36 | ||
| 64 | func (s *SmtpWriter) WriteMsg(msg string, level int) error { | 37 | func (s *SmtpWriter) WriteMsg(msg string, level int) error { |
| 65 | if level < s.level { | 38 | if level < s.Level { |
| 66 | return nil | 39 | return nil |
| 67 | } | 40 | } |
| 68 | 41 | ||
| 69 | hp := strings.Split(s.host, ":") | 42 | hp := strings.Split(s.Host, ":") |
| 70 | 43 | ||
| 71 | // Set up authentication information. | 44 | // Set up authentication information. |
| 72 | auth := smtp.PlainAuth( | 45 | auth := smtp.PlainAuth( |
| 73 | "", | 46 | "", |
| 74 | s.username, | 47 | s.Username, |
| 75 | s.password, | 48 | s.Password, |
| 76 | hp[0], | 49 | hp[0], |
| 77 | ) | 50 | ) |
| 78 | // Connect to the server, authenticate, set the sender and recipient, | 51 | // Connect to the server, authenticate, set the sender and recipient, |
| 79 | // and send the email all in one step. | 52 | // and send the email all in one step. |
| 80 | content_type := "Content-Type: text/plain" + "; charset=UTF-8" | 53 | content_type := "Content-Type: text/plain" + "; charset=UTF-8" |
| 81 | mailmsg := []byte("To: " + strings.Join(s.recipientAddresses, ";") + "\r\nFrom: " + s.username + "<" + s.username + | 54 | mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.Username + "<" + s.Username + |
| 82 | ">\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) | 55 | ">\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) |
| 83 | 56 | ||
| 84 | err := smtp.SendMail( | 57 | err := smtp.SendMail( |
| 85 | s.host, | 58 | s.Host, |
| 86 | auth, | 59 | auth, |
| 87 | s.username, | 60 | s.Username, |
| 88 | s.recipientAddresses, | 61 | s.RecipientAddresses, |
| 89 | mailmsg, | 62 | mailmsg, |
| 90 | ) | 63 | ) |
| 91 | 64 | ... | ... |
-
Please register or sign in to post a comment