Debian Codegraph @

docker_compose_handler.go

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