| 49 | } |
| 50 | |
| 51 | func contentFormatter(group string) syslog.Formatter { |
| 52 | return func(p syslog.Priority, hostname, tag, content string) string { |
| 53 | timestamp := time.Now() |
| 54 | service := "convox/syslog" |
| 55 | container := "unknown" |
| 56 | message := content |
| 57 | |
| 58 | if parts := strings.SplitN(content, " ", 3); len(parts) == 3 { |
| 59 | if pp := strings.SplitN(parts[0], "/", 3); len(pp) == 3 { |
| 60 | service = fmt.Sprintf("%s/%s", pp[0], pp[1]) |
| 61 | cp := strings.Split(pp[2], "-") |
| 62 | container = cp[len(cp)-1] |
| 63 | } |
| 64 | |
| 65 | if i, err := strconv.ParseInt(parts[1], 10, 64); err == nil { |
| 66 | sec := i / 1000 |
| 67 | nsec := i - (sec * 1000) |
| 68 | timestamp = time.Unix(sec, nsec).UTC() |
| 69 | } |
| 70 | |
| 71 | message = parts[2] |
| 72 | } |
| 73 | |
| 74 | line := os.Getenv("SYSLOG_FORMAT") |
| 75 | |
| 76 | line = strings.ReplaceAll(line, "{DATE}", timestamp.Format(time.RFC3339)) |
| 77 | line = strings.ReplaceAll(line, "{GROUP}", group) |
| 78 | line = strings.ReplaceAll(line, "{SERVICE}", service) |
| 79 | line = strings.ReplaceAll(line, "{CONTAINER}", container) |
| 80 | line = strings.ReplaceAll(line, "{MESSAGE}", message) |
| 81 | |
| 82 | for newline := true; newline; newline = strings.HasSuffix(line, "\n") { |
| 83 | line = strings.TrimSuffix(line, "\n") |
| 84 | } |
| 85 | |
| 86 | return line + "\n" |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | func main() { |
| 91 | lambda.Start(Handler) |