Skip to content

Commit

Permalink
[Enhancement] Add logging configuration options to support log file m…
Browse files Browse the repository at this point in the history
…anagement
  • Loading branch information
nitin-sachdev-29 committed Feb 4, 2025
1 parent 2e4bd84 commit 27d39d5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
37 changes: 27 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ const (
)

type Config struct {
LogLevel string
EnableDebug bool
EnableSyslog bool
SyslogFacility string
LogJSON bool
LogLevel string
EnableDebug bool
EnableSyslog bool
SyslogFacility string
LogJSON bool
LogFile string
LogRotateBytes int
LogRotateMaxFiles int
LogRotateDuration time.Duration

Partition string
Service string
Expand Down Expand Up @@ -134,6 +138,10 @@ func DefaultConfig() (*Config, error) {
PingType: PingTypeUDP,
DisableCoordinateUpdates: false,
Partition: "",
LogFile: "",
LogRotateBytes: 0,
LogRotateMaxFiles: 0,
LogRotateDuration: 0,
}, nil
}

Expand Down Expand Up @@ -165,11 +173,15 @@ type Telemetry struct {

// HumanConfig contains configuration that the practitioner can set
type HumanConfig struct {
LogLevel flags.StringValue `mapstructure:"log_level"`
EnableDebug flags.BoolValue `mapstructure:"enable_debug"`
EnableSyslog flags.BoolValue `mapstructure:"enable_syslog"`
SyslogFacility flags.StringValue `mapstructure:"syslog_facility"`
LogJSON flags.BoolValue `mapstructure:"log_json"`
LogLevel flags.StringValue `mapstructure:"log_level"`
EnableDebug flags.BoolValue `mapstructure:"enable_debug"`
EnableSyslog flags.BoolValue `mapstructure:"enable_syslog"`
SyslogFacility flags.StringValue `mapstructure:"syslog_facility"`
LogJSON flags.BoolValue `mapstructure:"log_json"`
LogFile flags.StringValue `mapstructure:"log_file"`
LogRotateBytes intValue `mapstructure:"log_rotate_bytes"`
LogRotateMaxFiles intValue `mapstructure:"log_rotate_max_files"`
LogRotateDuration flags.DurationValue `mapstructure:"log_rotate_duration"`

InstanceID flags.StringValue `mapstructure:"instance_id"`
Service flags.StringValue `mapstructure:"consul_service"`
Expand Down Expand Up @@ -502,5 +514,10 @@ func MergeConfig(dst *Config, src *HumanConfig) error {

src.PassingThreshold.Merge(&dst.PassingThreshold)
src.CriticalThreshold.Merge(&dst.CriticalThreshold)

src.LogFile.Merge(&dst.LogFile)
src.LogRotateBytes.Merge(&dst.LogRotateBytes)
src.LogRotateMaxFiles.Merge(&dst.LogRotateMaxFiles)
src.LogRotateDuration.Merge(&dst.LogRotateDuration)
return nil
}
23 changes: 13 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ package main
import (
"flag"
"fmt"
"github.com/hashicorp/consul-esm/version"
"github.com/hashicorp/consul/logging"
"github.com/hashicorp/go-hclog"
"github.com/mitchellh/cli"
"io"
"os"
"os/signal"
"strings"
"syscall"

"github.com/hashicorp/consul-esm/version"
"github.com/hashicorp/consul/logging"
"github.com/hashicorp/go-hclog"
"github.com/mitchellh/cli"
)

const (
Expand Down Expand Up @@ -65,11 +64,15 @@ func main() {

// Set up logging.
logConfig := logging.Config{
Name: "consul-esm",
LogLevel: config.LogLevel,
EnableSyslog: config.EnableSyslog,
SyslogFacility: config.SyslogFacility,
LogJSON: config.LogJSON,
Name: "consul-esm",
LogLevel: config.LogLevel,
EnableSyslog: config.EnableSyslog,
SyslogFacility: config.SyslogFacility,
LogJSON: config.LogJSON,
LogFilePath: config.LogFile,
LogRotateBytes: config.LogRotateBytes,
LogRotateMaxFiles: config.LogRotateMaxFiles,
LogRotateDuration: config.LogRotateDuration,
}
logger, err := logging.Setup(logConfig, os.Stdout)
if err != nil {
Expand Down

0 comments on commit 27d39d5

Please sign in to comment.