Skip to content

Commit

Permalink
https://circleci.com/gh/luminus-framework/luminus/55
Browse files Browse the repository at this point in the history
  • Loading branch information
yogthos committed Sep 2, 2023
1 parent 28f6bd2 commit dd186ef
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
23 changes: 21 additions & 2 deletions docs/database.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ <h1>
<h1>Database Access</h1>

<h2>Contents</h2>
<ol class="contents"><li><a href="#configuring_the_database">Configuring the Database</a></li><li><a href="#configuring_migrations">Configuring Migrations</a></li><li><a href="#setting_up_the_database_connection">Setting up the database connection</a></li><li><a href="#translating_sql_types">Translating SQL types</a></li><li><a href="#working_with_hugsql">Working with HugSQL</a></li><li><a href="#massaging_key_names_from_sql_to_clojure_style">Massaging key names from SQL to Clojure style</a></li></ol>
<ol class="contents"><li><a href="#configuring_the_database">Configuring the Database</a></li><li><a href="#configuring_migrations">Configuring Migrations</a></li><li><a href="#setting_up_the_database_connection">Setting up the database connection</a></li><li><a href="#translating_sql_types">Translating SQL types</a></li><li><a href="#working_with_hugsql">Working with HugSQL</a></li><li><a href="#massaging_key_names_from_sql_to_clojure_style">Massaging key names from SQL to Clojure style</a></li><li><a href="#logging_sql_queries">Logging SQL queries</a></li></ol>

<div id="content">
<h2 id="configuring&#95;the&#95;database">Configuring the Database</h2><p>Luminus defaults to using <a href='https://github.com/yogthos/migratus'>Migratus</a> for database migrations and <a href='http://www.hugsql.org/'>HugSQL</a> for database interaction. The migrations and a default connection will be setup when using a database profile such as <code>+postgres</code>.</p><h3 id="configuring&#95;migrations">Configuring Migrations</h3><p>We first have to set the connection strings for our database in <code>dev-config.edn</code> and <code>test-config.edn</code> files. These files come with a generated configuration for development and testing respectively:</p><h4 id="<code>dev-config.edn</code>:"><code>dev-config.edn</code>:</h4><pre><code class="clojure">{:database-url &quot;jdbc:postgresql://localhost/my&#95;app&#95;dev?user=db&#95;user&amp;password=db&#95;password&quot;}
Expand Down Expand Up @@ -149,7 +149,26 @@ <h2 id="configuring&#95;the&#95;database">Configuring the Database</h2><p>Luminu

&#40;defmethod hugsql.core/hugsql-result-fn :many &#91;sym&#93;
'yuggoth.db.core/result-many-snake-&gt;kebab&#41;
</code></pre><p>See the <a href='http://www.hugsql.org/'>official documentation</a> for more details.</p>
</code></pre><p>See the <a href='http://www.hugsql.org/'>official documentation</a> for more details.</p><h3 id="logging&#95;sql&#95;queries">Logging SQL queries</h3><p>You can use <a href='https://cljdoc.org/d/com.github.seancorfield/next.jdbc/1.3.883/api/next.jdbc#with-logging'>next.jdbc/with-logging</a> in order to log SQL queries in dev environment:</p><pre><code class="clojure">&#40;ns cljapp.db.core
...&#41;

&#40;defn with-logging &#91;connection&#93;
&#40;next.jdbc/with-logging connection
&#40;fn &#91;sym sql-params&#93;
&#40;log/debug &#40;str sym &quot; &quot; sql-params&#41;&#41;
&#40;System/currentTimeMillis&#41;&#41;
&#40;fn &#91;sym state result&#93;
&#40;log/debug sym &#40;str &#40;- &#40;System/currentTimeMillis&#41; state&#41; &quot;ms&quot;
&#40;if &#40;sequential? result&#41; &#40;str &quot;, &quot; &#40;count result&#41; &#40;if &#40;&gt; &#40;count result&#41; 1&#41; &quot; items&quot; &quot; item&quot;&#41;&#41; &quot;&quot;&#41;&#41;&#41;&#41;&#41;&#41;

&#40;defstate &#94;:dynamic &#42;db&#42;
:start &#40;if-let &#91;jdbc-url &#40;env :database-url&#41;&#93;
&#40;with-logging &#40;conman/connect! {:jdbc-url jdbc-url}&#41;&#41;
&#40;do
&#40;log/warn &quot;database connection URL was not found, please set :database-url in your config, e.g: dev-config.edn&quot;&#41;
&#42;db&#42;&#41;&#41;
:stop &#40;conman/disconnect! &#42;db&#42;&#41;&#41;
</code></pre>
</div>
</div>
</div>
Expand Down
27 changes: 25 additions & 2 deletions docs/logging.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ <h1>
<h1>Logging</h1>

<h2>Contents</h2>
<ol class="contents"><li><a href="#logging">Logging</a></li><li><a href="#logging_configuration">Logging Configuration</a></li></ol>
<ol class="contents"><li><a href="#logging">Logging</a></li><li><a href="#logging_configuration">Logging Configuration</a></li><li><a href="#logging_http_requests">Logging HTTP requests</a></li></ol>

<div id="content">
<h2 id="logging">Logging</h2><p>By default, logging functionality is provided by the <a href='https://github.com/clojure/tools.logging'>clojure.tools.logging</a> library. The library provides macros that delegate to a specific logging implementation. The default implementation used in Luminus is the <a href='http://logback.qos.ch/'>logback</a> library.</p><p>There are six log levels in <code>clojure.tools.logging</code>, and any Clojure data structures can be logged directly. The log levels are <code>trace</code>, <code>debug</code>, <code>info</code>, <code>warn</code>, <code>error</code>, and <code>fatal</code>.</p><pre><code class="clojure">&#40;ns example
Expand Down Expand Up @@ -135,7 +135,30 @@ <h2 id="logging">Logging</h2><p>By default, logging functionality is provided by
&lt;/root&gt;
&lt;/configuration&gt;
</code></pre><p>Then we can start the app with the following flag to have it use this logging configuration:</p><pre><code>java -Dlogback.configurationFile=prod-log-config.xml -jar myapp.jar
</code></pre><p>Please refer to the <a href='http://logback.qos.ch/manual/configuration.html'>official documentation</a> for further information on configuring <code>logback</code>.</p>
</code></pre><p>Please refer to the <a href='http://logback.qos.ch/manual/configuration.html'>official documentation</a> for further information on configuring <code>logback</code>.</p><h3 id="logging&#95;http&#95;requests">Logging HTTP requests</h3><p>You can use <a href='https://github.com/nberger/ring-logger#usage'>ring-logger</a> in order to log HTTP requests:</p><pre><code class="clojure"># project.clj

:dependencies &#91;...
&#91;ring-logger &quot;1.1.1&quot;&#93;
&#93;

</code></pre><pre><code class="clojure">&#40;ns myapp.routes.home
&#40;:require
...
&#91;ring.logger&#93;&#41;&#41;


&#40;defn home-routes &#91;&#93;
&#91; &quot;&quot;
{:middleware &#91;
ring.logger/wrap-with-logger
middleware/wrap-csrf
middleware/wrap-formats
&#93;}
&#91;&quot;/&quot; {:get home-page}&#93;
&#91;&quot;/about&quot; {:get about-page}&#93;&#93;&#41;
...


</div>
</div>
</div>
Expand Down

0 comments on commit dd186ef

Please sign in to comment.