dev-resources.site
for different kinds of informations.
Clojure Is Awesome!!! [PART 2]
Published at
12/15/2024
Categories
clojure
singleton
designpatterns
cleancode
Author
borba
Author
5 person written this
borba
open
From the series 'I don't need to say anything... :)
(ns singleton)
(defprotocol LoggerService
"Protocol defining logging operations."
(log-info [this message] "Logs an informational message.")
(log-error [this message] "Logs an error message.")
(log-debug [this message] "Logs a debug message."))
(defrecord FileLogger [log-file]
LoggerService
(log-info [_ message]
(spit log-file (str (java.time.Instant/now) " [INFO]: " message "\n") :append true))
(log-error [_ message]
(spit log-file (str (java.time.Instant/now) " [ERROR]: " message "\n") :append true))
(log-debug [_ message]
(spit log-file (str (java.time.Instant/now) " [DEBUG]: " message "\n") :append true)))
(defonce logger-instance
(atom nil))
(defn get-logger
"Returns the singleton instance of the LoggerService."
[]
(if-let [instance @logger-instance]
instance
(let [new-instance (->FileLogger "application.log")]
(reset! logger-instance new-instance)
new-instance)))
(defn log-endpoint
"A Pedestal handler that logs requests and responses."
[request]
(let [logger (get-logger)]
(log-info logger (str "Received request: " (:uri request)))
{:status 200
:body "Request logged successfully!"}))
(require '[io.pedestal.http :as http])
(def service
{:env :prod
::http/routes #{["/log" :get log-endpoint]}
::http/type :jetty
::http/port 8080})
(comment
;; Start the server
(http/create-server service)
;; curl http://localhost:8080/log
)
cleancode Article's
30 articles in total
STOP Writing Dirty Code: Fix The Data Class Code Smell Now!
read article
Абстракции vs. привязка к технологии
read article
An Initiation to Domain-Driven Design
read article
7 Essential Design Patterns for JavaScript Developers: Boost Your Coding Mastery
read article
Orden en el Código .NET
read article
Movie X: A Developer’s Dive Into Flutter Project Organization
read article
3 Code Comment Mistakes You're Making Right Now
read article
From Chaos to Control
read article
Clean code
read article
Want to Learn Docker in Advance Way?
read article
3 very simple React patterns to immediately improve your codebase 🪄
read article
Refactoring 021 - Remove Dead Code
read article
Code Commenting Ethics: When Over-Documentation Hurts Development
read article
Clean Code: Managing Side Effects with Functional Programming
read article
Why Use Getters and Setters?!
read article
Clojure Is Awesome!!! [PART 4]
read article
Clojure Is Awesome!!! [PART 3]
read article
Python's Magic Methods
read article
Clojure Is Awesome!!! [PART 2]
currently reading
Why should I care about Quality? I'm a developer!
read article
Mastering Laravel Blade: @stack, @push, and @endpush
read article
How to write a good Clean code? - Tips for Developers with Examples
read article
Union and Intersection Types in TypeScript
read article
Arquitetura Viva: Moldando Sistemas para Mudanças
read article
Dependency Injection in ASP.NET Core with Extension Classes: A Comprehensive Guide
read article
Rails transactional callbacks beyond models
read article
Python Best Practices: Writing Clean and Maintainable Code
read article
Excited to Be Part of This Community! 🚀
read article
Single Responsibility Principle in Javascript
read article
Build Express APIs Faster than AI
read article
Featured ones: