forked from mendersoftware/mender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeployment_logger.go
292 lines (244 loc) · 7.31 KB
/
deployment_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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright 2018 Northern.tech AS
//
// 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 main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"syscall"
)
// error messages
var (
ErrLoggerNotInitialized = errors.New("logger not initialized")
ErrNotEnoughSpaceForLogs = errors.New("not enough space for storing logs")
)
type FileLogger struct {
logFileName string
logFile io.WriteCloser
}
// NewFileLogger creates instance of file logger; it is initialized
// just before logging is started
func NewFileLogger(name string) *FileLogger {
// open log file
logFile, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_APPEND|os.O_SYNC, 0600)
if err != nil {
// if we can not open file for logging; return nil
return nil
}
// return FileLogger only when logging is possible (we can open log file)
return &FileLogger{
logFileName: name,
logFile: logFile,
}
}
func (fl *FileLogger) Write(log []byte) (int, error) {
return fl.logFile.Write(log)
}
func (fl *FileLogger) Deinit() error {
return fl.logFile.Close()
}
type DeploymentLogManager struct {
logLocation string
deploymentID string
logger *FileLogger
// how many log files we are keeping in log directory before rotating
maxLogFiles int
minLogSizeBytes uint64
// it is easy to add logging hook, but not so much remove it;
// we need a mechanism for emabling and disabling logging
loggingEnabled bool
}
const baseLogFileName = "deployments"
const logFileNameScheme = baseLogFileName + ".%04d.%s.log"
func NewDeploymentLogManager(logDirLocation string) *DeploymentLogManager {
return &DeploymentLogManager{
logLocation: logDirLocation,
// file logger needs to be instanciated just before writing logs
//logger:
// for now we can hardcode this
maxLogFiles: 5,
minLogSizeBytes: 1024 * 100, //100kb
loggingEnabled: false,
}
}
func (dlm DeploymentLogManager) WriteLog(log []byte) error {
if dlm.logger == nil {
return ErrLoggerNotInitialized
}
_, err := dlm.logger.Write(log)
return err
}
// check if there is enough space to store the logs
func (dlm *DeploymentLogManager) haveEnoughSpaceForStoringLogs() bool {
var stat syscall.Statfs_t
syscall.Statfs(dlm.logLocation, &stat)
// Available blocks * size per block = available space in bytes
availableSpace := stat.Bavail * uint64(stat.Bsize)
if availableSpace < dlm.minLogSizeBytes {
// can not log error here; no space for logs
return false
}
return true
}
func (dlm *DeploymentLogManager) Enable(deploymentID string) error {
if dlm.loggingEnabled {
return nil
}
if !dlm.haveEnoughSpaceForStoringLogs() {
return ErrNotEnoughSpaceForLogs
}
dlm.deploymentID = deploymentID
// we might have new deployment so might need to rotate files
dlm.Rotate()
// instanciate logger
logFileName := fmt.Sprintf(logFileNameScheme, 1, deploymentID)
dlm.logger = NewFileLogger(filepath.Join(dlm.logLocation, logFileName))
if dlm.logger == nil {
return ErrLoggerNotInitialized
}
dlm.loggingEnabled = true
return nil
}
func (dlm *DeploymentLogManager) Disable() error {
if !dlm.loggingEnabled {
return nil
}
if err := dlm.logger.Deinit(); err != nil {
return err
}
dlm.loggingEnabled = false
return nil
}
func (dlm DeploymentLogManager) getSortedLogFiles() ([]string, error) {
// list all the log files in log directory
logFiles, err :=
filepath.Glob(filepath.Join(dlm.logLocation, baseLogFileName+".*"))
if err != nil {
return nil, err
}
sort.Sort(sort.Reverse(sort.StringSlice(logFiles)))
return logFiles, nil
}
//log naming convention: <base_name>.%04d.<deployment_id>.log
func (dlm DeploymentLogManager) rotateLogFileName(name string) string {
logFileName := filepath.Base(name)
nameChunks := strings.Split(logFileName, ".")
if len(nameChunks) != 4 {
// we have malformed file name or file is not a log file
return name
}
seq, err := strconv.Atoi(nameChunks[1])
if err == nil {
// IDEA: this will allow handling 9999 log files correctly
// for more we need to change implementation of getSortedLogFiles()
return filepath.Join(filepath.Dir(name),
fmt.Sprintf(logFileNameScheme, (seq+1), nameChunks[2]))
}
return name
}
func (dlm DeploymentLogManager) Rotate() {
logFiles, err := dlm.getSortedLogFiles()
if err != nil {
// can not rotate
return
}
// do we have some log files already
if len(logFiles) == 0 {
return
}
// check if we need to delete the oldest file(s)
for len(logFiles) > dlm.maxLogFiles {
os.Remove(logFiles[0])
logFiles = logFiles[1:]
}
// check if last file is the one with the current deployment ID
if strings.Contains(logFiles[len(logFiles)-1], dlm.deploymentID) {
return
}
// after rotating we should end up with dlm.maxLogFiles-1 files to
// have a space for creating new log file
for len(logFiles) > dlm.maxLogFiles-1 {
os.Remove(logFiles[0])
logFiles = logFiles[1:]
}
// rename log files; only those not removed
for i := range logFiles {
os.Rename(logFiles[i], dlm.rotateLogFileName(logFiles[i]))
}
}
func (dlm DeploymentLogManager) findLogsForSpecificID(deploymentID string) (string, error) {
logFiles, err := dlm.getSortedLogFiles()
if err != nil {
return "", err
}
// look for the file containing given deployment id
for _, file := range logFiles {
if strings.Contains(file, deploymentID) {
return file, nil
}
}
return "", os.ErrNotExist
}
// GetLogs is returnig logs as a JSON string. Function is having the same
// signature as json.Marshal() ([]byte, error)
func (dlm DeploymentLogManager) GetLogs(deploymentID string) ([]byte, error) {
// opaque individual raw JSON entries into `{"messages:" [...]}` format
type formattedDeploymentLogs struct {
Messages []json.RawMessage `json:"messages"`
}
// must be initialized as below
// if we will use `var logsList []json.RawMessage` instead, while marshalling
// to JSON we will end up with `{"messages":null}` instead of `{"messages":[]}`
logsList := make([]json.RawMessage, 0)
logFileName, err := dlm.findLogsForSpecificID(deploymentID)
// log file for specific deployment id does not exist
if err == os.ErrNotExist {
logs := formattedDeploymentLogs{logsList}
return json.Marshal(logs)
}
if err != nil {
return nil, err
}
logF, err := os.Open(logFileName)
if err != nil {
return nil, err
}
defer logF.Close()
// read log file line by line
scanner := bufio.NewScanner(logF)
// read log file line by line
for scanner.Scan() {
var logLine json.RawMessage
// check if the log is valid JSON
err = json.Unmarshal([]byte(scanner.Text()), &logLine)
if err != nil {
// we have broken JSON log; just skip it for now
continue
}
// here we should have a list of verified JSON logs
logsList = append(logsList, logLine)
}
if err = scanner.Err(); err != nil {
return nil, err
}
logs := formattedDeploymentLogs{logsList}
return json.Marshal(logs)
}