diff --git a/main.go b/main.go index 870a7b1..aab07e5 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( "time" ) +// Block ... const ( MaxLogSize5MB int64 = 5 * 1024 * 1024 MaxLogSize1MB int64 = 1 * 1024 * 1024 @@ -21,6 +22,7 @@ const ( logDirName = "log" ) +// Logger ... type Logger struct { mu *sync.Mutex outputFile *os.File @@ -32,6 +34,7 @@ type Logger struct { splitCount int } +// New ... func New(name string, maxFileSize int64) (logger *Logger, err error) { logger = &Logger{} @@ -60,6 +63,7 @@ func New(name string, maxFileSize int64) (logger *Logger, err error) { return logger, nil } +// Log ... func (l *Logger) Log(format string, v ...interface{}) { if l.outputFile == nil { return @@ -76,6 +80,46 @@ func (l *Logger) Log(format string, v ...interface{}) { l.outputFile.WriteString(s) } +// 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) +} + +// PrintTrace ... +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) +} + +// Trace ... +func (l *Logger) Trace(format string, v ...interface{}) { + if l.outputFile == nil { + return + } + + l.mu.Lock() + defer l.mu.Unlock() + + _, 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) + + if l.shouldSplit(len(s)) { + l.split() + } + l.outputFile.WriteString(s) +} + +// PrintAndTrace ... +func (l *Logger) PrintAndTrace(format string, v ...interface{}) { + l.Print(format, v) + l.Trace(format, v) +} + +// LogHTTPRequest ... func (l *Logger) LogHTTPRequest(req *http.Request, userID string) string { if userID == "" { userID = "-" @@ -98,10 +142,12 @@ func (l *Logger) LogHTTPRequest(req *http.Request, userID string) string { const splitLine = "==============================================================" +// LogHTTPResponse ... func (l *Logger) LogHTTPResponse(status int, duration time.Duration, size int) string { return fmt.Sprintf("Response:\n%d %v %dB\n%s\n", status, duration, size, splitLine) } +// CombineHTTPLogs ... func (l *Logger) CombineHTTPLogs(in string, out string) { if l.outputFile == nil { return @@ -117,36 +163,7 @@ func (l *Logger) CombineHTTPLogs(in string, out string) { l.outputFile.WriteString(msg) } -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) -} - -func (l *Logger) Print(format string, v ...interface{}) { - msg := fmt.Sprintf(format, v...) - fmt.Printf("%s: %s\n", time.Now().Format(time.RFC3339), msg) -} - -func (l *Logger) Trace(format string, v ...interface{}) { - if l.outputFile == nil { - return - } - - l.mu.Lock() - defer l.mu.Unlock() - - _, 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) - - if l.shouldSplit(len(s)) { - l.split() - } - l.outputFile.WriteString(s) -} - +// Close ... func (l *Logger) Close() { if l.outputFile == nil { return @@ -185,3 +202,4 @@ func (l *Logger) shouldSplit(nextEntrySize int) bool { stats, _ := l.outputFile.Stat() return int64(nextEntrySize) >= (l.maxFileSize - stats.Size()) } +