Security Logging Fields 63/129063/9
authorsourabh_sourabh <sourabh.sourabh@est.tech>
Wed, 4 May 2022 10:53:01 +0000 (11:53 +0100)
committersourabh_sourabh <sourabh.sourabh@est.tech>
Fri, 6 May 2022 12:43:46 +0000 (13:43 +0100)
- logback encoder and sleuth dependencies are added.
- New appender is added to log JSON format content on console.

Issue-ID: CPS-986
Signed-off-by: sourabh_sourabh <sourabh.sourabh@est.tech>
Change-Id: I9e45a9d842831a87af3fb9ac309485110357f0c8

pom.xml
src/main/resources/application.yml
src/main/resources/logback-spring.xml [new file with mode: 0644]
src/main/resources/logback.xml [deleted file]
src/test/resources/application.yml

diff --git a/pom.xml b/pom.xml
index 9b6612f..a2931f7 100755 (executable)
--- a/pom.xml
+++ b/pom.xml
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
+            <dependency>
+                <groupId>org.springframework.cloud</groupId>
+                <artifactId>spring-cloud-starter-sleuth</artifactId>
+                <version>3.1.1</version>
+            </dependency>
             <dependency>
                 <groupId>org.spockframework</groupId>
                 <artifactId>spock-bom</artifactId>
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
+            <dependency>
+                <groupId>net.logstash.logback</groupId>
+                <artifactId>logstash-logback-encoder</artifactId>
+                <version>7.0.1</version>
+            </dependency>
+            <dependency>
+                <groupId>org.codehaus.janino</groupId>
+                <artifactId>janino</artifactId>
+                <version>3.1.7</version>
+            </dependency>
+            <dependency>
+                <groupId>com.vladmihalcea</groupId>
+                <artifactId>hibernate-types-52</artifactId>
+                <version>2.10.0</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 
         <dependency>
             <groupId>com.vladmihalcea</groupId>
             <artifactId>hibernate-types-52</artifactId>
-            <version>2.10.0</version>
         </dependency>
         <dependency>
             <groupId>org.liquibase</groupId>
             <version>0.18.0</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-sleuth</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>net.logstash.logback</groupId>
+            <artifactId>logstash-logback-encoder</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.codehaus.janino</groupId>
+            <artifactId>janino</artifactId>
+        </dependency>
     </dependencies>
 
     <build>
index 7db9510..9b3be03 100755 (executable)
@@ -25,6 +25,8 @@ rest:
         base-path: /cps-temporal/api
 
 spring:
+    application:
+        name: cps-temporal
     datasource:
         url: jdbc:postgresql://${DB_HOST}:${DB_PORT}/cpstemporaldb
         username: ${DB_USERNAME}
@@ -90,4 +92,8 @@ management:
             show-details: always
             # kubernetes probes: liveness and readiness
             probes:
-                enabled: true
\ No newline at end of file
+                enabled: true
+
+logging:
+    format: json
+    
\ No newline at end of file
diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml
new file mode 100644 (file)
index 0000000..c71c9a6
--- /dev/null
@@ -0,0 +1,74 @@
+<!--
+  ============LICENSE_START=======================================================
+  Copyright (c) 2021 Bell Canada.
+  Modifications Copyright (C) 2022 Nordix Foundation
+  ================================================================================
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+        http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+  ============LICENSE_END=========================================================
+-->
+
+<configuration scan="true" scanPeriod="30 seconds" debug="false">
+
+    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
+    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
+
+    <springProperty scope="context" name="springAppName" source="spring.application.name"/>
+    <springProperty scope="context" name="username" source="security.auth.username"/>
+    <springProperty scope="context" name="loggingFormat" source="logging.format"/>
+
+    <property name="currentTimeStamp" value="%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX,UTC}"/>
+
+    <appender name="jsonConsole"
+              class="ch.qos.logback.core.ConsoleAppender">
+        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
+            <providers>
+                <pattern>
+                    <omitEmptyFields>true</omitEmptyFields>
+                    <pattern>
+                        {
+                        "logTimeStamp": "${currentTimeStamp:-}",
+                        "logTypeName": "",
+                        "logLevel": "%level",
+                        "traceId": "%X{traceId:-}",
+                        "statusCode": "",
+                        "principalId": "${username:-}",
+                        "serviceName": "${springAppName:-}",
+                        "message": "%message",
+                        "spanId": "%X{spanId:-}",
+                        "processId": "${PID:-}",
+                        "threadName": "%thread",
+                        "class": "%logger{40}",
+                        "exception": "%wEx"
+                        }
+                    </pattern>
+                </pattern>
+            </providers>
+        </encoder>
+    </appender>
+
+    <appender name="asyncConsole" class="ch.qos.logback.classic.AsyncAppender">
+        <if condition='property("loggingFormat").equalsIgnoreCase("json")'>
+            <then>
+                <appender-ref ref="jsonConsole"/>
+            </then>
+            <else>
+                <appender-ref ref="CONSOLE"/>
+            </else>
+        </if>
+    </appender>
+
+    <root level="INFO">
+        <appender-ref ref="asyncConsole"/>
+    </root>
+
+</configuration>
diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml
deleted file mode 100644 (file)
index a75b7aa..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-<!--
-  ============LICENSE_START=======================================================
-  Copyright (c) 2021 Bell Canada.
-  ================================================================================
-  Licensed under the Apache License, Version 2.0 (the "License");
-  you may not use this file except in compliance with the License.
-  You may obtain a copy of the License at
-
-        http://www.apache.org/licenses/LICENSE-2.0
-
-  Unless required by applicable law or agreed to in writing, software
-  distributed under the License is distributed on an "AS IS" BASIS,
-  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-  See the License for the specific language governing permissions and
-  limitations under the License.
-  ============LICENSE_END=========================================================
--->
-
-<configuration>
-
-    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
-        <encoder>
-            <pattern>%d - %highlight(%-5level) [%-20.20thread] %cyan(%logger{36}) - %msg%n</pattern>
-        </encoder>
-    </appender>
-
-    <!-- Logger for cps classes -->
-    <logger name="org.onap.cps" level="info"/>
-
-    <!-- Logger for sql statements. Set to info to disable, debug to enable -->
-    <logger name="org.hibernate.SQL" level="info"/>
-
-    <!-- Logger for sql bindings. Set to info to disable, to trace to enable -->
-    <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="info"/>
-
-    <!-- Logger for hibernate statistics. Set to warn to disable, to info to enable -->
-    <logger name="org.hibernate.engine.internal.StatisticalLoggingSessionEventListener" level="warn"/>
-
-    <root level="info">
-        <appender-ref ref="STDOUT" />
-    </root>
-
-</configuration>
index 9bdacbe..ce0e95b 100644 (file)
@@ -24,6 +24,8 @@ rest:
         base-path: /cps-temporal/api
 
 spring:
+    application:
+        name: cps-temporal
     datasource:
         url: ${DB_URL}
         username: ${DB_USERNAME}
@@ -71,3 +73,6 @@ security:
     auth:
         username: testUser
         password: testPassword
+
+logging:
+    format: json