formatter.go 911 B

1234567891011121314151617181920212223242526272829
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package syslogexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/syslogexporter"
  4. import (
  5. "go.opentelemetry.io/collector/pdata/plog"
  6. )
  7. func createFormatter(protocol string) formatter {
  8. if protocol == protocolRFC5424Str {
  9. return newRFC5424Formatter()
  10. }
  11. return newRFC3164Formatter()
  12. }
  13. type formatter interface {
  14. format(plog.LogRecord) string
  15. }
  16. // getAttributeValueOrDefault returns the value of the requested log record's attribute as a string.
  17. // If the attribute was not found, it returns the provided default value.
  18. func getAttributeValueOrDefault(logRecord plog.LogRecord, attributeName string, defaultValue string) string {
  19. value := defaultValue
  20. if attributeValue, found := logRecord.Attributes().Get(attributeName); found {
  21. value = attributeValue.AsString()
  22. }
  23. return value
  24. }