This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-07-13
Channels
- # announcements (1)
- # babashka (28)
- # beginners (49)
- # calva (34)
- # cider (8)
- # clj-kondo (7)
- # clojure (114)
- # clojure-austin (1)
- # clojure-denver (15)
- # clojure-europe (8)
- # clojure-norway (3)
- # clojurescript (83)
- # datahike (1)
- # datomic (5)
- # emacs (6)
- # events (1)
- # helix (11)
- # honeysql (2)
- # hyperfiddle (95)
- # jackdaw (1)
- # jobs-discuss (6)
- # kaocha (5)
- # lsp (15)
- # malli (3)
- # off-topic (171)
- # polylith (17)
- # re-frame (18)
- # releases (1)
- # ring (3)
- # sql (7)
@U02CV2P4J6S You didn't specify in what way, but this should now also work in bb, more or less, I think: https://www.baeldung.com/java-unix-domain-socket
Yeah the curl one is specially for http over unix
turns out for my use case right now I can use the python script that chat gpt gave me.. whatever works
import socket
import json
# Create a socket connection to the mpv IPC server
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.connect("/tmp/mpvsocket")
# Prepare the command to get playback position (timestamp)
msg = {'command': ['get_property', 'playback-time']}
sock.send(json.dumps(msg).encode('utf-8'))
sock.send(b'\n')
# Receive the response
data = sock.recv(4096)
response = json.loads(data.decode('utf-8'))
# Extract the timestamp
timestamp = response.get('data')
print(timestamp)
This should work in vanilla bb I think
clojure.tools.logging
uses some logging implementations that are all implemented in java so not available in bababshka:
SLF4J
Apache Commons Logging
Log4J 2
Log4J
java.util.logging
https://github.com/clojure/tools.logging#selecting-a-logging-implementation
In this case, I imagine bb is using JUL logging.
However there is no way to configure the logging since the JUL classes are not included with bb.
Does it make sense to add some JUL classes to allow for some basic programatic configuration of JUL ?
Clojure code in thread.
cc @borkdudefor a CLI app that can be run with bb I want to configure logging like this:
(ns compoje.jul
(:import (java.time ZoneOffset)
(java.util.logging ConsoleHandler Logger LogRecord
Formatter SimpleFormatter)))
(defn simple-formatter
"Clojure bridge for java.util.logging.SimpleFormatter.
Can register a clojure fn as a logger formatter.
* format-fn - clojure fn that receives the record to send to logging."
(^SimpleFormatter [format-fn]
(proxy [SimpleFormatter] []
(format [record]
(format-fn record)))))
(defn format-log-record
"Format jul logger record."
(^String [^LogRecord record]
(let [fmt "%5$s"
instant (.getInstant record)
date (-> instant (.atZone ZoneOffset/UTC))
level (.getLevel record)
src (.getSourceClassName record)
msg (.getMessage record)
thr (.getThrown record)
logger (.getLoggerName record)]
(format fmt date src logger level msg thr))))
(defn set-logger-format
"Configure JUL logger to use a custom log formatter.
* formatter - instance of java.util.logging.Formatter"
([]
(set-logger-format (simple-formatter format-log-record)))
([^Formatter formatter]
(let [main-logger (doto (Logger/getLogger "")
(.setUseParentHandlers false))
handler (doto (ConsoleHandler.)
(.setFormatter formatter))
handlers (.getHandlers main-logger)]
(doseq [h handlers]
(.removeHandler main-logger h))
(.addHandler main-logger handler))))
the idea is to call (set-logger-format) in main method so that logging will not output any timestamps
ok, that would work if I will switch to using timbre in my app.
but if I use a library that uses clojure.tools.logging
then I can't do anything about logging formatting
you can switch implementations based on reader conditionals or a macro:
(defmacro if-bb [then else]
(if (System/getProperty "babashka.version") then else))
if I add a lib that uses clojure.tools.logging in my bb app, I can't confugure logging for that
seems like that might not be so, since timbre has some hooks for c.t.l : https://github.com/ptaoussanis/timbre/blob/master/src/taoensso/timbre/tools/logging.clj
timbre is configured as the tools.logging implementation in bb. I'm not sure what your question is.
@U011NGC5FFY Some libraries use logging implementations rather than clojure.tools.logging or slf4j (especially java libraries) and in that case I don't think you'll be able to configure them using just timbre. Is this what you're asking about? In JVM clojure you can use https://github.com/fzakaria/slf4j-timbre to direct everything to timbre but that might not be possible in babashka, as it's probably missing some of those classes?
hi @UEQPKG7HQ, I know and yes those libraries cannot be used from babashka either since the classes are missing.