Skip to content
This repository has been archived by the owner on Jan 4, 2025. It is now read-only.

Commit

Permalink
Merge pull request #39 from tulilirockz/fix-logging
Browse files Browse the repository at this point in the history
fix: implement file-based logging and log levels properly
  • Loading branch information
bketelsen authored Feb 27, 2024
2 parents e7eed68 + b4f3163 commit 2f8d223
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
5 changes: 5 additions & 0 deletions cmd/layer/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spf13/cobra"
"github.com/ublue-os/bext/internal"
"github.com/ublue-os/bext/pkg/fileio"
"github.com/ublue-os/bext/pkg/logging"
)

var ListCmd = &cobra.Command{
Expand Down Expand Up @@ -53,6 +54,10 @@ func listCmd(cmd *cobra.Command, args []string) error {
return err
}

if !*fLogOnly {
slog.SetDefault(logging.NewMuteLogger())
}

if *fCheck {
if len(args) < 1 {
return internal.NewPositionalError("LAYER")
Expand Down
78 changes: 39 additions & 39 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"log/slog"
"os"
"path"
Expand All @@ -11,65 +10,66 @@ import (
"github.com/ublue-os/bext/cmd/layer"
"github.com/ublue-os/bext/cmd/mount"
"github.com/ublue-os/bext/internal"
"github.com/ublue-os/bext/pkg/logging"
appLogging "github.com/ublue-os/bext/pkg/logging"
)

var RootCmd = &cobra.Command{
Use: "bext",
Short: "Manager for Systemd system extensions",
Long: `Manage your systemd system extensions from your CLI, managing their cache, multiple versions, and building.`,
SilenceUsage: true,
Use: "bext",
Short: "Manager for Systemd system extensions",
Long: `Manage your systemd system extensions from your CLI, managing their cache, multiple versions, and building.`,
PersistentPreRunE: initLogging,
SilenceUsage: true,
}

var (
fLogFile string
fLogLevel string
fNoLogging bool
)

func Execute() {
err := RootCmd.Execute()
if err != nil {
if err := RootCmd.Execute(); err != nil {
slog.Debug("Application exited with error", slog.String("errormsg", err.Error()), slog.Int("exitcode", 1))
os.Exit(1)
}
}

var (
fLogFile *string
fLogLevel *string
fNoLogging *bool
)

func init() {
fLogFile = RootCmd.PersistentFlags().String("log-file", "", "File where user facing logs will be written to")
fLogLevel = RootCmd.PersistentFlags().String("log-level", "info", "File where user facing logs will be written to")
fNoLogging = RootCmd.PersistentFlags().Bool("quiet", false, "Do not log anything to anywhere")
internal.Config.NoProgress = RootCmd.PersistentFlags().Bool("no-progress", false, "Do not use progress bars whenever they would be")

RootCmd.AddCommand(layer.LayerCmd)
RootCmd.AddCommand(mount.MountCmd)
RootCmd.AddCommand(AddToPathCmd)

if *fNoLogging {
slog.SetDefault(appLogging.NewMuteLogger())
return
}
func initLogging(cmd *cobra.Command, args []string) error {
var logWriter *os.File = os.Stdout
if *fLogFile != "" {
abs, err := filepath.Abs(path.Clean(*fLogFile))
if fLogFile != "-" {
abs, err := filepath.Abs(path.Clean(fLogFile))
if err != nil {
os.Exit(1)
return err
}
logWriter, err = os.Create(abs)
logWriter, err = os.OpenFile(abs, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0755)
if err != nil {
fmt.Println("Could not open log file")
os.Exit(1)
return err
}
defer logWriter.Close()
}

logLevel, err := appLogging.StrToLogLevel(*fLogLevel)
logLevel, err := appLogging.StrToLogLevel(fLogLevel)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
return err
}

main_app_logger := slog.New(appLogging.SetupAppLogger(logWriter, logLevel, *fLogFile != ""))
main_app_logger := slog.New(appLogging.SetupAppLogger(logWriter, logLevel, fLogFile != "-"))

if fNoLogging {
slog.SetDefault(logging.NewMuteLogger())
} else {
slog.SetDefault(main_app_logger)
}
return nil
}

slog.SetDefault(main_app_logger)
func init() {
RootCmd.PersistentFlags().StringVar(&fLogFile, "log-file", "-", "File where user-facing logs will be written to")
RootCmd.PersistentFlags().StringVar(&fLogLevel, "log-level", "info", "Log level for user-facing logs")
RootCmd.PersistentFlags().BoolVar(&fNoLogging, "quiet", false, "Do not log anything to anywhere")
internal.Config.NoProgress = RootCmd.PersistentFlags().Bool("no-progress", false, "Do not use progress bars whenever they would be")

RootCmd.AddCommand(layer.LayerCmd)
RootCmd.AddCommand(mount.MountCmd)
RootCmd.AddCommand(AddToPathCmd)
}

0 comments on commit 2f8d223

Please sign in to comment.