Skip to content

Commit

Permalink
logging slf4j logback and install openssl in dockerfile needed for ap…
Browse files Browse the repository at this point in the history
…i-server startup
  • Loading branch information
0xbase12 committed Apr 8, 2024
1 parent b4bbc59 commit e97e06f
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 53 deletions.
40 changes: 21 additions & 19 deletions code/project.clj
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
(def parent-version "6.7.15")
(def parent-version "6.8.0")
(def slf4j-version "2.0.12")
(def logback-version "1.5.3")

(defproject sixsq.nuvla.ring/code "2.1.1-SNAPSHOT"

:description "simple ring container for micro-services"

:url "https://github.com/nuvla/server"

:license {:name "Apache 2.0"
:url "http://www.apache.org/licenses/LICENSE-2.0.txt"
:license {:name "Apache 2.0"
:url "http://www.apache.org/licenses/LICENSE-2.0.txt"
:distribution :repo}

:plugins [[lein-parent "0.3.9"]]

:parent-project {:coords [sixsq.nuvla/parent ~parent-version]
:inherit [:plugins
:min-lein-version
:managed-dependencies
:repositories
:deploy-repositories]}

Expand All @@ -28,19 +28,21 @@
:aot [sixsq.nuvla.server.ring]

:dependencies
[[aleph]
[environ]
[log4j]
[org.clojure/clojure]
[org.clojure/tools.logging]
[org.slf4j/slf4j-api]
[org.slf4j/slf4j-log4j12]
[ring/ring-core] ;; added to force version
]
[[aleph "0.7.1"]
[environ "1.2.0"]
[ring/ring-core "1.12.1"]
[org.clojure/clojure "1.11.2"]
[org.clojure/tools.logging "1.3.0"]
[org.slf4j/slf4j-api ~slf4j-version]
[org.slf4j/log4j-over-slf4j ~slf4j-version]
[org.slf4j/jul-to-slf4j ~slf4j-version]
[org.slf4j/jcl-over-slf4j ~slf4j-version]
[ch.qos.logback/logback-classic ~logback-version]
[ch.qos.logback/logback-core ~logback-version]]

:profiles
{:test {:source-paths ["test"]
:resource-paths ["test-resources"]
:plugins [[lein-test-report-junit-xml "0.2.0"]]
:test-report-junit-xml {:output-dir "test-reports"}}
:dev {:dependencies [[clj-kondo "RELEASE"]]}})
{:test {:source-paths ["test"]
:resource-paths ["test-resources"]
:plugins [[lein-test-report-junit-xml "0.2.0"]]
:test-report-junit-xml {:output-dir "test-reports"}}
:dev {:dependencies [[clj-kondo "RELEASE"]]}})
36 changes: 16 additions & 20 deletions code/src/sixsq/nuvla/server/ring.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"Provides a simple, generic ring application server that can be started
either via the static 'main' function (e.g. for running as a daemon) or via
the 'start' function (e.g. for testing from the REPL)."
(:require
[clojure.string :as str]
[clojure.tools.logging :as log])
(:import
[java.io Closeable]
[java.net InetSocketAddress])
(:require [aleph.http :refer [start-server]]
[clojure.string :as str]
[clojure.tools.logging :as log]
[environ.core :refer [env]])
(:import [java.io Closeable]
[java.net InetSocketAddress])
(:gen-class))


Expand Down Expand Up @@ -60,11 +60,10 @@


(defn- register-shutdown-hook
"Registers a shutdown hook in the JVM to shutdown the application and
"Registers a shutdown hook in the JVM to shut down the application and
application container cleanly."
[shutdown-fn]
(.. (Runtime/getRuntime)
(addShutdownHook (hook shutdown-fn))))
(.addShutdownHook (Runtime/getRuntime) (hook shutdown-fn)))


(defn- create-shutdown-fn
Expand All @@ -86,19 +85,17 @@
"Starts the aleph container with the given ring handler and on the given
port. Returns the server object that has been started, which can be stopped
by calling 'close' on the object."
[handler ^long port host]
[handler ^long port ^String host]
(log/info "starting aleph application container on host:port" (str host ":" port))
(let [start-server (dyn-resolve 'aleph.http/start-server)
server (->> port
(InetSocketAddress. host)
(let [server (->> (InetSocketAddress. host port)
(hash-map :socket-address)
(start-server handler))]
(log/info "started aleph application container on host:port" (str host ":" port))
server))


(defn- validate-host
"If the argument is the a valid string representation of an IP address or
"If the argument is a valid string representation of an IP address or
host, the value is returned. Otherwise, the default host is returned."
[host]
(if (and (string? host) (not (str/blank? host)))
Expand All @@ -125,19 +122,19 @@

(defn start
"Starts the application and application server. Return a 'stop' function
that will shutdown the application and server when called."
that will shut down the application and server when called."
[server-init & [port host]]
(let [server-init-fn (dyn-resolve server-init)
[handler finalization-fn] (server-init-fn)
port (parse-port port)
host (validate-host host)]
port (parse-port port)
host (validate-host host)]

(log/info "starting " server-init "on" host "and port" port)
(log/info "java vendor: " (System/getProperty "java.vendor"))
(log/info "java version: " (System/getProperty "java.version"))
(log/info "java classpath: " (System/getProperty "java.class.path"))

(let [server (start-container handler port host)
(let [server (start-container handler port host)
shutdown-fn (create-shutdown-fn server finalization-fn)]

(log/info "started" server-init "on" host "and port" port)
Expand All @@ -157,8 +154,7 @@
function. Starting the server from the REPL will use the values given to the
start function."
[]
(let [env (dyn-resolve 'environ.core/env)
server-port (env :nuvla-server-port)
(let [server-port (env :nuvla-server-port)
server-host (env :nuvla-server-host)]
(if-let [server-init (env :nuvla-server-init)]
[server-init server-port server-host]
Expand Down
7 changes: 0 additions & 7 deletions code/test-resources/log4j.properties

This file was deleted.

117 changes: 117 additions & 0 deletions code/test-resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<!-- Logback Configuration. See http://logback.qos.ch/ -->
<!-- scan configuration for changes enable it by setting it to true -->
<configuration scan="true" scanPeriod="10 seconds">

<!-- <property resource="nuvla.properties"/>-->

<!-- Console (STDOUT) output. -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">

<!-- Only print log messages at level WARN or higher. -->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>

<!-- Default encoder is ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<!-- two-line layout suitable for a terminal -->
<pattern>%date - %-5level - %logger - [%thread] - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>


<!-- The output file configuration for log/all.log -->
<!-- <appender name="ALL_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">-->

<!-- &lt;!&ndash; Default encoder is ch.qos.logback.classic.encoder.PatternLayoutEncoder &ndash;&gt;-->
<!-- <encoder>-->
<!-- <pattern>%date{HH:mm:ss.SSS} %-5level %logger{25}: %msg %X thread=%thread%n</pattern>-->
<!-- </encoder>-->

<!-- &lt;!&ndash; Default location of log file is log/all.log &ndash;&gt;-->
<!-- <file>log/all.log</file>-->

<!-- <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">-->
<!-- &lt;!&ndash; Roll over log files daily &ndash;&gt;-->
<!-- <fileNamePattern>log/all.%d{yyyy-MM-dd}.%i.log</fileNamePattern>-->

<!-- &lt;!&ndash; And start a new file every 64 MB &ndash;&gt;-->
<!-- <maxFileSize>64 MB</maxFileSize>-->

<!-- &lt;!&ndash; Keep at most 15 days of history &ndash;&gt;-->
<!-- <maxHistory>15</maxHistory>-->

<!-- &lt;!&ndash; Up to a maximum of 512 MB &ndash;&gt;-->
<!-- <totalSizeCap>512MB</totalSizeCap>-->

<!-- &lt;!&ndash; Ensure short-lived processes still clean up old logs &ndash;&gt;-->
<!-- <cleanHistoryOnStart>true</cleanHistoryOnStart>-->
<!-- </rollingPolicy>-->
<!-- </appender>-->


<!-- The output file configuration for log/app.log -->
<!-- <appender name="APP_LOG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">-->

<!-- &lt;!&ndash; Default encoder is ch.qos.logback.classic.encoder.PatternLayoutEncoder &ndash;&gt;-->
<!-- <encoder>-->
<!-- <pattern>%date{HH:mm:ss.SSS} %-5level %logger{25}: %msg %X thread=%thread%n</pattern>-->
<!-- </encoder>-->

<!-- &lt;!&ndash; Default location of log file is log/app.log &ndash;&gt;-->
<!-- <file>log/app.log</file>-->

<!-- <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">-->
<!-- &lt;!&ndash; Roll over log files daily &ndash;&gt;-->
<!-- <fileNamePattern>log/app.%d{yyyy-MM-dd}.%i.log</fileNamePattern>-->

<!-- &lt;!&ndash; And start a new file every 64 MB &ndash;&gt;-->
<!-- <maxFileSize>64 MB</maxFileSize>-->

<!-- &lt;!&ndash; Keep at most 15 days of history &ndash;&gt;-->
<!-- <maxHistory>15</maxHistory>-->

<!-- &lt;!&ndash; Up to a maximum of 512 MB &ndash;&gt;-->
<!-- <totalSizeCap>512MB</totalSizeCap>-->

<!-- &lt;!&ndash; Ensure short-lived processes still clean up old logs &ndash;&gt;-->
<!-- <cleanHistoryOnStart>true</cleanHistoryOnStart>-->
<!-- </rollingPolicy>-->
<!-- </appender>-->


<!-- Root log level is "ALL", meaning all log levels are emitted. -->
<root level="ALL">

<!-- Send all log messages to console (filtered to WARN) -->
<appender-ref ref="CONSOLE"/>

<!-- &lt;!&ndash; Send all log messages to log/all.log &ndash;&gt;-->
<!-- <appender-ref ref="ALL_LOG_FILE" />-->
</root>


<!-- Log messages from your application will be included in
log/all.log. In addition, we will send just messages from your
application to log/app.log -->
<!-- <logger name="${app_root_logger:-com.example.application}" level="ALL">-->
<!-- <appender-ref ref="APP_LOG_FILE" />-->
<!-- </logger>-->

<!-- If you have a REPL or interactive shell with a logger named
'user' or 'dev', send those messages to log/app.log too. -->
<!-- <logger name="user" level="ALL">-->
<!-- <appender-ref ref="APP_LOG_FILE" />-->
<!-- </logger>-->
<!-- <logger name="dev" level="ALL">-->
<!-- <appender-ref ref="APP_LOG_FILE" />-->
<!-- </logger>-->


<!-- Make java.util.logging more efficient at disabled levels.
See http://logback.qos.ch/manual/configuration.html#LevelChangePropagator -->
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Provides a simple ring example to ensure that generic ring container works.")


(defn handler [request]
(defn handler [_request]
{:status 200
:headers {"Content-Type" "text/plain"}
:body "Ring Example Running!\n"})
Expand Down
18 changes: 13 additions & 5 deletions code/test/sixsq/nuvla/server/ring_test.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
(ns sixsq.nuvla.server.ring-test
(:require
[clojure.test :refer [are deftest is]]
[sixsq.nuvla.server.ring :as t])
(:import
(clojure.lang ExceptionInfo)))
(:require [clojure.test :refer [are deftest is]]
[sixsq.nuvla.server.ring :as t])
(:import (clojure.lang ExceptionInfo)))


(deftest check-as-symbol
Expand Down Expand Up @@ -50,3 +48,13 @@
10 10
10 "10")))

(deftest start
(let [shutdown-fn (t/start 'sixsq.nuvla.server.example/init)]
(is (true? (fn? shutdown-fn)))
(shutdown-fn)))

(deftest register-shutdown-hook
(#'t/register-shutdown-hook prn))

(deftest server-cfg
(is (thrown-with-msg? ExceptionInfo #"NUVLA_SERVER_INIT is not defined" (#'t/server-cfg))))
6 changes: 5 additions & 1 deletion container/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ COPY target/dependency/lib /opt/nuvla/server/lib
COPY src/scripts/log4j.properties /opt/nuvla/server/resources/
COPY src/scripts/start.sh /opt/nuvla/server/bin/
RUN chmod 0755 /opt/nuvla/server/bin/start.sh \
&& mkdir -p /opt/nuvla/server/lib.d/example
&& mkdir -p /opt/nuvla/server/lib.d/

RUN apk upgrade --update-cache --available && \
apk add openssl && \
rm -rf /var/cache/apk/*

ENV NUVLA_SERVER_INIT=sixsq.nuvla.server.example/init

Expand Down

0 comments on commit e97e06f

Please sign in to comment.