forked from ebhomengo/niki
1
0
Fork 0
niki/logger/logger.go

59 lines
1.2 KiB
Go
Raw Normal View History

2023-12-17 13:03:01 +00:00
package logger
import (
"io"
"log/slog"
"os"
"gopkg.in/natefinch/lumberjack.v2"
)
const (
defaultFilePath = "logs/logs.json"
defaultUseLocalTime = false
defaultFileMaxSizeInMB = 10
defaultFileAgeInDays = 30
)
type Config struct {
FilePath string
UseLocalTime bool
FileMaxSizeInMB int
FileMaxAgeInDays int
}
var l *slog.Logger
// init is default logger and Singleton that lets you ensure that a logger has only one instance, while providing a global access point to this instance.
func init() {
fileWriter := &lumberjack.Logger{
Filename: defaultFilePath,
LocalTime: defaultUseLocalTime,
MaxSize: defaultFileMaxSizeInMB,
MaxAge: defaultFileAgeInDays,
}
l = slog.New(
slog.NewJSONHandler(io.MultiWriter(fileWriter, os.Stdout), &slog.HandlerOptions{}),
)
}
func L() *slog.Logger {
return l
}
2023-12-20 15:39:25 +00:00
// New is constructor logger with special settings.
2023-12-17 13:03:01 +00:00
func New(cfg Config, opt *slog.HandlerOptions) *slog.Logger {
fileWriter := &lumberjack.Logger{
Filename: cfg.FilePath,
LocalTime: cfg.UseLocalTime,
MaxSize: cfg.FileMaxSizeInMB,
MaxAge: cfg.FileMaxAgeInDays,
}
logger := slog.New(
slog.NewJSONHandler(io.MultiWriter(fileWriter, os.Stdout), opt),
)
return logger
}