File: driver_syslog.go

package info (click to toggle)
incus 6.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,392 kB
  • sloc: sh: 16,313; ansic: 3,121; python: 457; makefile: 337; ruby: 51; sql: 50; lisp: 6
file content (151 lines) | stat: -rw-r--r-- 3,298 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package logging

import (
	"encoding/json"
	"fmt"
	"log/syslog"
	"strings"

	"github.com/lxc/incus/v6/internal/server/state"
	"github.com/lxc/incus/v6/shared/api"
)

var facilityMap = 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,
}

// SyslogLogger represents a syslog logger.
type SyslogLogger struct {
	common
	address  string
	facility syslog.Priority
	network  string
	tag      string
	writer   *syslog.Writer
}

// NewSyslogLogger instantiates a new syslog logger.
func NewSyslogLogger(s *state.State, name string) (*SyslogLogger, error) {
	addr, facility := s.GlobalConfig.LoggingConfigForSyslog(name)
	network, address := parseAddress(addr)

	return &SyslogLogger{
		common:   newCommonLogger(name, s.GlobalConfig),
		address:  address,
		facility: parseFacility(facility),
		network:  network,
		tag:      "incus",
	}, nil
}

func (c *SyslogLogger) write(event api.Event) error {
	msg := fmt.Sprintf("type: %s log: %s", event.Type, string(event.Metadata))
	lvl := "info"

	if event.Type == api.EventTypeLogging {
		logEvent := api.EventLogging{}

		err := json.Unmarshal(event.Metadata, &logEvent)
		if err != nil {
			return err
		}

		lvl = logEvent.Level
	}

	switch strings.ToLower(lvl) {
	case "panic":
		return c.writer.Err(msg)
	case "fatal":
		return c.writer.Err(msg)
	case "error":
		return c.writer.Err(msg)
	case "warn", "warning":
		return c.writer.Warning(msg)
	case "trace":
		return c.writer.Warning(msg)
	case "info":
		return c.writer.Info(msg)
	case "debug":
		return c.writer.Debug(msg)
	}

	return nil
}

// HandleEvent handles the event received from the internal event listener.
func (c *SyslogLogger) HandleEvent(event api.Event) {
	if !c.processEvent(event) {
		return
	}

	_ = c.write(event)
}

// Start starts the syslog logger.
func (c *SyslogLogger) Start() error {
	writer, err := syslog.Dial(c.network, c.address, c.facility, c.tag)
	if err != nil {
		return err
	}

	c.writer = writer
	return nil
}

// Stop cleans up the syslog logger.
func (c *SyslogLogger) Stop() {
	if c.writer != nil {
		_ = c.writer.Close()
	}
}

// Validate checks whether the logger configuration is correct.
func (c *SyslogLogger) Validate() error {
	if c.address == "" {
		return fmt.Errorf("%s: Address cannot be empty", c.name)
	}

	return nil
}

// parseAddress parses a syslog address into two parts: protocol and the address itself.
func parseAddress(address string) (string, string) {
	protocol := "udp"
	port := "514"

	if strings.Contains(address, "://") {
		parts := strings.SplitN(address, "://", 2)
		protocol = parts[0]
		address = parts[1]
	}

	if !strings.Contains(address, ":") {
		address = fmt.Sprintf("%s:%s", address, port)
	}

	return protocol, address
}

// parseFacility parses a string into a syslog.Priority.
func parseFacility(facility string) syslog.Priority {
	facility = strings.ToLower(strings.TrimSpace(facility))

	val, ok := facilityMap[facility]
	if ok {
		return val
	}

	return syslog.LOG_DAEMON
}