all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Leo Nikkilä via Guix-patches via" <guix-patches@gnu.org>
To: 69399@debbugs.gnu.org
Cc: "Leo Nikkilä" <hello@lnikki.la>
Subject: [bug#69399] [PATCH] services: nginx: Make log formats configurable.
Date: Mon, 26 Feb 2024 02:29:18 +0200	[thread overview]
Message-ID: <20240226003615.21481-1-hello@lnikki.la> (raw)

Currently it's not possible to configure the log format through Guix,
since additional log formats must be declared before they're used, and
`extra-content' is always added at the bottom of the `http' block.

This patch aims to make these log formats configurable.

* gnu/services/web.scm (<nginx-log-format-configuration>): New record.
(<nginx-configuration>)[log-format, log-formats]: New fields.
(assert-valid-log-format-escape): New procedure.
(emit-nginx-log-format-config): New procedure.
(default-nginx-config): Make log formats configurable.
* doc/guix.texi (Web Services): Document it.
---
 doc/guix.texi        | 40 +++++++++++++++++++++++++++++++++++++---
 gnu/services/web.scm | 41 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 76 insertions(+), 5 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 671cdab6f8..71a00740bc 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -31684,6 +31693,31 @@ valued G-expression.
 @end table
 @end deftp
 
+@deftp {Data Type} nginx-log-format-configuration
+Data type representing the configuration of a custom Nginx log format.
+This type has the following parameters:
+
+@table @asis
+@item @code{name} (type: symbol)
+The name of the log format as a symbol.
+
+@item @code{escape} (default: @code{'default}) (type: symbol)
+The mode used to escape values of variables in the format.  The
+supported values are:
+
+@itemize
+@item @code{'default}, to escape all characters outside of the ASCII
+      printable range.
+@item @code{'json}, to escape all characters invalid in JSON strings.
+@item @code{'none}, to disable escaping.
+@end itemize
+
+@item @code{format} (type: string)
+The format as accepted by the @code{log_format} directive.
+
+@end table
+@end deftp
+
 @anchor{nginx-server-configuration}
 @deftp {Data Type} nginx-server-configuration
 Data type representing the configuration of an nginx server block.
diff --git a/gnu/services/web.scm b/gnu/services/web.scm
index 406117c457..5f895bab6d 100644
--- a/gnu/services/web.scm
+++ b/gnu/services/web.scm
@@ -17,6 +17,7 @@
 ;;; Copyright © 2022 Simen Endsjø <simendsjo@gmail.com>
 ;;; Copyright © 2023 Bruno Victal <mirai@makinata.eu>
 ;;; Copyright © 2023 Miguel Ángel Moreno <mail@migalmoreno.com>
+;;; Copyright © 2024 Leo Nikkilä <hello@lnikki.la>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -103,6 +104,8 @@ (define-module (gnu services web)
             nginx-configuration-nginx
             nginx-configuration-shepherd-requirement
             nginx-configuration-log-directory
+            nginx-configuration-log-format
+            nginx-configuration-log-formats
             nginx-configuration-log-level
             nginx-configuration-run-directory
             nginx-configuration-server-blocks
@@ -114,6 +117,12 @@ (define-module (gnu services web)
             nginx-configuration-extra-content
             nginx-configuration-file
 
+            nginx-log-format-configuration
+            nginx-log-format-configuration?
+            nginx-log-format-configuration-name
+            nginx-log-format-configuration-escape
+            nginx-log-format-configuration-format
+
             nginx-server-configuration
             nginx-server-configuration?
             nginx-server-configuration-listen
@@ -522,6 +531,23 @@ (define httpd-service-type
                   (httpd-configuration))
                 (description "Run the Apache httpd Web server.")))
 
+(define-record-type* <nginx-log-format-configuration>
+  nginx-log-format-configuration make-nginx-log-format-configuration
+  nginx-log-format-configuration?
+  (name                nginx-log-format-configuration-name)
+  (escape              nginx-log-format-configuration-escape
+                       (sanitize assert-valid-log-format-escape)
+                       (default 'default))
+  (format              nginx-log-format-configuration-format))
+
+(define (assert-valid-log-format-escape escape)
+  "Ensure @var{escape} is one of @code{'default}, @code{'json}, or
+@code{'none}."
+  (unless (memq escape '(default json none))
+    (raise
+     (formatted-message (G_ "unknown log format escape '~a'~%") escape)))
+  escape)
+
 (define-record-type* <nginx-server-configuration>
   nginx-server-configuration make-nginx-server-configuration
   nginx-server-configuration?
@@ -580,6 +606,10 @@ (define-record-type* <nginx-configuration>
   (log-level     nginx-configuration-log-level
                  (sanitize assert-valid-log-level)
                  (default 'error))
+  (log-format    nginx-configuration-log-format     ;symbol
+                 (default 'combined))
+  (log-formats   nginx-configuration-log-formats    ;list of <nginx-log-format-configuration>
+                 (default '()))
   (run-directory nginx-configuration-run-directory  ;string
                  (default "/var/run/nginx"))
   (server-blocks nginx-configuration-server-blocks
@@ -635,6 +665,12 @@ (define emit-global-directive
     ((key . value)
      (format #f "~a ~a;~%" key value))))
 
+(define emit-nginx-log-format-config
+  (match-lambda
+    (($ <nginx-log-format-configuration> name escape format)
+     (list "    log_format " (symbol->string name) " escape="
+           (symbol->string escape) " " format ";\n"))))
+
 (define emit-nginx-location-config
   (match-lambda
     (($ <nginx-location-configuration> uri body)
@@ -718,7 +754,7 @@ (define (default-nginx-config config)
   (match-record config
                 <nginx-configuration>
                 (nginx log-directory run-directory
-                 log-level
+                 log-level log-format log-formats
                  server-blocks upstream-blocks
                  server-names-hash-bucket-size
                  server-names-hash-bucket-max-size
@@ -740,7 +776,8 @@ (define (default-nginx-config config)
            "    fastcgi_temp_path " run-directory "/fastcgi_temp;\n"
            "    uwsgi_temp_path " run-directory "/uwsgi_temp;\n"
            "    scgi_temp_path " run-directory "/scgi_temp;\n"
-           "    access_log " log-directory "/access.log;\n"
+           (map emit-nginx-log-format-config log-formats)
+           "    access_log " log-directory "/access.log " (symbol->string log-format) ";\n"
            "    include " nginx "/share/nginx/conf/mime.types;\n"
            (if lua-package-path
                #~(format #f "    lua_package_path ~s;~%"
-- 
2.41.0





             reply	other threads:[~2024-02-26  1:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-26  0:29 Leo Nikkilä via Guix-patches via [this message]
2024-02-26  1:16 ` [bug#69399] [PATCH v2] services: nginx: Make log formats configurable Leo Nikkilä via Guix-patches via

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240226003615.21481-1-hello@lnikki.la \
    --to=guix-patches@gnu.org \
    --cc=69399@debbugs.gnu.org \
    --cc=hello@lnikki.la \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.