diff --git a/main.go b/main.go index aab07e5..6bc0f55 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,8 @@ import ( "time" ) +const dateTimeFormat = "2006-01-02 15:04:05" + // Block ... const ( MaxLogSize5MB int64 = 5 * 1024 * 1024 @@ -38,25 +40,25 @@ type Logger struct { func New(name string, maxFileSize int64) (logger *Logger, err error) { logger = &Logger{} - logger.outputFileName = name + "-log" + logger.outputFileName = name logger.mu = &sync.Mutex{} logger.maxFileSize = maxFileSize err = os.Mkdir(logDirName, os.ModePerm) if err != nil { if !os.IsExist(err) { - fmt.Printf("logger: mkdir: couldn't create event log directory\n") + fmt.Fprintf(os.Stderr, "gologger: couldn't create event log directory: %s\n", err.Error()) return nil, err } } - date := strings.Replace(time.Now().Format(time.RFC3339), ":", ".", -1) - logger.outputFileName += "_" + date + 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.Printf("logger: new: couldn't create event log file\n") + fmt.Fprintf(os.Stderr, "gologger: couldn't create event log file: %s\n", err.Error()) return nil, err } @@ -73,7 +75,7 @@ func (l *Logger) Log(format string, v ...interface{}) { defer l.mu.Unlock() msg := fmt.Sprintf(format, v...) - s := time.Now().Format(time.RFC3339) + ": " + msg + "\n" + s := time.Now().Format(dateTimeFormat) + ": " + msg + "\n" if l.shouldSplit(len(s)) { l.split() } @@ -83,7 +85,7 @@ func (l *Logger) Log(format string, v ...interface{}) { // Print ... func (l *Logger) Print(format string, v ...interface{}) { msg := fmt.Sprintf(format, v...) - fmt.Printf("%s: %s\n", time.Now().Format(time.RFC3339), msg) + fmt.Printf("%s: %s\n", time.Now().Format(dateTimeFormat), msg) } // PrintTrace ... @@ -91,7 +93,7 @@ func (l *Logger) PrintTrace(format string, v ...interface{}) { _, file, line, _ := runtime.Caller(1) msg := fmt.Sprintf(format, v...) - fmt.Printf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg) + fmt.Printf("%s: %s %d: %s\n", time.Now().Format(dateTimeFormat), file, line, msg) } // Trace ... @@ -105,7 +107,7 @@ func (l *Logger) Trace(format string, v ...interface{}) { _, file, line, _ := runtime.Caller(1) msg := fmt.Sprintf(format, v...) - s := fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(time.RFC3339), file, line, msg) + s := fmt.Sprintf("%s: %s %d: %s\n", time.Now().Format(dateTimeFormat), file, line, msg) if l.shouldSplit(len(s)) { l.split() @@ -129,7 +131,7 @@ func (l *Logger) LogHTTPRequest(req *http.Request, userID string) string { b.WriteString("Request:\n") // CLF-like header - fmt.Fprintf(&b, "%s %s %s\n", req.RemoteAddr, userID, time.Now().Format(time.RFC3339)) + fmt.Fprintf(&b, "%s %s %s\n", req.RemoteAddr, userID, time.Now().Format(dateTimeFormat)) body, err := httputil.DumpRequest(req, true) if err != nil { @@ -171,7 +173,7 @@ func (l *Logger) Close() { err := l.outputFile.Close() if err != nil { - fmt.Printf("logger: on exit: couldn't close event log file\n") + fmt.Fprintf(os.Stderr, "gologger: couldn't close event log file: %s\n", err.Error()) } } @@ -183,7 +185,7 @@ func (l *Logger) split() { // close old file err := l.outputFile.Close() if err != nil { - fmt.Printf("logger: split: couldn't close event file\n") + fmt.Fprintf(os.Stderr, "gologger: split: couldn't close event file\n") return } @@ -194,7 +196,7 @@ func (l *Logger) split() { l.outputFile, errnew = os.Create(path) if errnew != nil { - fmt.Printf("logger: split: couldn't create event log file\n") + fmt.Fprintf(os.Stderr, "gologger: couldn't create new event log file: %s\n", err.Error()) } } @@ -202,4 +204,3 @@ func (l *Logger) shouldSplit(nextEntrySize int) bool { stats, _ := l.outputFile.Stat() return int64(nextEntrySize) >= (l.maxFileSize - stats.Size()) } -