| 67 | } |
| 68 | |
| 69 | func (s *syslogWriter) createMessage(message message.Message) (line []byte, err error) { |
| 70 | switch s.format { |
| 71 | case config.LogFormatLJSON: |
| 72 | details := map[string]interface{}{} |
| 73 | for label, value := range message.Labels() { |
| 74 | details[string(label)] = value |
| 75 | } |
| 76 | line, err = json.Marshal( |
| 77 | syslogJsonLine{ |
| 78 | Code: message.Code(), |
| 79 | Message: message.Explanation(), |
| 80 | Details: details, |
| 81 | }, |
| 82 | ) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | case config.LogFormatText: |
| 87 | msg := message.Explanation() |
| 88 | var labels []string |
| 89 | for labelName, labelValue := range message.Labels() { |
| 90 | labels = append(labels, fmt.Sprintf("%s=%s", labelName, labelValue)) |
| 91 | } |
| 92 | if len(labels) > 0 { |
| 93 | msg += fmt.Sprintf(" (%s)", strings.Join(labels, " ")) |
| 94 | } |
| 95 | line = []byte(msg) |
| 96 | default: |
| 97 | return nil, fmt.Errorf("log format not supported: %s", s.format) |
| 98 | } |
| 99 | return line, nil |
| 100 | } |
| 101 | |
| 102 | type syslogJsonLine struct { |
| 103 | Code string `json:"code"` |