aboutsummaryrefslogtreecommitdiffstats
path: root/config
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2022-11-19 14:34:10 +0100
committerRobin Jarry <robin@jarry.cc>2022-11-21 13:18:34 +0100
commite5f2fb08d8a7604aeef726698ef696874bcd2561 (patch)
tree6927564ae1f016abc16bb9c5041969092c3ba10c /config
parent9db3710dd73b6949321a028b4dc2dc2277e97ce0 (diff)
downloadaerc-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')
-rw-r--r--config/aerc.conf15
-rw-r--r--config/binds.go2
-rw-r--r--config/config.go13
-rw-r--r--config/general.go41
4 files changed, 57 insertions, 14 deletions
diff --git a/config/aerc.conf b/config/aerc.conf
index 557cea8e..adfa1e12 100644
--- a/config/aerc.conf
+++ b/config/aerc.conf
@@ -24,6 +24,21 @@ pgp-provider=internal
# Default: false
unsafe-accounts-conf=false
+# Output log messages to specified file. A path starting with ~/ is expanded to
+# the user home dir. When redirecting aerc's output to a file using > shell
+# redirection, this setting is ignored and log messages are printed to stdout.
+#
+# Default: ""
+#log-file=
+
+# Only log messages above the specified level to log-file. Supported levels
+# are: debug, info, warn and error. When redirecting aerc's output to a file
+# using > shell redirection, this setting is ignored and the log level is
+# forced to debug.
+#
+# Default: info
+#log-level=info
+
[ui]
#
# Describes the format for each row in a mailbox view. This field is compatible
diff --git a/config/binds.go b/config/binds.go
index 63fc335a..4b32e6da 100644
--- a/config/binds.go
+++ b/config/binds.go
@@ -72,7 +72,7 @@ func (config *AercConfig) parseBinds(root string) error {
filename := path.Join(root, "binds.conf")
if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) {
- logging.Debugf("%s not found, installing the system default", filename)
+ fmt.Printf("%s not found, installing the system default", filename)
if err := installTemplate(root, "binds.conf"); err != nil {
return err
}
diff --git a/config/config.go b/config/config.go
index 049b305f..c4794cc1 100644
--- a/config/config.go
+++ b/config/config.go
@@ -2,6 +2,7 @@ package config
import (
"errors"
+ "fmt"
"log"
"os"
"path"
@@ -11,8 +12,6 @@ import (
"github.com/go-ini/ini"
"github.com/kyoh86/xdg"
"github.com/mitchellh/go-homedir"
-
- "git.sr.ht/~rjarry/aerc/logging"
)
type AercConfig struct {
@@ -119,14 +118,12 @@ func LoadConfigFromFile(root *string, accts []string) (*AercConfig, error) {
// if it doesn't exist copy over the template, then load
if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) {
- logging.Debugf("%s not found, installing the system default", filename)
+ fmt.Printf("%s not found, installing the system default", filename)
if err := installTemplate(*root, "aerc.conf"); err != nil {
return nil, err
}
}
- logging.Infof("Parsing configuration from %s", filename)
-
file, err := ini.LoadSources(ini.LoadOptions{
KeyValueDelimiters: "=",
}, filename)
@@ -147,6 +144,9 @@ func LoadConfigFromFile(root *string, accts []string) (*AercConfig, error) {
Openers: make(map[string][]string),
}
+ if err := config.parseGeneral(file); err != nil {
+ return nil, err
+ }
if err := config.parseFilters(file); err != nil {
return nil, err
}
@@ -168,9 +168,6 @@ func LoadConfigFromFile(root *string, accts []string) (*AercConfig, error) {
if err := config.parseUi(file); err != nil {
return nil, err
}
- if err := config.parseGeneral(file); err != nil {
- return nil, err
- }
if err := config.parseTemplates(file); err != nil {
return nil, err
}
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
}