Skip to content

Commit

Permalink
Allow setting different log levels for log tags
Browse files Browse the repository at this point in the history
  • Loading branch information
a-hassanin committed Jan 19, 2024
1 parent 1666181 commit 316abf3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
5 changes: 4 additions & 1 deletion jobs/bosh-dns-windows/spec
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ properties:
logging.format.timestamp:
description: "Format for the timestamp in the component logs. Valid values are 'rfc3339' and 'deprecated'."
default: "rfc3339"


logging.tags:
description: "Provide logging configuration for log tags to overwrite the default logger behavior (e.g log_level property)"

post_start.timeout:
description: "Number of seconds to attempt to retry Resolve-DnsName before timing out the post-start script"
default: 60
3 changes: 3 additions & 0 deletions jobs/bosh-dns/spec
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,6 @@ properties:
logging.format.timestamp:
description: "Format for the timestamp in the component logs. Valid values are 'rfc3339' and 'deprecated'."
default: "rfc3339"

logging.tags:
description: "Provide logging configuration for log tags to overwrite the default logger behavior (e.g log_level property)"
3 changes: 2 additions & 1 deletion jobs/bosh-dns/templates/config.json.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
logging: {
format: {
timestamp: p('logging.format.timestamp')
}
},
tags: p('logging.tags')
}
}.to_json
%>
22 changes: 22 additions & 0 deletions src/bosh-dns/dns/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ func (c Config) GetLogLevel() (boshlog.LogLevel, error) {
return level, nil
}

func (c Config) GetLoggingTags() []boshlog.LogTag {
var tags []boshlog.LogTag

for _, logTag := range c.Logging.Tags {
loggerLevelValue, err := boshlog.Levelify(logTag.LogLevel)
if err != nil {
continue
}
tags = append(tags, boshlog.LogTag{
Name: logTag.Name,
LogLevel: loggerLevelValue,
})
}
return tags
}

func (c Config) UseRFC3339Formatting() bool {
return strings.EqualFold(c.Logging.Format.TimeStamp, RFCFormatting)
}
Expand Down Expand Up @@ -91,8 +107,14 @@ type InternalUpcheckDomain struct {
DNSQuery string `json:"dns_query"`
}

type LogTag struct {
Name string `json:"name"`
LogLevel string `json:"log_level"`
}

type LoggingConfig struct {
Format FormatConfig `json:"format,omitempty"`
Tags []LogTag `json:"tags,omitempty"`
}

type FormatConfig struct {
Expand Down
14 changes: 14 additions & 0 deletions src/bosh-dns/dns/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,20 @@ var _ = Describe("Config", func() {
})
})

Context("LoggingTags", func() {
It("returns levelified logging tags", func() {
configFilePath := writeConfigFile(`{"address": "127.0.0.1", "port": 53, "logging":{"tags": [
{"name": "ForwardHandler", "log_level": "DEBUG"}, {"name": "TEST", "log_level": "INVALID"},
{"name": "main", "log_level": "ERROR"}]}}`)

dnsConfig, err := config.LoadFromFile(configFilePath)
Expect(err).ToNot(HaveOccurred())

Expect(dnsConfig.GetLoggingTags()).To(Equal(
[]boshlog.LogTag{{"ForwardHandler", 0}, {"main", 3}}))

Check failure on line 403 in src/bosh-dns/dns/config/config_test.go

View workflow job for this annotation

GitHub Actions / lint_bosh_dns (ubuntu-latest)

composites: github.com/cloudfoundry/bosh-utils/logger.LogTag struct literal uses unkeyed fields (govet)
})
})

Context("AppendDefaultDNSPortIfMissing", func() {
It("allows multiple recursors to be configured with default port of 53", func() {
recursors, err := config.AppendDefaultDNSPortIfMissing([]string{"8.8.8.8", "10.244.4.4:9700", "2001:db8::1", "[2001:db8::1]:1234"})
Expand Down
1 change: 1 addition & 0 deletions src/bosh-dns/dns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func mainExitCode() int {
if config.UseRFC3339Formatting() {
logger.UseRFC3339Timestamps()
}
logger.UseTags(config.GetLoggingTags())
logTag := "main"
logger.Info(logTag, "bosh-dns starting")
defer logger.FlushTimeout(5 * time.Second) //nolint:errcheck
Expand Down

0 comments on commit 316abf3

Please sign in to comment.