Skip to content

Commit

Permalink
feat: logging restriction to errors (#1472)
Browse files Browse the repository at this point in the history
* with the `LOG_ALL: off` environment setting the logs are no longer flooded with less relevant information, only the errors to act on should be logged
  • Loading branch information
dhhyi authored Sep 8, 2023
1 parent 7c160ce commit 2d0bf20
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ services:
environment:
# ICM_BASE_URL:
LOGGING: 'true'
# LOG_ALL: 'false'
SOURCE_MAPS: 'true'
TRUST_ICM: 'true'
# PROXY_ICM: 'true'
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/guides/nginx-startup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions docs/guides/ssr-startup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
1 change: 1 addition & 0 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions nginx/features/log_all-off.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
map $status $loggable {
~^[23] 0;
default 1;
}
3 changes: 3 additions & 0 deletions nginx/features/log_all.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
map $status $loggable {
default 1;
}
1 change: 1 addition & 0 deletions nginx/features/prometheus.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
server {
listen 8080;
access_log off;
location /stub_status {
stub_status;
}
Expand Down
2 changes: 1 addition & 1 deletion nginx/templates/multi-channel.conf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 7 additions & 3 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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),
})
);
}
Expand Down Expand Up @@ -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}`);
}

Expand Down Expand Up @@ -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);
}
Expand Down
11 changes: 9 additions & 2 deletions src/app/core/interceptors/universal-log.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
if (!/on|1|true|yes/.test(process.env.LOGGING?.toLowerCase())) {
if (!this.logging) {
return next.handle(req);
}

Expand All @@ -21,7 +25,10 @@ export class UniversalLogInterceptor implements HttpInterceptor {
const start = performance.now();

const logger = (res: HttpEvent<unknown>) => {
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}` : '';

Expand Down

0 comments on commit 2d0bf20

Please sign in to comment.