From b4d58afcdc35cd2b8aeee07bb15a69057d323419 Mon Sep 17 00:00:00 2001
From: Lars Johansson <lars.johansson@ess.eu>
Date: Mon, 29 Aug 2022 13:29:37 +0200
Subject: [PATCH] Externalize logging configuration

Externalize logging configuration.
Rearrange application.properties content.
Update README file.
---
 README.md                                     |  4 +--
 .../openepics/names/service/LogService.java   | 31 ++++++++++++-------
 src/main/resources/application.properties     | 22 ++++++++-----
 3 files changed, 36 insertions(+), 21 deletions(-)

diff --git a/README.md b/README.md
index de760d1b..767e122d 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@ The purpose of Naming is to handle Naming of ESS wide physical and logical devic
 
 Naming backend is a web application implemented as a REST style web service backed by a relational database. The web service is implemented as a Spring Boot application and the database is available as PostgreSQL.
 
-Background and motivation, application and database design, and more is available through content in docs folder.
+Background and motivation, application and database design, and more, is available through content in docs folder.
 
 ### Environment
 
@@ -63,7 +63,7 @@ JUnit tests start a docker container for the application (Naming backend) and an
             .waitingFor(ITUtil.NAMING, Wait.forLogMessage(".*Started NamingApplication.*", 1));
 ```
 
-Thereafter a number of http requests (GET) and curl commands (PUT, POST, PATCH, DELETE) are run towards the application to test behavior (CRUD - create, read, update, delete) and replies are received and checked if content is as expected.
+Thereafter a number of http requests (GET) and curl commands (POST, PUT, PATCH, DELETE) are run towards the application to test behavior (CRUD - create, read, update, delete) and replies are received and checked if content is as expected.
 
 #### Note
 
diff --git a/src/main/java/org/openepics/names/service/LogService.java b/src/main/java/org/openepics/names/service/LogService.java
index 877e3adb..6448ad97 100644
--- a/src/main/java/org/openepics/names/service/LogService.java
+++ b/src/main/java/org/openepics/names/service/LogService.java
@@ -23,7 +23,6 @@ import java.util.logging.Logger;
 
 import org.apache.commons.lang3.StringUtils;
 import org.openepics.names.service.exception.ServiceException;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
@@ -35,15 +34,23 @@ import org.springframework.stereotype.Service;
 @Service
 public class LogService {
 
-    private static final String ORG_OPENEPICS_NAMES                     = "org.openepics.names";
-    private static final String ORG_OPENEPICS_NAMES_UTIL_EXCEPTION_UTIL = "org.openepics.names.util.ExceptionUtil";
-
-    private int logStackTraceLength;
-
-    @Autowired
-    protected LogService(@Value("${logutil.stacktrace.length:10}") int logStackTraceLength) {
-        this.logStackTraceLength = logStackTraceLength;
-    }
+    /**
+     * Number of elements from stacktrace to log. Default is 10.
+     */
+    @Value("${naming.logging.stacktrace.length:10}")
+    int loggingStackTraceLength;
+    /**
+     * String to be contained in stacktrace (row) in order for stacktrace to be included in log.
+     * If filter is empty, then stacktrace is included.
+     */
+    @Value("${naming.logging.filter.include:org.openepics.names}")
+    String loggingFilterInclude;
+    /**
+     * String to be contained in stacktrace (row) in order for stacktrace to be excluded from log.
+     * If filter is empty, then stacktrace is included.
+     */
+    @Value("${naming.logging.filter.exclude:org.openepics.names.util.ExceptionUtil}")
+    String loggingFilterExclude;
 
     /**
      * Log service exception.
@@ -79,7 +86,7 @@ public class LogService {
      * @param e service exception
      */
     public void logStackTraceElements(Logger logger, Level level, ServiceException e) {
-        logStackTraceElements(logger, level, e, logStackTraceLength, ORG_OPENEPICS_NAMES, ORG_OPENEPICS_NAMES_UTIL_EXCEPTION_UTIL);
+        logStackTraceElements(logger, level, e, loggingStackTraceLength, loggingFilterInclude, loggingFilterExclude);
     }
 
     /**
@@ -90,7 +97,7 @@ public class LogService {
      * @param e exception
      */
     public void logStackTraceElements(Logger logger, Level level, Exception e) {
-        logStackTraceElements(logger, level, e, logStackTraceLength, ORG_OPENEPICS_NAMES, ORG_OPENEPICS_NAMES_UTIL_EXCEPTION_UTIL);
+        logStackTraceElements(logger, level, e, loggingStackTraceLength, loggingFilterInclude, loggingFilterExclude);
     }
 
     /**
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index fd0e35c8..010fee7f 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -2,18 +2,25 @@
 # Naming
 # ------------------------------------------------------------------------------
 
-# logging
-logging.level.org.openepics.names=DEBUG
-logutil.stacktrace.length=10
-
+# server
+#     port
 server.port=8083
 
+# logging
+naming.logging.stacktrace.length=10
+logging.level.org.openepics.names=INFO
+logging.level.org.springframework.web=INFO
+spring.http.log-request-details=true
+
+# spring
+#     profiles
+#     config
+#     datasource
+#     jpa
 spring.profiles.active=@spring.profiles.active@
 
-#Use ENV file
 spring.config.import=optional:file:.env[.properties]
 
-# datasource
 spring.datasource.url=${NAMING_DATABASE_URL:jdbc:postgresql://postgres:5432/discs_names}
 spring.datasource.username=${NAMING_DATABASE_USERNAME:discs_names}
 spring.datasource.password=${NAMING_DATABASE_PASSWORD:discs_names}
@@ -22,7 +29,8 @@ spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
 spring.jpa.hibernate.ddl-auto=none
 spring.jpa.hibernate.show-sql=true
 
-# swagger uI
+# open api, swagger ui
+#     doc
 app.version=0.0.1-SNAPSHOT
 openapi.externaldocs.description=ESS Naming Convention
 openapi.externaldocs.url=https://chess.esss.lu.se/enovia/link/ESS-0000757/21308.51166.45568.45993/valid
-- 
GitLab