logging_log_formatter_json.go 557 B

12345678910111213141516171819202122232425262728
  1. package fairwind
  2. import "encoding/json"
  3. type LogFormatterJSON struct {
  4. }
  5. func NewLogFormatterJSON() *LogFormatterJSON {
  6. return &LogFormatterJSON{}
  7. }
  8. func (this *LogFormatterJSON) Format(line LogLine) (string, error) {
  9. vector := map[string]any{
  10. "severity": SeverityToString(line.Severity),
  11. "timestamp": line.Timestamp,
  12. "message": line.Message,
  13. }
  14. for _, parameter := range line.Parameters {
  15. vector[parameter.Key] = parameter.Value
  16. }
  17. buffer, err := json.Marshal(vector)
  18. if err != nil {
  19. return "", err
  20. }
  21. return string(buffer), nil
  22. }