mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 09:44:17 +02:00
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
/*
|
|
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) {
|
|
logLevel := &slog.LevelVar{}
|
|
|
|
opts := &yadu.Options{
|
|
Level: logLevel,
|
|
AddSource: true,
|
|
NoColor: !isatty.IsTerminal(os.Stdout.Fd()),
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|