diff --git a/main.go b/main.go index dfe162b..7d13f37 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,6 @@ const ( MaxLogSize500KB int64 = 500 * 1024 MaxLogSize100KB int64 = 100 * 1024 MaxLogSize512B int64 = 512 - logDirName = "log" ) // Logger ... @@ -36,20 +35,22 @@ type Logger struct { maxFileSize int64 splitCount int + + directory string } // New ... -func New(name string, maxFileSize int64) (logger *Logger, err error) { +func New(name, dir string, maxFileSize int64) (logger *Logger, err error) { logger = &Logger{} logger.outputFileName = name logger.mu = &sync.Mutex{} logger.maxFileSize = maxFileSize + logger.directory = dir - err = os.Mkdir(logDirName, os.ModePerm) + err = os.Mkdir(dir, os.ModePerm) if err != nil { if !os.IsExist(err) { - fmt.Fprintf(os.Stderr, "gologger: couldn't create event log directory: %s\n", err.Error()) return nil, err } } @@ -57,10 +58,8 @@ func New(name string, maxFileSize int64) (logger *Logger, err error) { date := strings.Replace(time.Now().Format(dateTimeFormat), ":", ".", -1) logger.outputFileName += " " + date logger.fullName = logger.outputFileName + ".txt" - path := filepath.Join(logDirName, logger.fullName) - logger.outputFile, err = os.Create(path) - if err != nil { - fmt.Fprintf(os.Stderr, "gologger: couldn't create event log file: %s\n", err.Error()) + path := filepath.Join(dir, logger.fullName) + if logger.outputFile, err = os.Create(path); err != nil { return nil, err } @@ -177,38 +176,31 @@ func (l *Logger) CombineHTTPLogs(in string, out string) { } // Close ... -func (l *Logger) Close() { +func (l *Logger) Close() error { if l.outputFile == nil { - return + return nil } - err := l.outputFile.Close() - if err != nil { - fmt.Fprintf(os.Stderr, "gologger: couldn't close event log file: %s\n", err.Error()) - } + return l.outputFile.Close() } -func (l *Logger) split() { +func (l *Logger) split() error { if l.outputFile == nil { - return + return nil } // close old file err := l.outputFile.Close() if err != nil { - fmt.Fprintf(os.Stderr, "gologger: split: couldn't close event file\n") - return + return err } - l.splitCount++ // open new file - var errnew error - path := filepath.Join(logDirName, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)+".txt") - l.outputFile, errnew = os.Create(path) + l.splitCount++ + path := filepath.Join(l.directory, l.outputFileName+fmt.Sprintf("(%d)", l.splitCount)+".txt") + l.outputFile, err = os.Create(path) - if errnew != nil { - fmt.Fprintf(os.Stderr, "gologger: couldn't create new event log file: %s\n", err.Error()) - } + return err } func (l *Logger) shouldSplit(nextEntrySize int) bool {