Skip to content

Commit b24d40b

Browse files
authored
Optional command line flag to disable zerolog journald logger (#15)
1 parent 8e91cff commit b24d40b

File tree

9 files changed

+68
-47
lines changed

9 files changed

+68
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.idea/
2+
netbootd

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,19 @@ Usage:
214214
netbootd server [flags]
215215

216216
Flags:
217-
-a, --address string IP address to listen on (DHCP, TFTP, HTTP)
218-
-r, --api-port int HTTP API port to listen on (default 8081)
219-
-h, --help help for server
220-
-p, --http-port int HTTP port to listen on (default 8080)
221-
-i, --interface string interface to listen on, e.g. eth0 (DHCP)
222-
-m, --manifests string load manifests from directory
217+
-a, --address string IP address to listen on (DHCP, TFTP, HTTP)
218+
-r, --api-port int HTTP API port to listen on (default 8081)
219+
--api-tls-cert string Path to TLS certificate API
220+
--api-tls-key string Path to TLS certificate for API
221+
-h, --help help for server
222+
-p, --http-port int HTTP port to listen on (default 8080)
223+
-i, --interface string interface to listen on, e.g. eth0 (DHCP)
224+
-m, --manifests string load manifests from directory
225+
226+
Global Flags:
227+
-d, --debug enable debug logging
228+
--disable-journal-logger disable zerolog journald logger
229+
--trace enable trace logging
223230
```
224231
225232
Run e.g. `./netbootd --trace server -m ./examples/`

cmd/arpinject.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package cmd
22

33
import (
4+
"net"
5+
6+
"github.com/DSpeichert/netbootd/config"
47
"github.com/DSpeichert/netbootd/dhcpd/arp"
58
"github.com/rs/zerolog/log"
69
"github.com/spf13/cobra"
7-
"net"
810
)
911

1012
var (
@@ -24,6 +26,7 @@ func init() {
2426
var arpInjectCmd = &cobra.Command{
2527
Use: "arpinject",
2628
Run: func(cmd *cobra.Command, args []string) {
29+
config.InitZeroLog()
2730
parsedIp := net.ParseIP(ip)
2831
parsedMac, err := net.ParseMAC(mac)
2932
if err != nil {

cmd/root.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package cmd
22

33
import (
44
"fmt"
5+
"os"
6+
57
"github.com/DSpeichert/netbootd/config"
68
"github.com/spf13/cobra"
79
"github.com/spf13/viper"
8-
"os"
910
)
1011

1112
var (
@@ -23,6 +24,9 @@ func init() {
2324

2425
rootCmd.PersistentFlags().BoolVar(&trace, "trace", false, "enable trace logging")
2526
viper.BindPFlag("trace", rootCmd.Flags().Lookup("trace"))
27+
28+
rootCmd.PersistentFlags().BoolVar(&config.ZeroLogJournalDEnabled, "disable-journal-logger", false, "disable zerolog journald logger")
29+
viper.BindPFlag("disable-journal-logger", rootCmd.Flags().Lookup("disable-journal-logger"))
2630
}
2731

2832
var rootCmd = &cobra.Command{
@@ -36,6 +40,7 @@ allows for complete flexibility in provisioning machines.`,
3640

3741
func Execute() {
3842
if err := rootCmd.Execute(); err != nil {
43+
config.InitZeroLog()
3944
fmt.Println(err)
4045
os.Exit(1)
4146
}

cmd/server.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package cmd
22

33
import (
4+
"net"
5+
"os"
6+
"os/signal"
7+
48
"github.com/DSpeichert/netbootd/api"
9+
"github.com/DSpeichert/netbootd/config"
510
"github.com/DSpeichert/netbootd/dhcpd"
611
"github.com/DSpeichert/netbootd/httpd"
712
"github.com/DSpeichert/netbootd/store"
@@ -11,9 +16,6 @@ import (
1116
"github.com/rs/zerolog/log"
1217
"github.com/spf13/cobra"
1318
"github.com/spf13/viper"
14-
"net"
15-
"os"
16-
"os/signal"
1719
)
1820

1921
var (
@@ -55,6 +57,7 @@ var serverCmd = &cobra.Command{
5557
Use: "server",
5658
Run: func(cmd *cobra.Command, args []string) {
5759
// configure logging
60+
config.InitZeroLog()
5861
if viper.GetBool("trace") {
5962
zerolog.SetGlobalLevel(zerolog.TraceLevel)
6063
} else if viper.GetBool("debug") {

config/viper.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func InitConfig() {
2323

2424
err := viper.ReadInConfig()
2525
if err != nil {
26+
InitZeroLog()
2627
log.Debug().
2728
Err(err).
2829
Msg("error reading config file")

config/zerolog.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package config
2+
3+
import (
4+
"io"
5+
"os"
6+
7+
"github.com/rs/zerolog"
8+
"github.com/rs/zerolog/journald"
9+
"github.com/rs/zerolog/log"
10+
)
11+
12+
var (
13+
zerologInitDone bool // to prevent zerolog to be initialized twice in specific situations (like parsing error of viper configuration file)
14+
ZeroLogJournalDEnabled bool // used by viper to store the status of the --disable-journal-logger flag
15+
)
16+
17+
func init() {
18+
// UNIX Time is faster and smaller than most timestamps
19+
// If you set zerolog.TimeFieldFormat to an empty string,
20+
// logs will write with UNIX time
21+
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
22+
}
23+
24+
func InitZeroLog() {
25+
if !zerologInitDone {
26+
if ZeroLogJournalDEnabled {
27+
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
28+
log.Debug().Msg("Enabled console writer")
29+
} else {
30+
journalWriter := journald.NewJournalDWriter()
31+
multi := io.MultiWriter(zerolog.ConsoleWriter{Out: os.Stderr}, journalWriter)
32+
log.Logger = log.Output(multi)
33+
log.Debug().Msg("Enabled journald writer")
34+
}
35+
zerologInitDone = true
36+
}
37+
}

journald_logger.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

main.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,10 @@ package main
22

33
import (
44
"github.com/DSpeichert/netbootd/cmd"
5-
"github.com/rs/zerolog"
6-
"github.com/rs/zerolog/log"
7-
"os"
85
)
96

107
//go:generate protoc proto/*.proto --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative
118

12-
var (
13-
journalLoggerEnabled bool
14-
)
15-
16-
func init() {
17-
// UNIX Time is faster and smaller than most timestamps
18-
// If you set zerolog.TimeFieldFormat to an empty string,
19-
// logs will write with UNIX time
20-
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
21-
if !journalLoggerEnabled {
22-
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
23-
}
24-
}
25-
269
func main() {
2710
cmd.Execute()
2811
}

0 commit comments

Comments
 (0)