dcaff38c by 1fei

Update file.go

1 parent b68c814c
...@@ -18,25 +18,25 @@ type FileLogWriter struct { ...@@ -18,25 +18,25 @@ type FileLogWriter struct {
18 *log.Logger 18 *log.Logger
19 mw *MuxWriter 19 mw *MuxWriter
20 // The opened file 20 // The opened file
21 filename string 21 Filename string `json:"filename"`
22 22
23 maxlines int 23 Maxlines int `json:"maxlines"`
24 maxlines_curlines int 24 maxlines_curlines int
25 25
26 // Rotate at size 26 // Rotate at size
27 maxsize int 27 Maxsize int `json:"maxsize"`
28 maxsize_cursize int 28 maxsize_cursize int
29 29
30 // Rotate daily 30 // Rotate daily
31 daily bool 31 Daily bool `json:"daily"`
32 maxdays int64 32 Maxdays int64 `json:"maxdays`
33 daily_opendate int 33 daily_opendate int
34 34
35 rotate bool 35 Rotate bool `json:"rotate"`
36 36
37 startLock sync.Mutex // Only one log can write to the file 37 startLock sync.Mutex // Only one log can write to the file
38 38
39 level int 39 Level int `json:"level"`
40 } 40 }
41 41
42 type MuxWriter struct { 42 type MuxWriter struct {
...@@ -59,13 +59,13 @@ func (l *MuxWriter) SetFd(fd *os.File) { ...@@ -59,13 +59,13 @@ func (l *MuxWriter) SetFd(fd *os.File) {
59 59
60 func NewFileWriter() LoggerInterface { 60 func NewFileWriter() LoggerInterface {
61 w := &FileLogWriter{ 61 w := &FileLogWriter{
62 filename: "", 62 Filename: "",
63 maxlines: 1000000, 63 Maxlines: 1000000,
64 maxsize: 1 << 28, //256 MB 64 Maxsize: 1 << 28, //256 MB
65 daily: true, 65 Daily: true,
66 maxdays: 7, 66 Maxdays: 7,
67 rotate: true, 67 Rotate: true,
68 level: LevelTrace, 68 Level: LevelTrace,
69 } 69 }
70 // use MuxWriter instead direct use os.File for lock write when rotate 70 // use MuxWriter instead direct use os.File for lock write when rotate
71 w.mw = new(MuxWriter) 71 w.mw = new(MuxWriter)
...@@ -84,33 +84,12 @@ func NewFileWriter() LoggerInterface { ...@@ -84,33 +84,12 @@ func NewFileWriter() LoggerInterface {
84 // "rotate":true 84 // "rotate":true
85 //} 85 //}
86 func (w *FileLogWriter) Init(jsonconfig string) error { 86 func (w *FileLogWriter) Init(jsonconfig string) error {
87 var m map[string]interface{} 87 err := json.Unmarshal([]byte(jsonconfig), w)
88 err := json.Unmarshal([]byte(jsonconfig), &m)
89 if err != nil { 88 if err != nil {
90 return err 89 return err
91 } 90 }
92 if fn, ok := m["filename"]; !ok { 91 if len(w.Filename) == 0 {
93 return errors.New("jsonconfig must have filename") 92 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 } 93 }
115 err = w.StartLogger() 94 err = w.StartLogger()
116 return err 95 return err
...@@ -132,11 +111,11 @@ func (w *FileLogWriter) StartLogger() error { ...@@ -132,11 +111,11 @@ func (w *FileLogWriter) StartLogger() error {
132 func (w *FileLogWriter) docheck(size int) { 111 func (w *FileLogWriter) docheck(size int) {
133 w.startLock.Lock() 112 w.startLock.Lock()
134 defer w.startLock.Unlock() 113 defer w.startLock.Unlock()
135 if (w.maxlines > 0 && w.maxlines_curlines >= w.maxlines) || 114 if (w.Maxlines > 0 && w.maxlines_curlines >= w.Maxlines) ||
136 (w.maxsize > 0 && w.maxsize_cursize >= w.maxsize) || 115 (w.Maxsize > 0 && w.maxsize_cursize >= w.Maxsize) ||
137 (w.daily && time.Now().Day() != w.daily_opendate) { 116 (w.Daily && time.Now().Day() != w.daily_opendate) {
138 if err := w.DoRotate(); err != nil { 117 if err := w.DoRotate(); err != nil {
139 fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.filename, err) 118 fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err)
140 return 119 return
141 } 120 }
142 } 121 }
...@@ -145,7 +124,7 @@ func (w *FileLogWriter) docheck(size int) { ...@@ -145,7 +124,7 @@ func (w *FileLogWriter) docheck(size int) {
145 } 124 }
146 125
147 func (w *FileLogWriter) WriteMsg(msg string, level int) error { 126 func (w *FileLogWriter) WriteMsg(msg string, level int) error {
148 if level < w.level { 127 if level < w.Level {
149 return nil 128 return nil
150 } 129 }
151 n := 24 + len(msg) // 24 stand for the length "2013/06/23 21:00:22 [T] " 130 n := 24 + len(msg) // 24 stand for the length "2013/06/23 21:00:22 [T] "
...@@ -156,7 +135,7 @@ func (w *FileLogWriter) WriteMsg(msg string, level int) error { ...@@ -156,7 +135,7 @@ func (w *FileLogWriter) WriteMsg(msg string, level int) error {
156 135
157 func (w *FileLogWriter) createLogFile() (*os.File, error) { 136 func (w *FileLogWriter) createLogFile() (*os.File, error) {
158 // Open the log file 137 // Open the log file
159 fd, err := os.OpenFile(w.filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660) 138 fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
160 return fd, err 139 return fd, err
161 } 140 }
162 141
...@@ -169,7 +148,7 @@ func (w *FileLogWriter) initFd() error { ...@@ -169,7 +148,7 @@ func (w *FileLogWriter) initFd() error {
169 w.maxsize_cursize = int(finfo.Size()) 148 w.maxsize_cursize = int(finfo.Size())
170 w.daily_opendate = time.Now().Day() 149 w.daily_opendate = time.Now().Day()
171 if finfo.Size() > 0 { 150 if finfo.Size() > 0 {
172 content, err := ioutil.ReadFile(w.filename) 151 content, err := ioutil.ReadFile(w.Filename)
173 if err != nil { 152 if err != nil {
174 return err 153 return err
175 } 154 }
...@@ -181,18 +160,18 @@ func (w *FileLogWriter) initFd() error { ...@@ -181,18 +160,18 @@ func (w *FileLogWriter) initFd() error {
181 } 160 }
182 161
183 func (w *FileLogWriter) DoRotate() error { 162 func (w *FileLogWriter) DoRotate() error {
184 _, err := os.Lstat(w.filename) 163 _, err := os.Lstat(w.Filename)
185 if err == nil { // file exists 164 if err == nil { // file exists
186 // Find the next available number 165 // Find the next available number
187 num := 1 166 num := 1
188 fname := "" 167 fname := ""
189 for ; err == nil && num <= 999; num++ { 168 for ; err == nil && num <= 999; num++ {
190 fname = w.filename + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), num) 169 fname = w.Filename + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), num)
191 _, err = os.Lstat(fname) 170 _, err = os.Lstat(fname)
192 } 171 }
193 // return error if the last file checked still existed 172 // return error if the last file checked still existed
194 if err == nil { 173 if err == nil {
195 return fmt.Errorf("Rotate: Cannot find free log number to rename %s\n", w.filename) 174 return fmt.Errorf("Rotate: Cannot find free log number to rename %s\n", w.Filename)
196 } 175 }
197 176
198 // block Logger's io.Writer 177 // block Logger's io.Writer
...@@ -204,7 +183,7 @@ func (w *FileLogWriter) DoRotate() error { ...@@ -204,7 +183,7 @@ func (w *FileLogWriter) DoRotate() error {
204 183
205 // close fd before rename 184 // close fd before rename
206 // Rename the file to its newfound home 185 // Rename the file to its newfound home
207 err = os.Rename(w.filename, fname) 186 err = os.Rename(w.Filename, fname)
208 if err != nil { 187 if err != nil {
209 return fmt.Errorf("Rotate: %s\n", err) 188 return fmt.Errorf("Rotate: %s\n", err)
210 } 189 }
...@@ -222,10 +201,10 @@ func (w *FileLogWriter) DoRotate() error { ...@@ -222,10 +201,10 @@ func (w *FileLogWriter) DoRotate() error {
222 } 201 }
223 202
224 func (w *FileLogWriter) deleteOldLog() { 203 func (w *FileLogWriter) deleteOldLog() {
225 dir := path.Dir(w.filename) 204 dir := path.Dir(w.Filename)
226 filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { 205 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) { 206 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)) { 207 if strings.HasPrefix(filepath.Base(path), filepath.Base(w.Filename)) {
229 os.Remove(path) 208 os.Remove(path)
230 } 209 }
231 } 210 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!