aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/admin/vespa-wrapper/logfmt/handleline.go
blob: 813ca82acb46c2a5e2a8091864cd838a957cb50a (plain) (blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
// vespa logfmt command
// Author: arnej

package logfmt

import (
	"bytes"
	"encoding/json"
	"fmt"
	"strconv"
	"strings"
	"time"
)

type logFields struct {
	timestamp string // seconds, optional fractional seconds
	host      string
	pid       string // pid, optional tid
	service   string
	component string
	level     string
	messages  []string
}

// handle a line in "vespa.log" format; do filtering and formatting as specified in opts
func handleLine(opts *Options, line string) (string, error) {
	fieldStrings := strings.SplitN(line, "\t", 7)
	if len(fieldStrings) < 7 {
		return "", fmt.Errorf("not enough fields: '%s'", line)
	}
	fields := logFields{
		timestamp: fieldStrings[0],
		host:      fieldStrings[1],
		pid:       fieldStrings[2],
		service:   fieldStrings[3],
		component: fieldStrings[4],
		level:     fieldStrings[5],
		messages:  fieldStrings[6:],
	}

	if !opts.showLevel(fields.level) {
		return "", nil
	}
	if opts.OnlyHostname != "" && opts.OnlyHostname != fields.host {
		return "", nil
	}
	if opts.OnlyPid != "" && opts.OnlyPid != fields.pid {
		return "", nil
	}
	if opts.OnlyService != "" && opts.OnlyService != fields.service {
		return "", nil
	}
	if opts.OnlyInternal && !isInternal(fields.component) {
		return "", nil
	}
	if opts.ComponentFilter.unmatched(fields.component) {
		return "", nil
	}
	if opts.MessageFilter.unmatched(strings.Join(fields.messages, "\t")) {
		return "", nil
	}

	switch opts.Format {
	case FormatRaw:
		return line + "\n", nil
	case FormatJSON:
		return handleLineJson(opts, &fields)
	case FormatVespa:
		fallthrough
	default:
		return handleLineVespa(opts, &fields)
	}
}

func parseTimestamp(timestamp string) (time.Time, error) {
	secs, err := strconv.ParseFloat(timestamp, 64)
	if err != nil {
		return time.Time{}, err
	}
	nsecs := int64(secs * 1e9)
	return time.Unix(0, nsecs), nil
}

type logFieldsJson struct {
	Timestamp string   `json:"timestamp"`
	Host      string   `json:"host"`
	Pid       string   `json:"pid"`
	Service   string   `json:"service"`
	Component string   `json:"component"`
	Level     string   `json:"level"`
	Messages  []string `json:"messages"`
}

func handleLineJson(_ *Options, fields *logFields) (string, error) {
	timestamp, err := parseTimestamp(fields.timestamp)
	if err != nil {
		return "", err
	}
	outputFields := logFieldsJson{
		Timestamp: timestamp.Format(time.RFC3339Nano),
		Host:      fields.host,
		Pid:       fields.pid,
		Service:   fields.service,
		Component: fields.component,
		Level:     fields.level,
		Messages:  fields.messages,
	}
	buf := bytes.Buffer{}
	if err := json.NewEncoder(&buf).Encode(&outputFields); err != nil {
		return "", err
	}
	return buf.String(), nil
}

func handleLineVespa(opts *Options, fields *logFields) (string, error) {
	var buf strings.Builder

	if opts.showField("fmttime") {
		timestamp, err := parseTimestamp(fields.timestamp)
		if err != nil {
			return "", err
		}
		if opts.showField("usecs") {
			buf.WriteString(timestamp.Format("[2006-01-02 15:04:05.000000] "))
		} else if opts.showField("msecs") {
			buf.WriteString(timestamp.Format("[2006-01-02 15:04:05.000] "))
		} else {
			buf.WriteString(timestamp.Format("[2006-01-02 15:04:05] "))
		}
	} else if opts.showField("time") {
		buf.WriteString(fields.timestamp)
		buf.WriteString(" ")
	}
	if opts.showField("host") {
		buf.WriteString(fmt.Sprintf("%-8s ", fields.host))
	}
	if opts.showField("level") {
		buf.WriteString(fmt.Sprintf("%-7s ", strings.ToUpper(fields.level)))
	}
	if opts.showField("pid") {
		// OnlyPid, _, _ := strings.Cut(pidfield, "/")
		buf.WriteString(fmt.Sprintf("%6s ", fields.pid))
	}
	if opts.showField("service") {
		if opts.TruncateService {
			buf.WriteString(fmt.Sprintf("%-9.9s ", fields.service))
		} else {
			buf.WriteString(fmt.Sprintf("%-16s ", fields.service))
		}
	}
	if opts.showField("component") {
		if opts.TruncateComponent {
			buf.WriteString(fmt.Sprintf("%-15.15s ", fields.component))
		} else {
			buf.WriteString(fmt.Sprintf("%s\t", fields.component))
		}
	}
	if opts.showField("message") {
		var msgBuf strings.Builder
		for idx, message := range fields.messages {
			if idx > 0 {
				msgBuf.WriteString("\n\t")
			}
			if opts.DequoteNewlines {
				message = strings.ReplaceAll(message, "\\n\\t", "\n\t")
				message = strings.ReplaceAll(message, "\\n", "\n\t")
			}
			msgBuf.WriteString(message)
		}
		message := msgBuf.String()
		if strings.Contains(message, "\n") {
			buf.WriteString("\n\t")
		}
		buf.WriteString(message)
	}
	buf.WriteString("\n")
	return buf.String(), nil
}