mirror of
https://github.com/foxcpp/maddy.git
synced 2025-04-04 13:37:41 +03:00
cmd/maddy: Allow to set logging target from command line
Useful to redirect log messages generated before config is parsed.
This commit is contained in:
parent
6ba26d1e4e
commit
5b1622ad48
3 changed files with 22 additions and 5 deletions
|
@ -7,6 +7,7 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/foxcpp/maddy"
|
||||
"github.com/foxcpp/maddy/config"
|
||||
|
@ -16,6 +17,8 @@ import (
|
|||
func main() {
|
||||
configPath := flag.String("config", filepath.Join(ConfigDirectory, "maddy.conf"), "path to configuration file")
|
||||
|
||||
logTargets := flag.String("log", "stderr", "default logging target(s)")
|
||||
|
||||
flag.StringVar(&config.StateDirectory, "state", DefaultStateDirectory, "path to the state directory")
|
||||
flag.StringVar(&config.LibexecDirectory, "libexec", DefaultLibexecDirectory, "path to the libexec directory")
|
||||
flag.StringVar(&config.RuntimeDirectory, "runtime", DefaultRuntimeDirectory, "path to the runtime directory")
|
||||
|
@ -27,6 +30,13 @@ func main() {
|
|||
|
||||
flag.Parse()
|
||||
|
||||
var err error
|
||||
log.DefaultLogger.Out, err = maddy.LogOutputOption(strings.Split(*logTargets, " "))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
if err := ensureDirectoryWritable(config.StateDirectory); err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(2)
|
||||
|
|
12
config.go
12
config.go
|
@ -21,8 +21,12 @@ func logOutput(m *config.Map, node *config.Node) (interface{}, error) {
|
|||
return nil, m.MatchErr("can't declare block here")
|
||||
}
|
||||
|
||||
outs := make([]log.FuncLog, 0, len(node.Args))
|
||||
for _, arg := range node.Args {
|
||||
return LogOutputOption(node.Args)
|
||||
}
|
||||
|
||||
func LogOutputOption(args []string) (log.FuncLog, error) {
|
||||
outs := make([]log.FuncLog, 0, len(args))
|
||||
for _, arg := range args {
|
||||
switch arg {
|
||||
case "stderr":
|
||||
outs = append(outs, log.WriterLog(os.Stderr, false))
|
||||
|
@ -35,7 +39,7 @@ func logOutput(m *config.Map, node *config.Node) (interface{}, error) {
|
|||
}
|
||||
outs = append(outs, syslogOut)
|
||||
case "off":
|
||||
if len(node.Args) != 1 {
|
||||
if len(args) != 1 {
|
||||
return nil, errors.New("'off' can't be combined with other log targets")
|
||||
}
|
||||
return nil, nil
|
||||
|
@ -56,5 +60,5 @@ func logOutput(m *config.Map, node *config.Node) (interface{}, error) {
|
|||
}
|
||||
|
||||
func defaultLogOutput() (interface{}, error) {
|
||||
return log.WriterLog(os.Stderr, false), nil
|
||||
return log.DefaultLogger.Out, nil
|
||||
}
|
||||
|
|
|
@ -34,6 +34,9 @@ IMAP server functionality in one application.
|
|||
It is not recommended to use tmpfs for it since runtime directory is used only
|
||||
for large objects that can't be stored in RAM directly.
|
||||
|
||||
*-log* _targets..._
|
||||
Logging targets. Format is the same as 'log' config directive.
|
||||
|
||||
*-debug*
|
||||
Enable debug log. You want to use it when reporting bugs.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue