-
Notifications
You must be signed in to change notification settings - Fork 20
/
logging.go
139 lines (124 loc) · 4.16 KB
/
logging.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
// Copyright 2023 Fraunhofer AISEC
//
// 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.
//
// $$\ $$\ $$\ $$\
// $$ | $$ |\__| $$ |
// $$$$$$$\ $$ | $$$$$$\ $$\ $$\ $$$$$$$ |$$\ $$$$$$\ $$$$$$\ $$$$$$\
// $$ _____|$$ |$$ __$$\ $$ | $$ |$$ __$$ |$$ |\_$$ _| $$ __$$\ $$ __$$\
// $$ / $$ |$$ / $$ |$$ | $$ |$$ / $$ |$$ | $$ | $$ / $$ |$$ | \__|
// $$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$\ $$ | $$ |$$ |
// \$$$$$$\ $$ |\$$$$$ |\$$$$$ |\$$$$$$ |$$ | \$$$ |\$$$$$ |$$ |
// \_______|\__| \______/ \______/ \_______|\__| \____/ \______/ \__|
//
// This file is part of Clouditor Community Edition.
package logging
import (
"bytes"
"fmt"
"strings"
"clouditor.io/clouditor/internal/api"
"clouditor.io/clouditor/internal/util"
"github.com/sirupsen/logrus"
)
// RequestType specifies the type of request
type RequestType int
const (
Assess RequestType = iota
Add
Create
Register
Remove
Store
Send
Update
)
// String returns the RequestType as string.
func (r RequestType) String() string {
switch r {
case Assess:
return "assessed"
case Add:
return "added"
case Create:
return "created"
case Register:
return "registered"
case Remove:
return "removed"
case Store:
return "stored"
case Send:
return "sent"
case Update:
return "updated"
default:
return "unspecified"
}
}
// LogRequest creates a logging message with the given parameters
// - log *logrus.Entry
// - level the message must have
// - reqType is the request type
// - Optional. params for self-created string messages to be appended to the created log message. The elements do not need a space at the beginning of the message.
//
// The message looks like one of the following depending on the given information
//
// "*orchestrator.Catalog created with ID 'Cat1234'."
//
// or
//
// "*orchestrator.Certificate created with ID 'Cert1234' for Cloud Service '00000000-0000-0000-0000-000000000000'."
//
// or
//
// "*orchestrator.TargetOfEvaluation created with ID 'ToE1234' for Cloud Service '00000000-0000-0000-0000-000000000000' and Catalog 'EUCS'."
func LogRequest(log *logrus.Entry, level logrus.Level, reqType RequestType, req api.PayloadRequest, params ...string) {
var (
buffer bytes.Buffer
)
// Check if inputs are available
if log == nil || util.IsNil(req) {
return
}
// Retrieve the payload from the request. The request itself is usually
// a wrapper around the sent object.
payload := req.GetPayload()
if util.IsNil(payload) {
return
}
// We can retrieve the name via the proto descriptor. This should be
// sufficiently fast and also gives us the non-pointer type in comparison to
// the %T printf directive.
name := req.GetPayload().ProtoReflect().Descriptor().Name()
// Check, if our payload has an ID field
idreq, ok := payload.(interface{ GetId() string })
if ok && idreq.GetId() != "" {
buffer.WriteString(fmt.Sprintf("%s with ID '%s' %s", name, idreq.GetId(), reqType.String()))
} else {
buffer.WriteString(fmt.Sprintf("%s %s", name, reqType.String()))
}
// Check, if it is a cloud service request. In this case we can append the
// information about the target cloud service. However, we only want to do
// that, if the payload type is not a cloud service itself.
csreq, ok := req.(api.CloudServiceRequest)
if name != "CloudService" && ok {
buffer.WriteString(fmt.Sprintf(" for Cloud Service '%s'", csreq.GetCloudServiceId()))
}
// If params is not empty, the elements are joined and added to the message
if len(params) > 0 {
buffer.WriteString(" " + strings.Join(params, " "))
}
buffer.WriteString(".")
log.Log(level, buffer.String())
}