Skip to content

Commit fabdd73

Browse files
committed
Add support for logging to syslog.
1 parent 9ad39b7 commit fabdd73

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

cmd/chirpstack-gateway-bridge/cmd/configfile.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import (
1212
// when updating this template, don't forget to update config.md!
1313
const configTemplate = `[general]
1414
# debug=5, info=4, warning=3, error=2, fatal=1, panic=0
15-
log_level = {{ .General.LogLevel }}
15+
log_level={{ .General.LogLevel }}
16+
17+
# Log to syslog.
18+
#
19+
# When set to true, log messages are being written to syslog.
20+
log_to_syslog={{ .General.LogToSyslog }}
1621
1722
1823
# Filters.

cmd/chirpstack-gateway-bridge/cmd/root_run.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package cmd
22

33
import (
4+
"log/syslog"
45
"os"
56
"os/signal"
67
"syscall"
78

89
"github.com/pkg/errors"
910
log "github.com/sirupsen/logrus"
11+
lsyslog "github.com/sirupsen/logrus/hooks/syslog"
1012
"github.com/spf13/cobra"
1113

1214
"github.com/brocaar/chirpstack-gateway-bridge/internal/backend"
@@ -23,6 +25,7 @@ func run(cmd *cobra.Command, args []string) error {
2325

2426
tasks := []func() error{
2527
setLogLevel,
28+
setSyslog,
2629
printStartMessage,
2730
setupFilters,
2831
setupBackend,
@@ -52,6 +55,38 @@ func setLogLevel() error {
5255
return nil
5356
}
5457

58+
func setSyslog() error {
59+
if !config.C.General.LogToSyslog {
60+
return nil
61+
}
62+
63+
var prio syslog.Priority
64+
65+
switch log.StandardLogger().Level {
66+
case log.DebugLevel:
67+
prio = syslog.LOG_USER | syslog.LOG_DEBUG
68+
case log.InfoLevel:
69+
prio = syslog.LOG_USER | syslog.LOG_INFO
70+
case log.WarnLevel:
71+
prio = syslog.LOG_USER | syslog.LOG_WARNING
72+
case log.ErrorLevel:
73+
prio = syslog.LOG_USER | syslog.LOG_ERR
74+
case log.FatalLevel:
75+
prio = syslog.LOG_USER | syslog.LOG_CRIT
76+
case log.PanicLevel:
77+
prio = syslog.LOG_USER | syslog.LOG_CRIT
78+
}
79+
80+
hook, err := lsyslog.NewSyslogHook("", "", prio, "chirpstack-gateway-bridge")
81+
if err != nil {
82+
return errors.Wrap(err, "get syslog hook error")
83+
}
84+
85+
log.AddHook(hook)
86+
87+
return nil
88+
}
89+
5590
func printStartMessage() error {
5691
log.WithFields(log.Fields{
5792
"version": version,

docs/content/install/config.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ Example configuration file:
6464
{{<highlight toml>}}
6565
[general]
6666
# debug=5, info=4, warning=3, error=2, fatal=1, panic=0
67-
log_level = 4
67+
log_level=4
68+
69+
# Log to syslog.
70+
#
71+
# When set to true, log messages are being written to syslog.
72+
log_to_syslog=false
6873

6974

7075
# Filters.
@@ -145,10 +150,10 @@ type="semtech_udp"
145150
crc_check=true
146151

147152
# Event API URL.
148-
event_url="ipc:///tmp/concentratord_event"
153+
event_url="icp:///tmp/concentratord_event"
149154

150155
# Command API URL.
151-
command_url="ipc:///tmp/concentratord_command"
156+
command_url="icp:///tmp/concentratord_command"
152157

153158

154159
# Basic Station backend.

internal/config/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
// Config defines the configuration structure.
88
type Config struct {
99
General struct {
10-
LogLevel int `mapstructure:"log_level"`
10+
LogLevel int `mapstructure:"log_level"`
11+
LogToSyslog bool `mapstructure:"log_to_syslog"`
1112
}
1213

1314
Filters struct {

0 commit comments

Comments
 (0)