Skip to content

Commit

Permalink
fix(productlogging): logback config syntax erro
Browse files Browse the repository at this point in the history
  • Loading branch information
lwpk110 committed Nov 11, 2024
1 parent 098068c commit 7b45564
Show file tree
Hide file tree
Showing 8 changed files with 482 additions and 377 deletions.
4 changes: 2 additions & 2 deletions .chainsaw.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spec:
delete: 120s
error: 180s
exec: 300s
skipDelete: false
# skipDelete: true
# skipDelete: false
skipDelete: true
failFast: true
parallel: 1
48 changes: 0 additions & 48 deletions .gitignore copy

This file was deleted.

6 changes: 3 additions & 3 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion internal/common/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (l *LogbackXmlGenerator) FileName() string {

// Generate implements config.FileContentGenerator.
func (l *LogbackXmlGenerator) Generate(ctx context.Context) (string, error) {
logGenerator, err := productlogging.NewConfigGenerator(
logGenerator, err := NewConfigGenerator(
l.loggingSpec,
string(l.container),
fmt.Sprintf("%s.log4j.xml", l.container),
Expand All @@ -161,6 +161,7 @@ func (l *LogbackXmlGenerator) Generate(ctx context.Context) (string, error) {
cgo.ConsoleHandlerFormatter = ptr.To(dolphinv1alpha1.ConsoleConversionPattern)
},
)

if err != nil {
return "", err
}
Expand Down
141 changes: 141 additions & 0 deletions internal/common/productlogging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package common

import (
"path"
"strings"

"github.com/zncdatadev/operator-go/pkg/config"
"github.com/zncdatadev/operator-go/pkg/productlogging"

loggingv1alpha1 "github.com/zncdatadev/operator-go/pkg/apis/commons/v1alpha1"

"github.com/zncdatadev/operator-go/pkg/constants"
)

const logbackTemplate = `<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>{{ .ConsoleHandlerFormatter }}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>{{ .ConsoleHandlerLevel }}</level>
</filter>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>{{ .RotatingFileHandlerFile }}</File>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.log4j.XMLLayout" />
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>{{ .RotatingFileHandlerLevel }}</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<minIndex>1</minIndex>
<maxIndex>{{ .RotatingFileHandlerBackupCount }}</maxIndex>
<FileNamePattern>{{ .RotatingFileHandlerFile }}.%i</FileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>{{ .RotatingFileHandlerMaxSizeInMiB }}MB</MaxFileSize>
</triggeringPolicy>
</appender>
{{- .Loggers }}
<root level="{{ .RootLogLevel }}">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
`

var _ productlogging.LoggingConfig = &LogbackConfig{}

// LogbackConfig is a struct that contains logback logging configuration
type LogbackConfig struct {
productLogging *productlogging.ProductLogging
}

// Content implements the LoggingConfig interface
func (l *LogbackConfig) Content() (string, error) {
values := productlogging.JavaLogTemplateValue(l, l.productLogging)

p := config.TemplateParser{Template: l.Template(), Value: values}
return p.Parse()
}

// LoggerFormatter implements the LoggingConfig interface
func (l *LogbackConfig) LoggerFormatter(name string, level string) string {
return `<logger name="` + name + `" level="` + level + `" />`
}

// String implements the LoggingConfig interface
func (l *LogbackConfig) String() string {
c, e := l.Content()
if e != nil {
panic(e)
}
return c
}

// Template implements the LoggingConfig interface
func (l *LogbackConfig) Template() string {
return logbackTemplate
}

func NewConfigGenerator(
loggingConfigSpec *loggingv1alpha1.LoggingConfigSpec,
containerName string,
logFileName string,
logType productlogging.LogType,
opts ...productlogging.ConfigGeneratorOptionFunc,
) (*LogbackConfig, error) {

opt := &productlogging.ConfigGeneratorOption{}

for _, o := range opts {
o(opt)
}

rotatingFileHandlerMaxBytes := productlogging.DefaultRotatingFileHandlerMaxBytes

if opt.LogFileMaxBytes != nil {
rotatingFileHandlerMaxBytes = *opt.LogFileMaxBytes
}

p := &productlogging.ProductLogging{
RootLogLevel: productlogging.DefaultLoggerLevel,
ConsoleHandlerLevel: productlogging.DefaultLoggerLevel,
ConsoleHandlerFormatter: *opt.ConsoleHandlerFormatter,

RotatingFileHandlerLevel: productlogging.DefaultLoggerLevel,
RotatingFileHandlerFile: path.Join(constants.KubedoopLogDir, strings.ToLower(containerName), logFileName),
// Default File size is 10MB
RotatingFileHandlerMaxBytes: rotatingFileHandlerMaxBytes,
RotatingFileHandlerBackupCount: 1,

Loggers: make(map[string]loggingv1alpha1.LogLevelSpec),
}

if loggingConfigSpec != nil {
// If console and file log levels are defined, use them. Otherwise, use the default log level.
if loggingConfigSpec.Console != nil && loggingConfigSpec.Console.Level != "" {
p.ConsoleHandlerLevel = loggingConfigSpec.Console.Level
}
if loggingConfigSpec.File != nil && loggingConfigSpec.File.Level != "" {
p.RotatingFileHandlerLevel = loggingConfigSpec.File.Level
}

if loggingConfigSpec.Loggers != nil {
for name, level := range loggingConfigSpec.Loggers {
if name == productlogging.RootLoggerName {
p.RootLogLevel = level.Level
} else {
p.Loggers[name] = *level
}
}
}
}

return &LogbackConfig{productLogging: p}, nil
}
5 changes: 3 additions & 2 deletions pkg/config/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"context"
"fmt"

ctrl "sigs.k8s.io/controller-runtime"
)
Expand Down Expand Up @@ -34,14 +35,14 @@ func GenerateAllFile(ctx context.Context, confGenerator []FileContentGenerator)
data := make(map[string]string)
for _, generator := range confGenerator {
content, err := generator.Generate(ctx)

if err != nil {
configLogger.Error(err, "generate config error", "generator", generator, "fileName", generator.FileName())
continue
}
fmt.Print(content)

// if content != "" {
data[generator.FileName()] = content
// }
}
return data
}
Expand Down
20 changes: 11 additions & 9 deletions test/e2e/vector/aggregator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ spec:
containers:
- name: es7
image: docker.elastic.co/elasticsearch/elasticsearch:7.17.7
# resources:
# limits:
# cpu: 500m
# memory: 1Gi
resources:
limits:
cpu: 200m
memory: 1Gi
env:
- name: discovery.type
value: "single-node"
Expand All @@ -28,10 +28,10 @@ spec:
name: http
- name: kibana7
image: docker.elastic.co/kibana/kibana:7.17.7
# resources:
# limits:
# cpu: 500m
# memory: 1Gi
resources:
limits:
cpu: 200m
memory: 500Mi
env:
- name: ELASTICSEARCH_HOSTS
value: "http://localhost:9200"
Expand Down Expand Up @@ -83,7 +83,9 @@ data:
api_version: auto
compression: none
doc_type: _doc
endpoints:
endpoints:Copilot features might go through different early access stages, which are typically enabled and configured through settings.
- http://elasticsearch:9200
mode: bulk
# query:
Expand Down

0 comments on commit 7b45564

Please sign in to comment.