Skip to content

Commit

Permalink
Add new logger with log4j2
Browse files Browse the repository at this point in the history
The logger includes prefixes with the service name and the federation, read from env variables.
  • Loading branch information
ThanKarab committed Oct 23, 2024
1 parent 002f1ff commit ffa9b10
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
9 changes: 7 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#######################################################
# Build the spring boot maven project
#######################################################
FROM maven:3.9.3-amazoncorretto-17 as mvn-build-env
FROM maven:3.9.3-amazoncorretto-17 AS mvn-build-env
MAINTAINER Thanasis Karampatsis <[email protected]>

ENV CODE_PATH="/opt/code"
Expand Down Expand Up @@ -33,14 +33,19 @@ ENV APP_CONFIG_TEMPLATE="/opt/config/application.tmpl"
ENV APP_CONFIG_LOCATION="/opt/config/application.yml"
ENV SPRING_CONFIG_LOCATION="file:/opt/config/application.yml"

ENV SERVICE="portal-backend"
ENV FEDERATION="default"
ENV LOG_LEVEL="INFO"
ENV FRAMEWORK_LOG_LEVEL="INFO"

WORKDIR /opt

RUN apk add --no-cache curl

#######################################################
# Install dockerize
#######################################################
ENV DOCKERIZE_VERSION v0.6.1
ENV DOCKERIZE_VERSION=v0.6.1
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& rm dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz
Expand Down
10 changes: 0 additions & 10 deletions config/application.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ server:
forward-headers-strategy: native


### LOG LEVELS ###
logging:
level:
root: {{ default .Env.LOG_LEVEL_FRAMEWORK "ERROR" }}
eu:
hbp: {{ default .Env.LOG_LEVEL "INFO" }}
file:
name: /var/log/portalbackend/portalbackend.log


### DATABASE CONFIGURATION ###
spring:
datasource:
Expand Down
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
9 changes: 0 additions & 9 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ server:
port: 8080
forward-headers-strategy: native

### LOG LEVELS ###
logging:
level:
root: "INFO"
eu:
hbp: "DEBUG"
file:
name: /var/log/portalbackend/portalbackend.log

### DATABASE CONFIGURATION ###
spring:
datasource:
Expand Down
27 changes: 27 additions & 0 deletions src/main/resources/log4j2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Configuration:
name: Default

Properties:
Property:
name: log-path
value: "logs"

Appenders:
Console:
name: ConsoleAppender
target: SYSTEM_OUT
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss.SSS} - %-5level - %logger{36} - [${env:FEDERATION:-default}] - [${env:SERVICE:-portal-backend}] - %msg%n"

Loggers:
logger:
- name: hbp.mip
level: ${env:LOG_LEVEL:-INFO}
additivity: false # Prevent log propagation to parent loggers
AppenderRef:
- ref: ConsoleAppender

Root:
level: ${env:LOG_LEVEL_FRAMEWORK:-INFO}
AppenderRef:
- ref: ConsoleAppender

0 comments on commit ffa9b10

Please sign in to comment.