refactor(logging): use log-enable-gcp-fields

This commit is contained in:
Julien Neuhart
2025-03-26 11:37:48 +01:00
parent a9754d6a1c
commit 6c727b7e51
3 changed files with 42 additions and 34 deletions

View File

@@ -60,8 +60,7 @@ LIBREOFFICE_DISABLE_ROUTES=false
LOG_LEVEL=info LOG_LEVEL=info
LOG_FORMAT=auto LOG_FORMAT=auto
LOG_FIELDS_PREFIX= LOG_FIELDS_PREFIX=
LOG_ENABLE_GCP_SEVERITY=false LOG_ENABLE_GCP_FIELDS=false
PDFENGINES_ENGINES=
PDFENGINES_MERGE_ENGINES=qpdf,pdfcpu,pdftk PDFENGINES_MERGE_ENGINES=qpdf,pdfcpu,pdftk
PDFENGINES_SPLIT_ENGINES=pdfcpu,qpdf,pdftk PDFENGINES_SPLIT_ENGINES=pdfcpu,qpdf,pdftk
PDFENGINES_FLATTEN_ENGINES=qpdf PDFENGINES_FLATTEN_ENGINES=qpdf
@@ -132,8 +131,7 @@ run: ## Start a Gotenberg container
--log-level=$(LOG_LEVEL) \ --log-level=$(LOG_LEVEL) \
--log-format=$(LOG_FORMAT) \ --log-format=$(LOG_FORMAT) \
--log-fields-prefix=$(LOG_FIELDS_PREFIX) \ --log-fields-prefix=$(LOG_FIELDS_PREFIX) \
--log-enable-gcp-severity=$(LOG_ENABLE_GCP_SEVERITY) \ --log-enable-gcp-fields=$(LOG_ENABLE_GCP_FIELDS) \
--pdfengines-engines=$(PDFENGINES_ENGINES) \
--pdfengines-merge-engines=$(PDFENGINES_MERGE_ENGINES) \ --pdfengines-merge-engines=$(PDFENGINES_MERGE_ENGINES) \
--pdfengines-split-engines=$(PDFENGINES_SPLIT_ENGINES) \ --pdfengines-split-engines=$(PDFENGINES_SPLIT_ENGINES) \
--pdfengines-flatten-engines=$(PDFENGINES_FLATTEN_ENGINES) \ --pdfengines-flatten-engines=$(PDFENGINES_FLATTEN_ENGINES) \

View File

@@ -34,10 +34,10 @@ const (
// Logging is a module which implements the [gotenberg.LoggerProvider] // Logging is a module which implements the [gotenberg.LoggerProvider]
// interface. // interface.
type Logging struct { type Logging struct {
level string level string
format string format string
fieldsPrefix string fieldsPrefix string
enableGcpSeverity bool enableGcpFields bool
} }
// Descriptor returns a [Logging]'s module descriptor. // Descriptor returns a [Logging]'s module descriptor.
@@ -49,7 +49,14 @@ func (log *Logging) Descriptor() gotenberg.ModuleDescriptor {
fs.String("log-level", infoLoggingLevel, fmt.Sprintf("Choose the level of logging detail. Options include %s, %s, %s, or %s", errorLoggingLevel, warnLoggingLevel, infoLoggingLevel, debugLoggingLevel)) fs.String("log-level", infoLoggingLevel, fmt.Sprintf("Choose the level of logging detail. Options include %s, %s, %s, or %s", errorLoggingLevel, warnLoggingLevel, infoLoggingLevel, debugLoggingLevel))
fs.String("log-format", autoLoggingFormat, fmt.Sprintf("Specify the format of logging. Options include %s, %s, or %s", autoLoggingFormat, jsonLoggingFormat, textLoggingFormat)) fs.String("log-format", autoLoggingFormat, fmt.Sprintf("Specify the format of logging. Options include %s, %s, or %s", autoLoggingFormat, jsonLoggingFormat, textLoggingFormat))
fs.String("log-fields-prefix", "", "Prepend a specified prefix to each field in the logs") fs.String("log-fields-prefix", "", "Prepend a specified prefix to each field in the logs")
fs.Bool("log-enable-gcp-fields", false, "Enable GCP fields - namely: time, message, severity")
// Deprecated flags.
fs.Bool("log-enable-gcp-severity", false, "Enable Google Cloud Platform severity mapping") fs.Bool("log-enable-gcp-severity", false, "Enable Google Cloud Platform severity mapping")
err := fs.MarkDeprecated("log-enable-gcp-severity", "use log-enable-gcp-fields instead")
if err != nil {
panic(err)
}
return fs return fs
}(), }(),
@@ -64,7 +71,7 @@ func (log *Logging) Provision(ctx *gotenberg.Context) error {
log.level = flags.MustString("log-level") log.level = flags.MustString("log-level")
log.format = flags.MustString("log-format") log.format = flags.MustString("log-format")
log.fieldsPrefix = flags.MustString("log-fields-prefix") log.fieldsPrefix = flags.MustString("log-fields-prefix")
log.enableGcpSeverity = flags.MustBool("log-enable-gcp-severity") log.enableGcpFields = flags.MustDeprecatedBool("log-enable-gcp-severity", "log-enable-gcp-fields")
return nil return nil
} }
@@ -104,7 +111,7 @@ func (log *Logging) Logger(mod gotenberg.Module) (*zap.Logger, error) {
return nil, fmt.Errorf("get log level: %w", err) return nil, fmt.Errorf("get log level: %w", err)
} }
encoder, err := newLogEncoder(log.format, log.enableGcpSeverity) encoder, err := newLogEncoder(log.format, log.enableGcpFields)
if err != nil { if err != nil {
return nil, fmt.Errorf("get log encoder: %w", err) return nil, fmt.Errorf("get log encoder: %w", err)
} }
@@ -169,42 +176,44 @@ func newLogLevel(level string) (zapcore.Level, error) {
return lvl, nil return lvl, nil
} }
func newLogEncoder(format string, gcpSeverity bool) (zapcore.Encoder, error) { func newLogEncoder(format string, gcpFields bool) (zapcore.Encoder, error) {
isTerminal := term.IsTerminal(int(os.Stdout.Fd())) isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
encCfg := zap.NewProductionEncoderConfig() encCfg := zap.NewProductionEncoderConfig()
// Normalize the log format based on the output device.
if format == autoLoggingFormat {
if isTerminal {
format = textLoggingFormat
} else {
format = jsonLoggingFormat
}
}
// Use a human-readable time format if running in a terminal.
if isTerminal { if isTerminal {
// If interactive terminal, make output more human-readable by default.
// Credits: https://github.com/caddyserver/caddy/blob/v2.1.1/logging.go#L671.
encCfg.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) { encCfg.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(ts.Local().Format("2006/01/02 15:04:05.000")) encoder.AppendString(ts.Local().Format("2006/01/02 15:04:05.000"))
} }
}
if format == autoLoggingFormat { // Configure level encoding based on format and GCP settings.
format = textLoggingFormat if format == textLoggingFormat && isTerminal {
} if gcpFields {
if format == textLoggingFormat && gcpSeverity {
encCfg.EncodeLevel = gcpSeverityColorEncoder encCfg.EncodeLevel = gcpSeverityColorEncoder
} else if format == textLoggingFormat { } else {
encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
} else if gcpSeverity {
encCfg.EncodeLevel = gcpSeverityEncoder
}
} else {
if format == autoLoggingFormat {
format = jsonLoggingFormat
} }
}
if gcpSeverity { // For non-text (JSON) or when GCP fields are requested outside a terminal text output,
encCfg.EncodeLevel = gcpSeverityEncoder // adjust the configuration to use GCP-specific field names and encoders.
// Those only make sense in JSON. if gcpFields && format != textLoggingFormat {
encCfg.TimeKey = "time" encCfg.EncodeLevel = gcpSeverityEncoder
encCfg.LevelKey = "severity" encCfg.TimeKey = "time"
encCfg.MessageKey = "message" encCfg.LevelKey = "severity"
encCfg.EncodeTime = zapcore.ISO8601TimeEncoder encCfg.MessageKey = "message"
encCfg.EncodeDuration = zapcore.MillisDurationEncoder encCfg.EncodeTime = zapcore.ISO8601TimeEncoder
} encCfg.EncodeDuration = zapcore.MillisDurationEncoder
} }
switch format { switch format {

View File

@@ -51,6 +51,7 @@ func (mod *PdfEngines) Descriptor() gotenberg.ModuleDescriptor {
fs.StringSlice("pdfengines-write-metadata-engines", []string{"exiftool"}, "Set the PDF engines and their order for the write metadata feature - empty means all") fs.StringSlice("pdfengines-write-metadata-engines", []string{"exiftool"}, "Set the PDF engines and their order for the write metadata feature - empty means all")
fs.Bool("pdfengines-disable-routes", false, "Disable the routes") fs.Bool("pdfengines-disable-routes", false, "Disable the routes")
// Deprecated flags.
fs.StringSlice("pdfengines-engines", make([]string, 0), "Set the default PDF engines and their default order - all by default") fs.StringSlice("pdfengines-engines", make([]string, 0), "Set the default PDF engines and their default order - all by default")
err := fs.MarkDeprecated("pdfengines-engines", "use other flags for a more granular selection of PDF engines per method") err := fs.MarkDeprecated("pdfengines-engines", "use other flags for a more granular selection of PDF engines per method")
if err != nil { if err != nil {