feat: add log-path flag
This commit is contained in:
parent
d9a1e50f49
commit
8886a1baad
2 changed files with 23 additions and 14 deletions
11
cmd/root.go
11
cmd/root.go
|
|
@ -40,18 +40,17 @@ var (
|
|||
level = slog.LevelError
|
||||
}
|
||||
|
||||
if err := logger.Load(level); err != nil {
|
||||
logPath, _ := flags.GetString("log-path")
|
||||
if err := logger.Load(logPath, level); err != nil {
|
||||
return fmt.Errorf("failed to load logger: %w", err)
|
||||
}
|
||||
|
||||
configPath, _ := flags.GetString("config")
|
||||
configPath, _ := flags.GetString("config-path")
|
||||
cfg, err := config.Load(configPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load config: %w", err)
|
||||
}
|
||||
|
||||
tview.Styles.PrimitiveBackgroundColor = tcell.GetColor(cfg.Theme.BackgroundColor)
|
||||
|
||||
token, _ := flags.GetString("token")
|
||||
if token == "" {
|
||||
token, err = keyring.Get(consts.Name, "token")
|
||||
|
|
@ -60,6 +59,7 @@ var (
|
|||
}
|
||||
}
|
||||
|
||||
tview.Styles.PrimitiveBackgroundColor = tcell.GetColor(cfg.Theme.BackgroundColor)
|
||||
app = newApplication(cfg)
|
||||
return app.run(token)
|
||||
},
|
||||
|
|
@ -71,6 +71,7 @@ var (
|
|||
func init() {
|
||||
flags := rootCmd.Flags()
|
||||
flags.StringP("token", "t", "", "authentication token")
|
||||
flags.StringP("config", "c", config.DefaultPath(), "path of the configuration file")
|
||||
flags.String("config-path", config.DefaultPath(), "path of the configuration file")
|
||||
flags.String("log-path", logger.DefaultPath(), "path of the log file")
|
||||
flags.String("log-level", "info", "log level")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
|
@ -8,22 +9,29 @@ import (
|
|||
"github.com/ayn2op/discordo/internal/consts"
|
||||
)
|
||||
|
||||
// Load opens the log file and configures default logger.
|
||||
func Load(level slog.Level) error {
|
||||
const fileName = "logs.txt"
|
||||
|
||||
func DefaultPath() string {
|
||||
path, err := os.UserCacheDir()
|
||||
if err != nil {
|
||||
slog.Info(
|
||||
"user cache directory path cannot be determined; falling back to the current directory path",
|
||||
)
|
||||
path = "."
|
||||
}
|
||||
|
||||
return filepath.Join(path, consts.Name, fileName)
|
||||
}
|
||||
|
||||
// Load opens the log file and configures default logger.
|
||||
func Load(path string, level slog.Level) error {
|
||||
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
path = filepath.Join(path, consts.Name)
|
||||
if err := os.MkdirAll(path, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
path = filepath.Join(path, "logs.txt")
|
||||
file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("failed to open log file: %w", err)
|
||||
}
|
||||
|
||||
opts := &slog.HandlerOptions{AddSource: true, Level: level}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue