1
0

tex: enhance logging section

This commit is contained in:
surtur 2023-08-14 18:34:40 +02:00
parent aa2d7ed236
commit fbab037f96
Signed by: wanderer
SSH Key Fingerprint: SHA256:MdCZyJ2sHLltrLBp0xQO0O1qTW9BT/xl5nXkDvhlMCI

@ -275,12 +275,49 @@ package to prevent accidental imports.
\n{2}{Logging}
The program uses \emph{dependency injection} to share a single logger instance,
similar applies to the database client. These are passed around as a pointer,
so the underlying data stays the same. As a rule of thumb throughout the
The program uses \emph{dependency injection} to share a single logger instance
(the same technique is also used to share the database client). This logger is
then passed around as a pointer, so that the underlying data stays the same or
is modified concurrently for all consumers. As a rule of thumb throughout the
application, every larger \texttt{struct} that needs to be passed around is
passed around as a pointer.
An experimental (note: not anymore in \texttt{go1.21}, it was just recently
brought into Go's \textit{stdlib}) library for \textit{structured} logging
\texttt{slog} was used to facilitate every logging need the program might have.
It supports both JSON and plain-text logging, which was made configurable by
the program. Either a configuration file value or an environment variable can
be used to set this.
There are four log levels available by default (\texttt{DEBUG}, \texttt{INFO},
\texttt{WARNING}, \texttt{ERROR}) and the pertinent library funtions are
parametric. The first parameter of type \texttt{string} is the main message,
that is supplied as a \emph{value} to the \emph{key} named appropriately
`\texttt{msg}', a feature of structured loggers which can later be used for
filtering. Any other parameters need to be supplied in pairs, serving as key
and value, respectively.
This main \texttt{slog} interface has been extended in package
\texttt{slogging} to also provide the formatting functionality of the
\texttt{fmt} standard library package. This was achieved by directly embedding
\texttt{slog.Logger} in a custom \texttt{struct} type named \texttt{Slogger}
and implementing the additional methods on the custom type. The new type that
embeds the original \texttt{slog.Logger} gets to keep its methods thanks to the
composition nature of Go. Thus, common formatting directives like the one seen
in Listing~\ref{goFmtExpression} are now supported with the custom logger, in
addition to anything the base \texttt{slog.Logger} offers.
\vspace{\parskip}
\begin{lstlisting}[language=Go, caption={Example formatting expression supplied
to the logger}, label=goFmtExpression, basicstyle=\linespread{0.9}\small\ttfamily,
backgroundcolor=\color{lstbg},
]
slogger.Debugf("operation %q for user %q completed at %s", op, usr.ID, time.Now())
\end{lstlisting}
Furthermore, functionality was added to support changing the log level at
runtime, which is a convenient feature in certain situations.
\n{2}{Authentication}