Create a distinct log section in config

This commit is contained in:
Igor Perepilitsyn 2022-09-11 21:37:23 +02:00
parent 7247302f45
commit dd155dca97
9 changed files with 62 additions and 26 deletions

View file

@ -47,7 +47,7 @@ func initConfig() {
machineOutput := HasMachineOutputFlag() machineOutput := HasMachineOutputFlag()
zerolog.SetGlobalLevel(cfg.LogLevel) zerolog.SetGlobalLevel(cfg.Log.Level)
// If the user has requested a "machine" readable format, // If the user has requested a "machine" readable format,
// then disable login so the output remains valid. // then disable login so the output remains valid.
@ -55,7 +55,7 @@ func initConfig() {
zerolog.SetGlobalLevel(zerolog.Disabled) zerolog.SetGlobalLevel(zerolog.Disabled)
} }
if cfg.JSONLogs { if cfg.Log.Format == headscale.JSONLogFormat {
log.Logger = log.Output(os.Stdout) log.Logger = log.Output(os.Stdout)
} }

View file

@ -172,8 +172,10 @@ tls_letsencrypt_listen: ":http"
tls_cert_path: "" tls_cert_path: ""
tls_key_path: "" tls_key_path: ""
log_level: info log:
json_logs: false # Output formatting for logs: text or json
format: text
level: info
# Path to a file containg ACL policies. # Path to a file containg ACL policies.
# ACLs can be defined as YAML or HUJSON. # ACLs can be defined as YAML or HUJSON.

View file

@ -22,6 +22,9 @@ import (
const ( const (
tlsALPN01ChallengeType = "TLS-ALPN-01" tlsALPN01ChallengeType = "TLS-ALPN-01"
http01ChallengeType = "HTTP-01" http01ChallengeType = "HTTP-01"
JSONLogFormat = "json"
TextLogFormat = "text"
) )
// Config contains the initial Headscale configuration. // Config contains the initial Headscale configuration.
@ -37,8 +40,7 @@ type Config struct {
PrivateKeyPath string PrivateKeyPath string
NoisePrivateKeyPath string NoisePrivateKeyPath string
BaseDomain string BaseDomain string
LogLevel zerolog.Level Log LogConfig
JSONLogs bool
DisableUpdateCheck bool DisableUpdateCheck bool
DERP DERPConfig DERP DERPConfig
@ -125,6 +127,11 @@ type ACLConfig struct {
PolicyPath string PolicyPath string
} }
type LogConfig struct {
Format string
Level zerolog.Level
}
func LoadConfig(path string, isFile bool) error { func LoadConfig(path string, isFile bool) error {
if isFile { if isFile {
viper.SetConfigFile(path) viper.SetConfigFile(path)
@ -148,8 +155,8 @@ func LoadConfig(path string, isFile bool) error {
viper.SetDefault("tls_letsencrypt_challenge_type", http01ChallengeType) viper.SetDefault("tls_letsencrypt_challenge_type", http01ChallengeType)
viper.SetDefault("tls_client_auth_mode", "relaxed") viper.SetDefault("tls_client_auth_mode", "relaxed")
viper.SetDefault("log_level", "info") viper.SetDefault("log.level", "info")
viper.SetDefault("json_logs", false) viper.SetDefault("log.format", TextLogFormat)
viper.SetDefault("dns_config", nil) viper.SetDefault("dns_config", nil)
@ -336,6 +343,34 @@ func GetACLConfig() ACLConfig {
} }
} }
func GetLogConfig() LogConfig {
logLevelStr := viper.GetString("log.level")
logLevel, err := zerolog.ParseLevel(logLevelStr)
if err != nil {
logLevel = zerolog.DebugLevel
}
logFormatOpt := viper.GetString("log.format")
var logFormat string
switch logFormatOpt {
case "json":
logFormat = JSONLogFormat
case "text":
logFormat = TextLogFormat
case "":
logFormat = TextLogFormat
default:
log.Error().
Str("func", "GetLogConfig").
Msgf("Could not parse log format: %s. Valid choices are 'json' or 'text'", logFormatOpt)
}
return LogConfig{
Format: logFormat,
Level: logLevel,
}
}
func GetDNSConfig() (*tailcfg.DNSConfig, string) { func GetDNSConfig() (*tailcfg.DNSConfig, string) {
if viper.IsSet("dns_config") { if viper.IsSet("dns_config") {
dnsConfig := &tailcfg.DNSConfig{} dnsConfig := &tailcfg.DNSConfig{}
@ -432,13 +467,6 @@ func GetHeadscaleConfig() (*Config, error) {
configuredPrefixes := viper.GetStringSlice("ip_prefixes") configuredPrefixes := viper.GetStringSlice("ip_prefixes")
parsedPrefixes := make([]netip.Prefix, 0, len(configuredPrefixes)+1) parsedPrefixes := make([]netip.Prefix, 0, len(configuredPrefixes)+1)
logLevelStr := viper.GetString("log_level")
logLevel, err := zerolog.ParseLevel(logLevelStr)
if err != nil {
logLevel = zerolog.DebugLevel
}
jsonLogs := viper.GetBool("json_logs")
legacyPrefixField := viper.GetString("ip_prefix") legacyPrefixField := viper.GetString("ip_prefix")
if len(legacyPrefixField) > 0 { if len(legacyPrefixField) > 0 {
log. log.
@ -491,8 +519,6 @@ func GetHeadscaleConfig() (*Config, error) {
GRPCAddr: viper.GetString("grpc_listen_addr"), GRPCAddr: viper.GetString("grpc_listen_addr"),
GRPCAllowInsecure: viper.GetBool("grpc_allow_insecure"), GRPCAllowInsecure: viper.GetBool("grpc_allow_insecure"),
DisableUpdateCheck: viper.GetBool("disable_check_updates"), DisableUpdateCheck: viper.GetBool("disable_check_updates"),
LogLevel: logLevel,
JSONLogs: jsonLogs,
IPPrefixes: prefixes, IPPrefixes: prefixes,
PrivateKeyPath: AbsolutePathFromConfigPath( PrivateKeyPath: AbsolutePathFromConfigPath(
@ -554,5 +580,7 @@ func GetHeadscaleConfig() (*Config, error) {
}, },
ACL: GetACLConfig(), ACL: GetACLConfig(),
Log: GetLogConfig(),
}, nil }, nil
} }

View file

@ -28,8 +28,9 @@ ip_prefixes:
- fd7a:115c:a1e0::/48 - fd7a:115c:a1e0::/48
- 100.64.0.0/10 - 100.64.0.0/10
listen_addr: 0.0.0.0:18080 listen_addr: 0.0.0.0:18080
log_level: disabled log:
json_logs: false level: disabled
format: text
logtail: logtail:
enabled: false enabled: false
metrics_listen_addr: 127.0.0.1:19090 metrics_listen_addr: 127.0.0.1:19090

View file

@ -1,4 +1,5 @@
log_level: trace log:
level: trace
acl_policy_path: "" acl_policy_path: ""
db_type: sqlite3 db_type: sqlite3
ephemeral_node_inactivity_timeout: 30m ephemeral_node_inactivity_timeout: 30m

View file

@ -27,8 +27,9 @@ ip_prefixes:
- fd7a:115c:a1e0::/48 - fd7a:115c:a1e0::/48
- 100.64.0.0/10 - 100.64.0.0/10
listen_addr: 0.0.0.0:18080 listen_addr: 0.0.0.0:18080
log_level: disabled log:
json_logs: false level: disabled
format: text
logtail: logtail:
enabled: false enabled: false
metrics_listen_addr: 127.0.0.1:19090 metrics_listen_addr: 127.0.0.1:19090

View file

@ -1,4 +1,5 @@
log_level: trace log:
level: trace
acl_policy_path: "" acl_policy_path: ""
db_type: sqlite3 db_type: sqlite3
ephemeral_node_inactivity_timeout: 30m ephemeral_node_inactivity_timeout: 30m

View file

@ -28,8 +28,9 @@ ip_prefixes:
- fd7a:115c:a1e0::/48 - fd7a:115c:a1e0::/48
- 100.64.0.0/10 - 100.64.0.0/10
listen_addr: 0.0.0.0:8080 listen_addr: 0.0.0.0:8080
log_level: disabled log:
json_logs: false format: text
level: disabled
logtail: logtail:
enabled: false enabled: false
metrics_listen_addr: 127.0.0.1:9090 metrics_listen_addr: 127.0.0.1:9090

View file

@ -1,4 +1,5 @@
log_level: trace log:
level: trace
acl_policy_path: "" acl_policy_path: ""
db_type: sqlite3 db_type: sqlite3
ephemeral_node_inactivity_timeout: 30m ephemeral_node_inactivity_timeout: 30m