Skip to content

Commit d4d39ad

Browse files
committed
fix test
1 parent 66fae0b commit d4d39ad

File tree

1 file changed

+82
-62
lines changed

1 file changed

+82
-62
lines changed

controllers/spec/common.go

Lines changed: 82 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,25 @@ type TLSConfig interface {
170170
GetMountPath() string
171171
}
172172

173+
type RollingCfg struct {
174+
Enabled bool
175+
Type string // "size" or "time"
176+
File string
177+
// size-based
178+
MaxBytes int
179+
Backups int
180+
// time-based
181+
When string // e.g. "D", "W0"
182+
Interval int
183+
}
184+
185+
type LogCfg struct {
186+
Level string // INFO/DEBUG/...
187+
Format string // "json" or "text"
188+
Handlers string // computed: "stream_handler[,rotating_file_handler|,timed_rotating_file_handler]"
189+
Rolling RollingCfg
190+
}
191+
173192
func IsManaged(object metav1.Object) bool {
174193
managed, exists := object.GetAnnotations()[AnnotationManaged]
175194
return !exists || managed != "false"
@@ -1105,88 +1124,89 @@ func generatePythonLogConfigCommand(name string, runtime *v1alpha1.PythonRuntime
11051124

11061125
func renderPythonInstanceLoggingINITemplate(name string, runtime *v1alpha1.PythonRuntime, agent v1alpha1.LogTopicAgent) (string, error) {
11071126
tmpl := template.Must(template.New("spec").Parse(pythonLoggingINITemplate))
1108-
var tpl bytes.Buffer
1109-
type logConfig struct {
1110-
RollingEnabled bool
1111-
Level string
1112-
Policy template.HTML
1113-
Handlers string
1114-
Format string
1127+
1128+
lc := LogCfg{
1129+
Level: "INFO",
1130+
Format: "text",
1131+
Rolling: RollingCfg{
1132+
Enabled: false,
1133+
Backups: 5,
1134+
},
11151135
}
1116-
lc := &logConfig{}
1117-
lc.Level = "INFO"
1118-
lc.Format = "text"
1119-
lc.Handlers = "stream_handler"
1136+
1137+
// level
11201138
if runtime.Log != nil && runtime.Log.Level != "" {
11211139
if level := parsePythonLogLevel(runtime); level != "" {
11221140
lc.Level = level
11231141
}
11241142
}
1125-
if runtime.Log != nil && runtime.Log.Format != nil {
1126-
lc.Format = string(*runtime.Log.Format)
1143+
// format
1144+
if runtime.Log != nil && runtime.Log.Format != nil && strings.ToLower(string(*runtime.Log.Format)) == "json" {
1145+
lc.Format = "json"
11271146
}
1147+
1148+
// default handler
1149+
lc.Handlers = "stream_handler"
1150+
1151+
// log file path
1152+
logFile := fmt.Sprintf("logs/functions/%s.log", name)
1153+
1154+
// rolling policy
11281155
if runtime.Log != nil && runtime.Log.RotatePolicy != nil {
1129-
lc.RollingEnabled = true
1130-
logFile := fmt.Sprintf("logs/functions/%s-${%s}.log", name, EnvShardID)
1156+
lc.Rolling.Enabled = true
11311157
switch *runtime.Log.RotatePolicy {
1158+
case v1alpha1.SizedPolicyWith10MB:
1159+
lc.Rolling.Type = "size"
1160+
lc.Rolling.File = logFile
1161+
lc.Rolling.MaxBytes = 10 * 1024 * 1024
1162+
lc.Handlers = "stream_handler,rotating_file_handler"
1163+
case v1alpha1.SizedPolicyWith50MB:
1164+
lc.Rolling.Type = "size"
1165+
lc.Rolling.File = logFile
1166+
lc.Rolling.MaxBytes = 50 * 1024 * 1024
1167+
lc.Handlers = "stream_handler,rotating_file_handler"
1168+
case v1alpha1.SizedPolicyWith100MB:
1169+
lc.Rolling.Type = "size"
1170+
lc.Rolling.File = logFile
1171+
lc.Rolling.MaxBytes = 100 * 1024 * 1024
1172+
lc.Handlers = "stream_handler,rotating_file_handler"
11321173
case v1alpha1.TimedPolicyWithDaily:
1174+
lc.Rolling.Type = "time"
1175+
lc.Rolling.File = logFile
1176+
lc.Rolling.When = "D"
1177+
lc.Rolling.Interval = 1
11331178
lc.Handlers = "stream_handler,timed_rotating_file_handler"
1134-
lc.Policy = template.HTML(fmt.Sprintf(`[handler_timed_rotating_file_handler]
1135-
args=(\"%s\", 'D', 1, 5,)
1136-
class=handlers.TimedRotatingFileHandler
1137-
level=%s
1138-
formatter=formatter`, logFile, lc.Level))
11391179
case v1alpha1.TimedPolicyWithWeekly:
1180+
lc.Rolling.Type = "time"
1181+
lc.Rolling.File = logFile
1182+
lc.Rolling.When = "W0" // every monday
1183+
lc.Rolling.Interval = 1
11401184
lc.Handlers = "stream_handler,timed_rotating_file_handler"
1141-
lc.Policy = template.HTML(fmt.Sprintf(`[handler_timed_rotating_file_handler]
1142-
args=(\"%s\", 'W0', 1, 5,)
1143-
class=handlers.TimedRotatingFileHandler
1144-
level=%s
1145-
formatter=formatter`, logFile, lc.Level))
11461185
case v1alpha1.TimedPolicyWithMonthly:
1186+
lc.Rolling.Type = "time"
1187+
lc.Rolling.File = logFile
1188+
lc.Rolling.When = "D" // day
1189+
lc.Rolling.Interval = 30
11471190
lc.Handlers = "stream_handler,timed_rotating_file_handler"
1148-
lc.Policy = template.HTML(fmt.Sprintf(`[handler_timed_rotating_file_handler]
1149-
args=(\"%s\", 'D', 30, 5,)
1150-
class=handlers.TimedRotatingFileHandler
1151-
level=%s
1152-
formatter=formatter`, logFile, lc.Level))
1153-
case v1alpha1.SizedPolicyWith10MB:
1154-
lc.Handlers = "stream_handler,rotating_file_handler"
1155-
lc.Policy = template.HTML(fmt.Sprintf(`[handler_rotating_file_handler]
1156-
args=(\"%s\", 'a', 10485760, 5,)
1157-
class=handlers.RotatingFileHandler
1158-
level=%s
1159-
formatter=formatter`, logFile, lc.Level))
1160-
case v1alpha1.SizedPolicyWith50MB:
1161-
lc.Handlers = "handler_stream_handler,rotating_file_handler"
1162-
lc.Policy = template.HTML(fmt.Sprintf(`[handler_rotating_file_handler]
1163-
args=(%s, 'a', 52428800, 5,)
1164-
class=handlers.RotatingFileHandler
1165-
level=%s
1166-
formatter=formatter`, logFile, lc.Level))
1167-
case v1alpha1.SizedPolicyWith100MB:
1168-
lc.Handlers = "handler_stream_handler,rotating_file_handler"
1169-
lc.Policy = template.HTML(fmt.Sprintf(`[handler_rotating_file_handler]
1170-
args=(%s, 'a', 104857600, 5,)
1171-
class=handlers.RotatingFileHandler
1172-
level=%s
1173-
formatter=formatter`, logFile, lc.Level))
1174-
}
1175-
} else if agent == v1alpha1.SIDECAR { // sidecar mode needs the rotated log file
1176-
lc.RollingEnabled = true
1177-
logFile := fmt.Sprintf("logs/functions/%s-${%s}.log", name, EnvShardID)
1191+
}
1192+
} else if agent == v1alpha1.SIDECAR {
1193+
// sidecar mode enables rolling by default, using size policy with 10MB
1194+
lc.Rolling = RollingCfg{
1195+
Enabled: true,
1196+
Type: "size",
1197+
File: logFile,
1198+
MaxBytes: 10 * 1024 * 1024,
1199+
Backups: 5,
1200+
}
11781201
lc.Handlers = "stream_handler,rotating_file_handler"
1179-
lc.Policy = template.HTML(fmt.Sprintf(`[handler_rotating_file_handler]
1180-
args=(\"%s\", 'a', 10485760, 5,)
1181-
class=handlers.RotatingFileHandler
1182-
level=%s
1183-
formatter=formatter`, logFile, lc.Level))
11841202
}
1185-
if err := tmpl.Execute(&tpl, lc); err != nil {
1203+
1204+
var buf bytes.Buffer
1205+
if err := tmpl.Execute(&buf, &lc); err != nil {
11861206
log.Error(err, "failed to render python instance logging template")
11871207
return "", err
11881208
}
1189-
return tpl.String(), nil
1209+
return buf.String(), nil
11901210
}
11911211

11921212
func parseJavaLogLevel(runtime *v1alpha1.JavaRuntime) string {

0 commit comments

Comments
 (0)