main.go 4.41 KB
package gologger

import (
	"fmt"
	"net/http"
	"net/http/httputil"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"sync"
	"time"
)

const dateTimeFormat = "2006-01-02 15:04:05"

// Block ...
const (
	MaxLogSize5MB   int64 = 5 * 1024 * 1024
	MaxLogSize1MB   int64 = 1 * 1024 * 1024
	MaxLogSize500KB int64 = 500 * 1024
	MaxLogSize100KB int64 = 100 * 1024
	MaxLogSize512B  int64 = 512
	logDirName            = "log"
)

// Logger ...
type Logger struct {
	mu         *sync.Mutex
	outputFile *os.File

	outputFileName string
	fullName       string
	maxFileSize    int64

	splitCount int
}

// New ...
func New(name string, maxFileSize int64) (logger *Logger, err error) {
	logger = &Logger{}

	logger.outputFileName = name
	logger.mu = &sync.Mutex{}
	logger.maxFileSize = maxFileSize

	err = os.Mkdir(logDirName, 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
		}
	}

	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())
		return nil, err
	}

	return logger, nil
}

// Log ...
func (l *Logger) Log(format string, v ...interface{}) {
	if l.outputFile == nil {
		return
	}

	l.mu.Lock()
	defer l.mu.Unlock()

	msg := fmt.Sprintf(format, v...)
	s := time.Now().Format(dateTimeFormat) + ": " + msg + "\n"
	if l.shouldSplit(len(s)) {
		l.split()
	}
	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(dateTimeFormat), 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(dateTimeFormat), 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(dateTimeFormat), 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 = "-"
	}

	var b strings.Builder

	b.WriteString("Request:\n")
	// CLF-like header
	fmt.Fprintf(&b, "%s %s %s\n", req.RemoteAddr, userID, time.Now().Format(dateTimeFormat))

	body, err := httputil.DumpRequest(req, true)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
	}
	b.WriteString(string(body) + "\n\n")

	return b.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
	}

	l.mu.Lock()
	defer l.mu.Unlock()

	msg := in + out
	if l.shouldSplit(len(msg)) {
		l.split()
	}
	l.outputFile.WriteString(msg)
}

// Close ...
func (l *Logger) Close() {
	if l.outputFile == nil {
		return
	}

	err := l.outputFile.Close()
	if err != nil {
		fmt.Fprintf(os.Stderr, "gologger: couldn't close event log file: %s\n", err.Error())
	}
}

func (l *Logger) split() {
	if l.outputFile == nil {
		return
	}

	// close old file
	err := l.outputFile.Close()
	if err != nil {
		fmt.Fprintf(os.Stderr, "gologger: split: couldn't close event file\n")
		return
	}

	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)

	if errnew != nil {
		fmt.Fprintf(os.Stderr, "gologger: couldn't create new event log file: %s\n", err.Error())
	}
}

func (l *Logger) shouldSplit(nextEntrySize int) bool {
	stats, _ := l.outputFile.Stat()
	return int64(nextEntrySize) >= (l.maxFileSize - stats.Size())
}