From 6c727b7e51144ad3a4cd91ea865a5459f19621e8 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Wed, 26 Mar 2025 11:37:48 +0100 Subject: [PATCH] refactor(logging): use log-enable-gcp-fields --- Makefile | 6 +-- pkg/modules/logging/logging.go | 69 ++++++++++++++++------------ pkg/modules/pdfengines/pdfengines.go | 1 + 3 files changed, 42 insertions(+), 34 deletions(-) diff --git a/Makefile b/Makefile index ae6747e9..fc271957 100644 --- a/Makefile +++ b/Makefile @@ -60,8 +60,7 @@ LIBREOFFICE_DISABLE_ROUTES=false LOG_LEVEL=info LOG_FORMAT=auto LOG_FIELDS_PREFIX= -LOG_ENABLE_GCP_SEVERITY=false -PDFENGINES_ENGINES= +LOG_ENABLE_GCP_FIELDS=false PDFENGINES_MERGE_ENGINES=qpdf,pdfcpu,pdftk PDFENGINES_SPLIT_ENGINES=pdfcpu,qpdf,pdftk PDFENGINES_FLATTEN_ENGINES=qpdf @@ -132,8 +131,7 @@ run: ## Start a Gotenberg container --log-level=$(LOG_LEVEL) \ --log-format=$(LOG_FORMAT) \ --log-fields-prefix=$(LOG_FIELDS_PREFIX) \ - --log-enable-gcp-severity=$(LOG_ENABLE_GCP_SEVERITY) \ - --pdfengines-engines=$(PDFENGINES_ENGINES) \ + --log-enable-gcp-fields=$(LOG_ENABLE_GCP_FIELDS) \ --pdfengines-merge-engines=$(PDFENGINES_MERGE_ENGINES) \ --pdfengines-split-engines=$(PDFENGINES_SPLIT_ENGINES) \ --pdfengines-flatten-engines=$(PDFENGINES_FLATTEN_ENGINES) \ diff --git a/pkg/modules/logging/logging.go b/pkg/modules/logging/logging.go index e2526038..0c4fd757 100644 --- a/pkg/modules/logging/logging.go +++ b/pkg/modules/logging/logging.go @@ -34,10 +34,10 @@ const ( // Logging is a module which implements the [gotenberg.LoggerProvider] // interface. type Logging struct { - level string - format string - fieldsPrefix string - enableGcpSeverity bool + level string + format string + fieldsPrefix string + enableGcpFields bool } // 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-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.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") + err := fs.MarkDeprecated("log-enable-gcp-severity", "use log-enable-gcp-fields instead") + if err != nil { + panic(err) + } return fs }(), @@ -64,7 +71,7 @@ func (log *Logging) Provision(ctx *gotenberg.Context) error { log.level = flags.MustString("log-level") log.format = flags.MustString("log-format") 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 } @@ -104,7 +111,7 @@ func (log *Logging) Logger(mod gotenberg.Module) (*zap.Logger, error) { 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 { return nil, fmt.Errorf("get log encoder: %w", err) } @@ -169,42 +176,44 @@ func newLogLevel(level string) (zapcore.Level, error) { 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())) 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 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) { encoder.AppendString(ts.Local().Format("2006/01/02 15:04:05.000")) } + } - if format == autoLoggingFormat { - format = textLoggingFormat - } - - if format == textLoggingFormat && gcpSeverity { + // Configure level encoding based on format and GCP settings. + if format == textLoggingFormat && isTerminal { + if gcpFields { encCfg.EncodeLevel = gcpSeverityColorEncoder - } else if format == textLoggingFormat { + } else { encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder - } else if gcpSeverity { - encCfg.EncodeLevel = gcpSeverityEncoder - } - } else { - if format == autoLoggingFormat { - format = jsonLoggingFormat } + } - if gcpSeverity { - encCfg.EncodeLevel = gcpSeverityEncoder - // Those only make sense in JSON. - encCfg.TimeKey = "time" - encCfg.LevelKey = "severity" - encCfg.MessageKey = "message" - encCfg.EncodeTime = zapcore.ISO8601TimeEncoder - encCfg.EncodeDuration = zapcore.MillisDurationEncoder - } + // For non-text (JSON) or when GCP fields are requested outside a terminal text output, + // adjust the configuration to use GCP-specific field names and encoders. + if gcpFields && format != textLoggingFormat { + encCfg.EncodeLevel = gcpSeverityEncoder + encCfg.TimeKey = "time" + encCfg.LevelKey = "severity" + encCfg.MessageKey = "message" + encCfg.EncodeTime = zapcore.ISO8601TimeEncoder + encCfg.EncodeDuration = zapcore.MillisDurationEncoder } switch format { diff --git a/pkg/modules/pdfengines/pdfengines.go b/pkg/modules/pdfengines/pdfengines.go index f029f0af..aae87f0d 100644 --- a/pkg/modules/pdfengines/pdfengines.go +++ b/pkg/modules/pdfengines/pdfengines.go @@ -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.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") err := fs.MarkDeprecated("pdfengines-engines", "use other flags for a more granular selection of PDF engines per method") if err != nil {