forked from humanlogio/humanlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_compose_handler.go
44 lines (39 loc) · 1.24 KB
/
docker_compose_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package humanlog
import (
"regexp"
"github.com/humanlogio/humanlog/internal/pkg/model"
)
// dcLogsPrefixRe parses out a prefix like 'web_1 | ' from docker-compose
// The regex exists of five parts:
// 1. An optional color terminal escape sequence
// 2. The name of the service
// 3. Any number of spaces, and a pipe symbol
// 4. An optional color reset escape sequence
// 5. The rest of the line
var dcLogsPrefixRe = regexp.MustCompile("^(?:\x1b\\[\\d+m)?(?P<service_name>[a-zA-Z0-9._-]+)\\s+\\|(?:\x1b\\[0m)? (?P<rest_of_line>.*)$")
type handler interface {
TryHandle([]byte, *model.Structured) bool
}
func tryDockerComposePrefix(d []byte, ev *model.Structured, nextHandler handler) bool {
matches := dcLogsPrefixRe.FindSubmatch(d)
if matches != nil {
if nextHandler.TryHandle(matches[2], ev) {
ev.KVs = append(ev.KVs, model.KV{
Key: "service", Value: string(matches[1]),
})
return true
}
// The Zap Development handler is only built for `JSONHandler`s so
// short-circuit calls for LogFmtHandlers
switch h := nextHandler.(type) {
case *JSONHandler:
if tryZapDevDCPrefix(matches[2], ev, h) {
ev.KVs = append(ev.KVs, model.KV{
Key: "service", Value: string(matches[1]),
})
return true
}
}
}
return false
}