Merge pull request #932 from DeanThompson/master
count log file lines
Showing
1 changed file
with
31 additions
and
3 deletions
| ... | @@ -15,10 +15,11 @@ | ... | @@ -15,10 +15,11 @@ |
| 15 | package logs | 15 | package logs |
| 16 | 16 | ||
| 17 | import ( | 17 | import ( |
| 18 | "bytes" | ||
| 18 | "encoding/json" | 19 | "encoding/json" |
| 19 | "errors" | 20 | "errors" |
| 20 | "fmt" | 21 | "fmt" |
| 21 | "io/ioutil" | 22 | "io" |
| 22 | "log" | 23 | "log" |
| 23 | "os" | 24 | "os" |
| 24 | "path/filepath" | 25 | "path/filepath" |
| ... | @@ -170,17 +171,44 @@ func (w *FileLogWriter) initFd() error { | ... | @@ -170,17 +171,44 @@ func (w *FileLogWriter) initFd() error { |
| 170 | w.maxsize_cursize = int(finfo.Size()) | 171 | w.maxsize_cursize = int(finfo.Size()) |
| 171 | w.daily_opendate = time.Now().Day() | 172 | w.daily_opendate = time.Now().Day() |
| 172 | if finfo.Size() > 0 { | 173 | if finfo.Size() > 0 { |
| 173 | content, err := ioutil.ReadFile(w.Filename) | 174 | count, err := w.lines() |
| 174 | if err != nil { | 175 | if err != nil { |
| 175 | return err | 176 | return err |
| 176 | } | 177 | } |
| 177 | w.maxlines_curlines = len(strings.Split(string(content), "\n")) | 178 | w.maxlines_curlines = count |
| 178 | } else { | 179 | } else { |
| 179 | w.maxlines_curlines = 0 | 180 | w.maxlines_curlines = 0 |
| 180 | } | 181 | } |
| 181 | return nil | 182 | return nil |
| 182 | } | 183 | } |
| 183 | 184 | ||
| 185 | func (w *FileLogWriter) lines() (int, error) { | ||
| 186 | fd, err := os.Open(w.Filename) | ||
| 187 | if err != nil { | ||
| 188 | return 0, err | ||
| 189 | } | ||
| 190 | defer fd.Close() | ||
| 191 | |||
| 192 | buf := make([]byte, 32768) // 32k | ||
| 193 | count := 0 | ||
| 194 | lineSep := []byte{'\n'} | ||
| 195 | |||
| 196 | for { | ||
| 197 | c, err := fd.Read(buf) | ||
| 198 | if err != nil && err != io.EOF { | ||
| 199 | return count, err | ||
| 200 | } | ||
| 201 | |||
| 202 | count += bytes.Count(buf[:c], lineSep) | ||
| 203 | |||
| 204 | if err == io.EOF { | ||
| 205 | break | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | return count, nil | ||
| 210 | } | ||
| 211 | |||
| 184 | // DoRotate means it need to write file in new file. | 212 | // DoRotate means it need to write file in new file. |
| 185 | // new file name like xx.log.2013-01-01.2 | 213 | // new file name like xx.log.2013-01-01.2 |
| 186 | func (w *FileLogWriter) DoRotate() error { | 214 | func (w *FileLogWriter) DoRotate() error { | ... | ... |
-
Please register or sign in to post a comment