mirror of
https://github.com/foxcpp/maddy.git
synced 2025-04-04 21:47:40 +03:00
log: Implement local syslog support
This commit is contained in:
parent
89ac5d6c67
commit
2ae192e12d
3 changed files with 29 additions and 0 deletions
|
@ -84,6 +84,8 @@ These can be specified only outside of any configuration block.
|
|||
Target can be one of the following:
|
||||
* `stderr`
|
||||
Write logs to stderr, this is the default.
|
||||
* `syslog`
|
||||
Send logs to the local syslog daemon.
|
||||
* `off`
|
||||
Do nothing. Useful to disable logging fully: `log off`
|
||||
* file path
|
||||
|
|
|
@ -135,6 +135,12 @@ func logOutput(m *config.Map, node *config.Node) (interface{}, error) {
|
|||
switch arg {
|
||||
case "stderr":
|
||||
outs = append(outs, log.StderrLog())
|
||||
case "syslog":
|
||||
syslogOut, err := log.Syslog()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to syslog daemon: %v", err)
|
||||
}
|
||||
outs = append(outs, syslogOut)
|
||||
case "off":
|
||||
outs = append(outs, nil)
|
||||
default:
|
||||
|
|
21
log/log.go
21
log/log.go
|
@ -3,6 +3,7 @@ package log
|
|||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log/syslog"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
@ -89,6 +90,26 @@ func WriterLog(w io.Writer) FuncLog {
|
|||
}
|
||||
}
|
||||
|
||||
func Syslog() (FuncLog, error) {
|
||||
w, err := syslog.New(syslog.LOG_MAIL|syslog.LOG_INFO, "maddy")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return func(t time.Time, debug bool, str string) {
|
||||
var err error
|
||||
if debug {
|
||||
err = w.Debug(str)
|
||||
} else {
|
||||
err = w.Info(str)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "!!! Failed to send message to syslog daemon: %v", err)
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
func MultiLog(outs ...FuncLog) FuncLog {
|
||||
return func(t time.Time, debug bool, str string) {
|
||||
for _, out := range outs {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue