// This package is different from the `logger` package.
// It is used to log information about this plugin itself.
// As a PDK plugin can't log to the standard output,
// we need to implement our own logging mechanism.

package log

import (
	"fmt"
	"time"

	"gopkg.in/natefinch/lumberjack.v2"
)

var (
	logger *Logger
)

type Logger struct {
	lumberjack.Logger
}

func init() {
	logger = &Logger{
		Logger: lumberjack.Logger{
			Filename:   "/tmp/plugin-audit-kong.log",
			MaxSize:    10, // megabytes
			MaxAge:     28, // days
			MaxBackups: 3,
		},
	}
}

func Info(msg string) {
	_, _ = fmt.Fprintf(logger, "%s [INFO] %s\n", ts(), msg)
}

func Infof(format string, args ...any) {
	msg := fmt.Sprintf(format, args...)
	_, _ = fmt.Fprintf(logger, "%s [INFO] %s\n", ts(), msg)
}

func Error(msg string) {
	_, _ = fmt.Fprintf(logger, "%s [ERROR] %s\n", ts(), msg)
}

func Errorf(format string, args ...any) {
	msg := fmt.Sprintf(format, args...)
	_, _ = fmt.Fprintf(logger, "%s [ERROR] %s\n", ts(), msg)
}

func ts() string {
	return time.Now().Format("2006/01/02 15:04:05")
}
