Initial commit
This commit is contained in:
14
internal/log/config/log.go
Normal file
14
internal/log/config/log.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package config
|
||||
|
||||
import "log/slog"
|
||||
|
||||
type Log struct {
|
||||
Level slog.Level
|
||||
|
||||
FilePath string
|
||||
|
||||
TextOutput bool
|
||||
StdoutEnabled bool
|
||||
FileEnabled bool
|
||||
FluentEnabled bool
|
||||
}
|
||||
56
internal/log/module.go
Normal file
56
internal/log/module.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
slogmulti "github.com/samber/slog-multi"
|
||||
"go.uber.org/fx"
|
||||
|
||||
"payouts/internal/config"
|
||||
)
|
||||
|
||||
var Module = fx.Options(
|
||||
fx.Provide(NewLogger),
|
||||
)
|
||||
|
||||
// Params represents the module input params
|
||||
type Params struct {
|
||||
fx.In
|
||||
|
||||
AppConfig *config.App
|
||||
}
|
||||
|
||||
func NewLogger(p Params) (*slog.Logger, error) {
|
||||
logConfig := p.AppConfig.Log
|
||||
opts := &slog.HandlerOptions{
|
||||
Level: logConfig.Level,
|
||||
}
|
||||
|
||||
handlers := []slog.Handler{}
|
||||
if logConfig.StdoutEnabled {
|
||||
if logConfig.TextOutput {
|
||||
handlers = append(handlers, slog.NewTextHandler(os.Stdout, opts))
|
||||
} else {
|
||||
handlers = append(handlers, slog.NewJSONHandler(os.Stdout, opts))
|
||||
}
|
||||
}
|
||||
|
||||
if logConfig.FileEnabled {
|
||||
file, err := os.OpenFile(logConfig.FilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if logConfig.TextOutput {
|
||||
handlers = append(handlers, slog.NewTextHandler(file, opts))
|
||||
} else {
|
||||
handlers = append(handlers, slog.NewJSONHandler(file, opts))
|
||||
}
|
||||
}
|
||||
|
||||
logger := slog.New(slogmulti.Fanout(handlers...))
|
||||
slog.SetDefault(logger)
|
||||
|
||||
return logger, nil
|
||||
}
|
||||
Reference in New Issue
Block a user