Skip to content

Commit

Permalink
Add option for logging to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
HoustonPutman committed Nov 14, 2023
1 parent 8e986e6 commit 16f377c
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/apache/solr-operator/version"
"github.com/fsnotify/fsnotify"
zkApi "github.com/pravega/zookeeper-operator/api/v1beta1"
"io"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -60,6 +61,7 @@ var (

// Operator scope
watchNamespaces string
logFile string

// External Operator dependencies
useZookeeperCRD bool
Expand Down Expand Up @@ -90,6 +92,7 @@ func init() {

flag.BoolVar(&useZookeeperCRD, "zk-operator", true, "The operator will not use the zk operator & crd when this flag is set to false.")
flag.StringVar(&watchNamespaces, "watch-namespaces", "", "The comma-separated list of namespaces to watch. If an empty string (default) is provided, the operator will watch the entire Kubernetes cluster.")
flag.StringVar(&logFile, "log-file", "", "A location to write logs to, if provided. This will not stop logs from being printed to stdout.")

flag.BoolVar(&clientSkipVerify, "tls-skip-verify-server", true, "Controls whether a client verifies the server's certificate chain and host name. If true (insecure), TLS accepts any certificate presented by the server and any host name in that certificate.")
flag.StringVar(&clientCertPath, "tls-client-cert-path", "", "Path where a TLS client cert can be found")
Expand Down Expand Up @@ -118,9 +121,25 @@ func main() {
flag.BoolVar(&enableLeaderElection, "leader-elect", true,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")

opts := zap.Options{
Development: true,
}

// Log to a file if requested by the user
logFile = strings.TrimSpace(logFile)
if logFile != "" {
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
if err != nil {
panic(err)
}
opts.DestWriter = io.MultiWriter(os.Stderr, file)
defer func() {
file.Sync()
file.Close()
}()
}

opts.BindFlags(flag.CommandLine)
flag.Parse()

Expand Down

0 comments on commit 16f377c

Please sign in to comment.