Debian Codegraph @

zap_development_handler.go

raw symbols SCIP
1 package humanlog
2
3 import (
4 "regexp"
5 "strings"
6 "time"
7
8 "github.com/humanlogio/humanlog/internal/pkg/model"
9 )
10
11 // Zap Development logs are made up of the following separated by whitespace
12 // 1. timestamp in ISO-8601 (??)
13 // 2. Log Level (one of DEBUG ERROR INFO WARN FATAL)
14 // 3. Caller Location in the source
15 // 4. The main logged message
16 // 5. a JSON object containing the structured k/v pairs
17 // 6. optional context lines - but since they are on a separate line the main
18 // scanner loop will never capture them
19 var zapDevLogsPrefixRe = regexp.MustCompile("^(?P<timestamp>\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}-\\d{4})\\s+(?P<level>\\w{4,5})\\s+(?P<location>\\S+)\\s+(?P<message>[^{]+?)\\s+(?P<jsonbody>{.+})$")
20
21 // Zap Development Logs when run in Docker-Compose are nearly identical to before
22 // Fields are tab separated instead of whitespace
23 // Timestamp is now in ...
24 // Everything else remains the same
25 var zapDevDCLogsPrefixRe = regexp.MustCompile("^(?P<timestamp>\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z)\t(?P<level>\\w{4,5})\t(?P<location>\\S+)\t(?P<message>[^{]+?)\t(?P<jsonbody>{.+})$")
26
27 // This is not obviously an RFC-compliant format and is not a constant in the
28 // time package which is worrisome but this pattern does work.
29 const someRFC = "2006-01-02T15:04:05.000-0700"
30
31 func tryZapDevPrefix(d []byte, ev *model.Structured, handler *JSONHandler) bool {
32 if matches := zapDevLogsPrefixRe.FindSubmatch(d); matches != nil {
33 if handler.TryHandle(matches[5], ev) {
34 t, err := time.Parse(someRFC, string(matches[1]))
35 if err != nil {
36 return false
37 }
38 ev.Time = t
39 ev.Level = strings.ToLower(string(matches[2]))
40 ev.Msg = string(matches[4])
41 ev.KVs = append(ev.KVs, model.KV{
42 Key: "caller", Value: string(matches[3]),
43 })
44 return true
45 }
46 }
47 return false
48 }
49
50 // This is not obviously an RFC-compliant format and is not a constant in the
51 // time package which is worrisome but this pattern does work.
52 const someOtherRFC = "2006-01-02T15:04:05.000Z"
53
54 func tryZapDevDCPrefix(d []byte, ev *model.Structured, handler *JSONHandler) bool {
55 if matches := zapDevDCLogsPrefixRe.FindSubmatch(d); matches != nil {
56 if handler.TryHandle(matches[5], ev) {
57 t, err := time.Parse(someOtherRFC, string(matches[1]))
58 if err != nil {
59 return false
60 }
61 ev.Time = t
62 ev.Level = strings.ToLower(string(matches[2]))
63 ev.Msg = string(matches[4])
64 ev.KVs = append(
65 ev.KVs,
66 model.KV{Key: "caller", Value: string(matches[3])},
67 )
68 return true
69 }
70 }
71 return false
72 }