Commit c138614d93069506b195c717ad830679af51b209

Authored by Marko Tikvić
1 parent a7081e5813
Exists in master

output directory controlor; better error handling

Showing 1 changed file with 17 additions and 25 deletions   Show diff stats
... ... @@ -23,7 +23,6 @@ const (
23 23 MaxLogSize500KB int64 = 500 * 1024
24 24 MaxLogSize100KB int64 = 100 * 1024
25 25 MaxLogSize512B int64 = 512
26   - logDirName = "log"
27 26 )
28 27  
29 28 // Logger ...
... ... @@ -36,20 +35,22 @@ type Logger struct {
36 35 maxFileSize int64
37 36  
38 37 splitCount int
  38 +
  39 + directory string
39 40 }
40 41  
41 42 // New ...
42   -func New(name string, maxFileSize int64) (logger *Logger, err error) {
  43 +func New(name, dir string, maxFileSize int64) (logger *Logger, err error) {
43 44 logger = &Logger{}
44 45  
45 46 logger.outputFileName = name
46 47 logger.mu = &sync.Mutex{}
47 48 logger.maxFileSize = maxFileSize
  49 + logger.directory = dir
48 50  
49   - err = os.Mkdir(logDirName, os.ModePerm)
  51 + err = os.Mkdir(dir, os.ModePerm)
50 52 if err != nil {
51 53 if !os.IsExist(err) {
52   - fmt.Fprintf(os.Stderr, "gologger: couldn't create event log directory: %s\n", err.Error())
53 54 return nil, err
54 55 }
55 56 }
... ... @@ -57,10 +58,8 @@ func New(name string, maxFileSize int64) (logger *Logger, err error) {
57 58 date := strings.Replace(time.Now().Format(dateTimeFormat), ":", ".", -1)
58 59 logger.outputFileName += " " + date
59 60 logger.fullName = logger.outputFileName + ".txt"
60   - path := filepath.Join(logDirName, logger.fullName)
61   - logger.outputFile, err = os.Create(path)
62   - if err != nil {
63   - fmt.Fprintf(os.Stderr, "gologger: couldn't create event log file: %s\n", err.Error())
  61 + path := filepath.Join(dir, logger.fullName)
  62 + if logger.outputFile, err = os.Create(path); err != nil {
64 63 return nil, err
65 64 }
66 65  
... ... @@ -177,38 +176,31 @@ func (l *Logger) CombineHTTPLogs(in string, out string) {
177 176 }
178 177  
179 178 // Close ...
180   -func (l *Logger) Close() {
  179 +func (l *Logger) Close() error {
181 180 if l.outputFile == nil {
182   - return
  181 + return nil
183 182 }
184 183  
185   - err := l.outputFile.Close()
186   - if err != nil {
187   - fmt.Fprintf(os.Stderr, "gologger: couldn't close event log file: %s\n", err.Error())
188   - }
  184 + return l.outputFile.Close()
189 185 }
190 186  
191   -func (l *Logger) split() {
  187 +func (l *Logger) split() error {
192 188 if l.outputFile == nil {
193   - return
  189 + return nil
194 190 }
195 191  
196 192 // close old file
197 193 err := l.outputFile.Close()
198 194 if err != nil {
199   - fmt.Fprintf(os.Stderr, "gologger: split: couldn't close event file\n")
200   - return
  195 + return err
201 196 }
202 197  
203   - l.splitCount++
204 198 // open new file
205   - var errnew error
206   - path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)+".txt")
207   - l.outputFile, errnew = os.Create(path)
  199 + l.splitCount++
  200 + path := filepath.Join(l.directory, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)+".txt")
  201 + l.outputFile, err = os.Create(path)
208 202  
209   - if errnew != nil {
210   - fmt.Fprintf(os.Stderr, "gologger: couldn't create new event log file: %s\n", err.Error())
211   - }
  203 + return err
212 204 }
213 205  
214 206 func (l *Logger) shouldSplit(nextEntrySize int) bool {
... ...