File: syslogconfig.go

package info (click to toggle)
golang-github-hlandau-dexlogconfig 0.0~git20161112.0.244f29b-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 88 kB
  • sloc: makefile: 2
file content (114 lines) | stat: -rw-r--r-- 2,806 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// +build !windows

package dexlogconfig

import "github.com/hlandau/xlog"
import "gopkg.in/hlandau/easyconfig.v1/cflag"
import "log/syslog"
import "strings"
import "gopkg.in/hlandau/svcutils.v1/exepath"
import "github.com/coreos/go-systemd/journal"
import "fmt"

var (
	syslogFacilityFlag  = cflag.String(flagGroup, "facility", "daemon", "Syslog facility to use")
	syslogFlag          = cflag.Bool(flagGroup, "syslog", false, "Log to syslog?")
	syslogSeverityFlag  = cflag.String(flagGroup, "syslogseverity", "DEBUG", "Syslog severity limit")
	journalFlag         = cflag.Bool(flagGroup, "journal", false, "Log to systemd journal?")
	journalSeverityFlag = cflag.String(flagGroup, "journalseverity", "DEBUG", "Systemd journal severity limit")
)

var facilities = map[string]syslog.Priority{
	"kern":     syslog.LOG_KERN,
	"user":     syslog.LOG_USER,
	"mail":     syslog.LOG_MAIL,
	"daemon":   syslog.LOG_DAEMON,
	"auth":     syslog.LOG_AUTH,
	"syslog":   syslog.LOG_SYSLOG,
	"lpr":      syslog.LOG_LPR,
	"news":     syslog.LOG_NEWS,
	"uucp":     syslog.LOG_UUCP,
	"cron":     syslog.LOG_CRON,
	"authpriv": syslog.LOG_AUTHPRIV,
	"ftp":      syslog.LOG_FTP,
	"local0":   syslog.LOG_LOCAL0,
	"local1":   syslog.LOG_LOCAL1,
	"local2":   syslog.LOG_LOCAL2,
	"local3":   syslog.LOG_LOCAL3,
	"local4":   syslog.LOG_LOCAL4,
	"local5":   syslog.LOG_LOCAL5,
	"local6":   syslog.LOG_LOCAL6,
	"local7":   syslog.LOG_LOCAL7,
}

func openSyslog() {
	if !syslogFlag.Value() {
		return
	}

	syslogFacility := syslogFacilityFlag.Value()
	f, ok := facilities[strings.ToLower(syslogFacility)]
	if !ok {
		return
	}

	pn := exepath.ProgramName
	if pn == "" {
		pn = "unknown"
	}

	w, err := syslog.New(f|syslog.LOG_DEBUG, pn)
	if err != nil {
		return
	}

	sink := xlog.NewSyslogSink(w)

	if sev, ok := xlog.ParseSeverity(syslogSeverityFlag.Value()); ok {
		sink.SetSeverity(sev)
	}

	xlog.RootSink.Add(sink)
}

func openJournal() {
	if !journalFlag.Value() || !journal.Enabled() {
		return
	}

	jsink.Tags = map[string]string{
		"SYSLOG_FACILITY": syslogFacilityFlag.Value(),
	}

	if exepath.ProgramName != "" {
		jsink.Tags["SYSLOG_TAG"] = exepath.ProgramName
	}

	if sev, ok := xlog.ParseSeverity(journalSeverityFlag.Value()); ok {
		jsink.MinSeverity = sev
	}

	xlog.RootSink.Add(&jsink)
}

type journalSink struct {
	Tags        map[string]string
	MinSeverity xlog.Severity
}

func (s *journalSink) ReceiveLocally(sev xlog.Severity, format string, params ...interface{}) {
	s.ReceiveFromChild(sev, format, params...)
}

func (s *journalSink) ReceiveFromChild(sev xlog.Severity, format string, params ...interface{}) {
	if sev > s.MinSeverity {
		return
	}

	journal.Send(fmt.Sprintf(format, params...), journal.Priority(sev.Syslog()), s.Tags)
	// ignore errors
}

var jsink = journalSink{
	MinSeverity: xlog.SevDebug,
}