Files
esctl/pkg/log/logger.go

60 lines
1.4 KiB
Go
Raw Normal View History

2026-04-21 10:50:09 +02:00
/*
Copyright © 2026 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package log
import (
"log/slog"
"os"
"runtime/debug"
"codeberg.org/scip/esctl/pkg/cfg"
"github.com/mattn/go-isatty"
"github.com/tlinden/yadu"
)
const LevelNotice = slog.Level(2)
func Init(conf *cfg.Config) {
2026-07-13 13:33:40 +02:00
logLevel := new(slog.LevelVar{})
2026-04-21 10:50:09 +02:00
2026-07-13 13:33:40 +02:00
opts := new(yadu.Options{
2026-04-21 10:50:09 +02:00
Level: logLevel,
AddSource: true,
NoColor: !isatty.IsTerminal(os.Stdout.Fd()),
2026-07-13 13:33:40 +02:00
})
2026-04-21 10:50:09 +02:00
buildInfo, _ := debug.ReadBuildInfo()
handler := yadu.NewHandler(os.Stdout, opts)
debuglogger := slog.New(handler).With(
slog.Group("program_info",
slog.String("version", cfg.Version),
slog.String("go_version", buildInfo.GoVersion),
),
)
slog.SetDefault(debuglogger)
switch conf.Debug {
case true:
logLevel.Set(slog.LevelDebug)
default:
logLevel.Set(slog.LevelInfo)
}
}