diff --git a/Dockerfile b/Dockerfile index f2bb0765b4..e9fd742e41 100644 --- a/Dockerfile +++ b/Dockerfile @@ -34,6 +34,7 @@ RUN cd dist && npm install ARG displayVersion= LABEL displayVersion="${displayVersion}" ENV DISPLAY_VERSION=${displayVersion} NODE_PATH=/dist/node_modules PATH=$PATH:/dist/node_modules/.bin +ENV LOG_ALL=on EXPOSE 4200 9113 RUN mkdir /.pm2 && chmod 777 -Rf /.pm2 && touch /dist/ecosystem.yml && chmod 777 -f /dist/ecosystem.yml USER nobody diff --git a/docker-compose.yml b/docker-compose.yml index 2cf0af67a0..ca1ce2ab46 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,6 +15,7 @@ services: environment: # ICM_BASE_URL: LOGGING: 'true' + # LOG_ALL: 'false' SOURCE_MAPS: 'true' TRUST_ICM: 'true' # PROXY_ICM: 'true' @@ -68,6 +69,7 @@ services: UPSTREAM_PWA: 'http://pwa:4200' NGINX_ENTRYPOINT_QUIET_LOGS: ANYVALUE CACHE: 0 + # LOG_ALL: off # SSL: 1 # SSR: 0 # DEBUG: 1 diff --git a/docs/guides/nginx-startup.md b/docs/guides/nginx-startup.md index c365ff44ae..a7961ad7c9 100644 --- a/docs/guides/nginx-startup.md +++ b/docs/guides/nginx-startup.md @@ -146,6 +146,8 @@ Built-in features can be enabled and disabled: - `DEVICE_DETECTION=off` disables user-agent detection (default `on`) - `PROMETHEUS=on` enables [Prometheus](https://prometheus.io) metrics exports on port `9113` (default `off`) - `SSL=on` to switch on HTTPS. See [HTTPS or SSL](#https-or-ssl) above for further explanation. +- `DEBUG=on` to log extra information like path matching. +- `LOG_ALL=off` to restrict logging to errors. ## Features diff --git a/docs/guides/ssr-startup.md b/docs/guides/ssr-startup.md index e95cd44550..a7fc277b5e 100644 --- a/docs/guides/ssr-startup.md +++ b/docs/guides/ssr-startup.md @@ -58,6 +58,7 @@ Make sure to use them as written in the table below. | | DEPLOY_URL | string | Set a [Deploy URL][concept-deploy-url] (default `/`) | | **Debug** :warning: | TRUST_ICM | any | Use this if ICM is deployed with an insecure certificate | | | LOGGING | switch | Enables extra log output | +| | LOG_ALL | switch | Logs success and error messages (if false, only errors) | | | SOURCE_MAPS | switch | Exposes source maps if activated | | **Hybrid Approach** | SSR_HYBRID | any | Enables running PWA and ICM in the [Hybrid Approach][concept-hybrid] | | | SSR_HYBRID_BACKEND | URL | When running in K8S, this contains the ICM WA service URL. For none K8S you can use ICM_BASE_URL | diff --git a/nginx/Dockerfile b/nginx/Dockerfile index c0e6fdba46..fec0b4797a 100644 --- a/nginx/Dockerfile +++ b/nginx/Dockerfile @@ -20,5 +20,6 @@ ENV SSL=off ENV CACHE_DURATION_NGINX_OK=10m ENV CACHE_DURATION_NGINX_NF=60m ENV LOGFORMAT=main +ENV LOG_ALL=on EXPOSE 80 443 9113 diff --git a/nginx/features/log_all-off.conf b/nginx/features/log_all-off.conf new file mode 100644 index 0000000000..3d5e8d7e53 --- /dev/null +++ b/nginx/features/log_all-off.conf @@ -0,0 +1,4 @@ +map $status $loggable { + ~^[23] 0; + default 1; +} diff --git a/nginx/features/log_all.conf b/nginx/features/log_all.conf new file mode 100644 index 0000000000..a32dea9204 --- /dev/null +++ b/nginx/features/log_all.conf @@ -0,0 +1,3 @@ +map $status $loggable { + default 1; +} diff --git a/nginx/features/prometheus.conf b/nginx/features/prometheus.conf index 04e8baa140..7eaf11ea63 100644 --- a/nginx/features/prometheus.conf +++ b/nginx/features/prometheus.conf @@ -1,5 +1,6 @@ server { listen 8080; + access_log off; location /stub_status { stub_status; } diff --git a/nginx/templates/multi-channel.conf.tmpl b/nginx/templates/multi-channel.conf.tmpl index 2ed5b5fbee..8ad50d04a8 100644 --- a/nginx/templates/multi-channel.conf.tmpl +++ b/nginx/templates/multi-channel.conf.tmpl @@ -96,7 +96,7 @@ server { {{- end }} access_log /var/log/nginx-access.log {{ getenv "LOGFORMAT" }}; - access_log /dev/stdout {{ getenv "LOGFORMAT" }}; + access_log /dev/stdout {{ getenv "LOGFORMAT" }} if=$loggable; include /etc/nginx/conf.d/cache-blacklist.conf; diff --git a/server.ts b/server.ts index 33d7607b1e..8c2a8d40ac 100644 --- a/server.ts +++ b/server.ts @@ -96,6 +96,7 @@ global['navigator'] = win.navigator; // The Express app is exported so that it can be used by serverless Functions. export function app() { const logging = /on|1|true|yes/.test(process.env.LOGGING?.toLowerCase()); + const log_all = /on|1|true|yes/.test(process.env.LOG_ALL?.toLowerCase()); const ICM_BASE_URL = process.env.ICM_BASE_URL || environment.icmBaseURL; @@ -197,7 +198,8 @@ export function app() { } server.use( morgan(logFormat, { - skip: (req: express.Request) => req.originalUrl.startsWith('/INTERSHOP/static'), + skip: (req: express.Request, res: express.Response) => + req.originalUrl.startsWith('/INTERSHOP/static') || (res.statusCode < 400 && !log_all), }) ); } @@ -392,7 +394,7 @@ export function app() { ); const angularUniversal = (req: express.Request, res: express.Response) => { - if (logging) { + if (logging && log_all) { console.log(`SSR ${req.originalUrl}`); } @@ -445,7 +447,9 @@ export function app() { res.status(500).send(err.message); } if (logging) { - console.log(`RES ${res.statusCode} ${req.originalUrl}`); + if (log_all || res.statusCode >= 400) { + console.log(`RES ${res.statusCode} ${req.originalUrl}`); + } if (err) { console.log(err); } diff --git a/src/app/core/interceptors/universal-log.interceptor.ts b/src/app/core/interceptors/universal-log.interceptor.ts index d7a77667f3..f4fe8e37e2 100644 --- a/src/app/core/interceptors/universal-log.interceptor.ts +++ b/src/app/core/interceptors/universal-log.interceptor.ts @@ -10,8 +10,12 @@ import { Observable } from 'rxjs'; import { tap } from 'rxjs/operators'; export class UniversalLogInterceptor implements HttpInterceptor { + private logging = /on|1|true|yes/.test(process.env.LOGGING?.toLowerCase()); + + private logAll = /on|1|true|yes/.test(process.env.LOG_ALL?.toLowerCase()); + intercept(req: HttpRequest, next: HttpHandler): Observable> { - if (!/on|1|true|yes/.test(process.env.LOGGING?.toLowerCase())) { + if (!this.logging) { return next.handle(req); } @@ -21,7 +25,10 @@ export class UniversalLogInterceptor implements HttpInterceptor { const start = performance.now(); const logger = (res: HttpEvent) => { - if (res instanceof HttpResponse || res instanceof HttpErrorResponse) { + if ( + (res instanceof HttpResponse || res instanceof HttpErrorResponse) && + (this.logAll || res.status >= 400 || res.status < 200) + ) { const duration = (performance.now() - start).toFixed(2); const size = res instanceof HttpResponse ? ` ${JSON.stringify(res.body)?.length}` : '';