count log file lines
This commit is contained in:
		
							parent
							
								
									07c628c7e9
								
							
						
					
					
						commit
						6a9d04c269
					
				
							
								
								
									
										34
									
								
								logs/file.go
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								logs/file.go
									
									
									
									
									
								
							| @ -15,10 +15,11 @@ | |||||||
| package logs | package logs | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"bytes" | ||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io/ioutil" | 	"io" | ||||||
| 	"log" | 	"log" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| @ -170,17 +171,44 @@ 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() | ||||||
| 	if finfo.Size() > 0 { | 	if finfo.Size() > 0 { | ||||||
| 		content, err := ioutil.ReadFile(w.Filename) | 		count, err := w.lines() | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return err | 			return err | ||||||
| 		} | 		} | ||||||
| 		w.maxlines_curlines = len(strings.Split(string(content), "\n")) | 		w.maxlines_curlines = count | ||||||
| 	} else { | 	} else { | ||||||
| 		w.maxlines_curlines = 0 | 		w.maxlines_curlines = 0 | ||||||
| 	} | 	} | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func (w *FileLogWriter) lines() (int, error) { | ||||||
|  | 	fd, err := os.Open(w.Filename) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return 0, err | ||||||
|  | 	} | ||||||
|  | 	defer fd.Close() | ||||||
|  | 
 | ||||||
|  | 	buf := make([]byte, 32768) // 32k | ||||||
|  | 	count := 0 | ||||||
|  | 	lineSep := []byte{'\n'} | ||||||
|  | 
 | ||||||
|  | 	for { | ||||||
|  | 		c, err := fd.Read(buf) | ||||||
|  | 		if err != nil && err != io.EOF { | ||||||
|  | 			return count, err | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		count += bytes.Count(buf[:c], lineSep) | ||||||
|  | 
 | ||||||
|  | 		if err == io.EOF { | ||||||
|  | 			break | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return count, nil | ||||||
|  | } | ||||||
|  | 
 | ||||||
| // DoRotate means it need to write file in new file. | // DoRotate means it need to write file in new file. | ||||||
| // new file name like xx.log.2013-01-01.2 | // new file name like xx.log.2013-01-01.2 | ||||||
| func (w *FileLogWriter) DoRotate() error { | func (w *FileLogWriter) DoRotate() error { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user