-
Notifications
You must be signed in to change notification settings - Fork 649
/
Copy pathjson_logger.go
256 lines (230 loc) · 7.74 KB
/
json_logger.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package logging
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"time"
"github.com/docker/go-units"
"github.com/fahedouch/go-logrotate"
"github.com/fsnotify/fsnotify"
"github.com/containerd/containerd/v2/core/runtime/v2/logging"
"github.com/containerd/log"
"github.com/containerd/nerdctl/v2/pkg/logging/jsonfile"
"github.com/containerd/nerdctl/v2/pkg/logging/tail"
"github.com/containerd/nerdctl/v2/pkg/strutil"
)
var JSONDriverLogOpts = []string{
LogPath,
MaxSize,
MaxFile,
Env,
Labels,
}
type JSONLogger struct {
Opts map[string]string
logger *logrotate.Logger
}
func JSONFileLogOptsValidate(logOptMap map[string]string) error {
for key := range logOptMap {
if !strutil.InStringSlice(JSONDriverLogOpts, key) {
log.L.Warnf("log-opt %s is ignored for json-file log driver", key)
}
}
return nil
}
func (jsonLogger *JSONLogger) Init(dataStore, ns, id string) error {
// Initialize the log file (https://github.com/containerd/nerdctl/issues/1071)
var jsonFilePath string
if logPath, ok := jsonLogger.Opts[LogPath]; ok {
jsonFilePath = logPath
} else {
jsonFilePath = jsonfile.Path(dataStore, ns, id)
}
if err := os.MkdirAll(filepath.Dir(jsonFilePath), 0700); err != nil {
return err
}
if _, err := os.Stat(jsonFilePath); errors.Is(err, os.ErrNotExist) {
if writeErr := os.WriteFile(jsonFilePath, []byte{}, 0600); writeErr != nil {
return writeErr
}
}
return nil
}
func (jsonLogger *JSONLogger) PreProcess(ctx context.Context, dataStore string, config *logging.Config) error {
var jsonFilePath string
if logPath, ok := jsonLogger.Opts[LogPath]; ok {
jsonFilePath = logPath
} else {
jsonFilePath = jsonfile.Path(dataStore, config.Namespace, config.ID)
}
l := &logrotate.Logger{
Filename: jsonFilePath,
}
// MaxBytes is the maximum size in bytes of the log file before it gets
// rotated. If not set, it defaults to 100 MiB.
// see: https://github.com/fahedouch/go-logrotate/blob/6a8beddaea39b2b9c77109d7fa2fe92053c063e5/logrotate.go#L500
if capacity, ok := jsonLogger.Opts[MaxSize]; ok {
var capVal int64
var err error
capVal, err = units.FromHumanSize(capacity)
if err != nil {
return err
}
if capVal <= 0 {
return fmt.Errorf("max-size must be a positive number")
}
l.MaxBytes = capVal
}
maxFile := 1
if maxFileString, ok := jsonLogger.Opts[MaxFile]; ok {
var err error
maxFile, err = strconv.Atoi(maxFileString)
if err != nil {
return err
}
if maxFile < 1 {
return fmt.Errorf("max-file cannot be less than 1")
}
}
// MaxBackups does not include file to write logs to
l.MaxBackups = maxFile - 1
jsonLogger.logger = l
return nil
}
func (jsonLogger *JSONLogger) Process(stdout <-chan string, stderr <-chan string) error {
return jsonfile.Encode(stdout, stderr, jsonLogger.logger)
}
func (jsonLogger *JSONLogger) PostProcess() error {
return nil
}
// Loads log entries from logfiles produced by the json-logger driver and forwards
// them to the provided io.Writers after applying the provided logging options.
func viewLogsJSONFile(lvopts LogViewOptions, stdout, stderr io.Writer, stopChannel chan os.Signal) error {
logFilePath := jsonfile.Path(lvopts.DatastoreRootPath, lvopts.Namespace, lvopts.ContainerID)
if _, err := os.Stat(logFilePath); err != nil {
// FIXME: this is a workaround for the actual issue, not a real solution
// https://github.com/containerd/nerdctl/issues/3187
if errors.Is(err, os.ErrNotExist) {
log.L.Warnf("Racing log file creation. Pausing briefly.")
time.Sleep(200 * time.Millisecond)
_, err = os.Stat(logFilePath)
}
if err != nil {
return fmt.Errorf("failed to stat JSON log file %w", err)
}
}
return viewLogsJSONFileDirect(lvopts, logFilePath, stdout, stderr, stopChannel)
}
// Loads JSON log entries directly from the provided JSON log file.
// If `LogViewOptions.Follow` is provided, it will refresh and re-read the file until
// it receives something through the stopChannel.
func viewLogsJSONFileDirect(lvopts LogViewOptions, jsonLogFilePath string, stdout, stderr io.Writer, stopChannel chan os.Signal) error {
fin, err := os.OpenFile(jsonLogFilePath, os.O_RDONLY, 0400)
if err != nil {
return err
}
defer func() { fin.Close() }()
// Search start point based on tail line.
start, err := tail.FindTailLineStartIndex(fin, lvopts.Tail)
if err != nil {
return fmt.Errorf("failed to tail %d lines of JSON logfile %q: %w", lvopts.Tail, jsonLogFilePath, err)
}
if _, err := fin.Seek(start, io.SeekStart); err != nil {
return fmt.Errorf("failed to seek in log file %q from %d position: %w", jsonLogFilePath, start, err)
}
limitedMode := (lvopts.Tail > 0) && (!lvopts.Follow)
limitedNum := lvopts.Tail
var stop bool
var watcher *fsnotify.Watcher
baseName := filepath.Base(jsonLogFilePath)
dir := filepath.Dir(jsonLogFilePath)
retryTimes := 2
backBytes := 0
for {
select {
case <-stopChannel:
log.L.Debug("received stop signal while re-reading JSON logfile, returning")
return nil
default:
if stop || (limitedMode && limitedNum == 0) {
log.L.Debugf("finished parsing log JSON filefile, path: %s", jsonLogFilePath)
return nil
}
if line, err := jsonfile.Decode(stdout, stderr, fin, lvopts.Timestamps, lvopts.Since, lvopts.Until); err != nil {
if len(line) > 0 {
time.Sleep(5 * time.Millisecond)
if retryTimes == 0 {
log.L.Infof("finished parsing log JSON filefile, path: %s, line: %s", jsonLogFilePath, string(line))
return fmt.Errorf("error occurred while doing read of JSON logfile %q: %w, retryTimes: %d", jsonLogFilePath, err, retryTimes)
}
retryTimes--
backBytes = len(line)
} else {
return fmt.Errorf("error occurred while doing read of JSON logfile %q: %w", jsonLogFilePath, err)
}
} else {
retryTimes = 2
backBytes = 0
}
if lvopts.Follow {
// Get the current file handler's seek.
lastPos, err := fin.Seek(int64(-backBytes), io.SeekCurrent)
if err != nil {
return fmt.Errorf("error occurred while trying to seek JSON logfile %q at position %d: %s", jsonLogFilePath, lastPos, err)
}
if watcher == nil {
// Initialize the watcher if it has not been initialized yet.
if watcher, err = NewLogFileWatcher(dir); err != nil {
return err
}
defer watcher.Close()
// If we just created the watcher, try again to read as we might have missed
// the event.
continue
}
var recreated bool
// Wait until the next log change.
recreated, err = startTail(context.Background(), baseName, watcher)
if err != nil {
return err
}
if recreated {
newF, err := openFileShareDelete(jsonLogFilePath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
//If the user application outputs logs too quickly,
//There is a slight possibility that nerdctl has just rotated the log file,
//try opening it once more.
time.Sleep(10 * time.Millisecond)
}
newF, err = openFileShareDelete(jsonLogFilePath)
if err != nil {
return fmt.Errorf("failed to open JSON logfile %q: %w", jsonLogFilePath, err)
}
}
fin.Close()
fin = newF
}
continue
}
stop = true
// Give the OS a second to breathe before re-opening the file:
}
}
}