diff options
author | Robin Jarry <robin@jarry.cc> | 2022-11-19 14:34:10 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-11-21 13:18:34 +0100 |
commit | e5f2fb08d8a7604aeef726698ef696874bcd2561 (patch) | |
tree | 6927564ae1f016abc16bb9c5041969092c3ba10c /config/general.go | |
parent | 9db3710dd73b6949321a028b4dc2dc2277e97ce0 (diff) | |
download | aerc-e5f2fb08d8a7604aeef726698ef696874bcd2561.tar.gz |
config: add log-file and log-level settings
Allow configuring persistent logging to file with a log level. When
redirecting the output of aerc to a file these two settings are ignored
and all messages are printed to stdout.
Suggested-by: Moritz Poldrack <moritz@poldrack.dev>
Suggested-by: Tim Culverhouse <tim@timculverhouse.com>
Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'config/general.go')
-rw-r--r-- | config/general.go | 41 |
1 files changed, 36 insertions, 5 deletions
diff --git a/config/general.go b/config/general.go index 7f8ed2ef..2a462a3e 100644 --- a/config/general.go +++ b/config/general.go @@ -2,38 +2,69 @@ package config import ( "fmt" + "os" "git.sr.ht/~rjarry/aerc/logging" "github.com/go-ini/ini" + "github.com/mattn/go-isatty" + "github.com/mitchellh/go-homedir" ) type GeneralConfig struct { - DefaultSavePath string `ini:"default-save-path"` - PgpProvider string `ini:"pgp-provider"` - UnsafeAccountsConf bool `ini:"unsafe-accounts-conf"` + DefaultSavePath string `ini:"default-save-path"` + PgpProvider string `ini:"pgp-provider"` + UnsafeAccountsConf bool `ini:"unsafe-accounts-conf"` + LogFile string `ini:"log-file"` + LogLevel logging.LogLevel `ini:"-"` } func defaultGeneralConfig() GeneralConfig { return GeneralConfig{ PgpProvider: "internal", UnsafeAccountsConf: false, + LogLevel: logging.INFO, } } func (config *AercConfig) parseGeneral(file *ini.File) error { + var level *ini.Key + var logFile *os.File + gen, err := file.GetSection("general") if err != nil { goto end } - if err := gen.MapTo(&config.General); err != nil { return err } + level, err = gen.GetKey("log-level") + if err == nil { + l, err := logging.ParseLevel(level.String()) + if err != nil { + return err + } + config.General.LogLevel = l + } if err := config.General.validatePgpProvider(); err != nil { return err } - end: + if !isatty.IsTerminal(os.Stdout.Fd()) { + logFile = os.Stdout + // redirected to file, force DEBUG level + config.General.LogLevel = logging.DEBUG + } else if config.General.LogFile != "" { + path, err := homedir.Expand(config.General.LogFile) + if err != nil { + return fmt.Errorf("log-file: %w", err) + } + logFile, err = os.OpenFile(path, + os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600) + if err != nil { + return fmt.Errorf("log-file: %w", err) + } + } + logging.Init(logFile, config.General.LogLevel) logging.Debugf("aerc.conf: [general] %#v", config.General) return nil } |