Fork me on GitHub
#babashka
<
2023-07-13
>
Benjamin07:07:19

Hi what is currently the way to talk to to unix socket?

2
borkdude07:07:39

@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

lispyclouds07:07:57

Yeah the curl one is specially for http over unix

Benjamin07:07:21

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)

lispyclouds07:07:43

This should work in vanilla bb I think

Eugen11:07:17

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 @borkdude

2
Eugen11:07:18

for 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))))

Eugen11:07:04

the idea is to call (set-logger-format) in main method so that logging will not output any timestamps

Eugen11:07:28

however none of the JUL classes are available

borkdude11:07:35

bb provides timbre as the logging implementation

Eugen11:07:47

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

Eugen11:07:20

since that lib is going to use JUL backend and timbre will not do anything about that

borkdude11:07:55

you can switch implementations based on reader conditionals or a macro:

(defmacro if-bb [then else]
  (if (System/getProperty "babashka.version") then else))

borkdude11:07:06

so in your lib you can use JUL for JVM and timbre for bb

Eugen11:07:38

not talking about my lib. I am talking about third party libs, outside of my control

Eugen11:07:40

if I add a lib that uses clojure.tools.logging in my bb app, I can't confugure logging for that

Eugen11:07:48

since in bb, it will default to JUL

Eugen11:07:17

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

borkdude11:07:52

timbre is configured as the tools.logging implementation in bb. I'm not sure what your question is.

Eugen12:07:30

I did not know that information.

Eugen12:07:33

no issue here

pavlosmelissinos06:07:27

@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?

Eugen08:07:41

hi @UEQPKG7HQ, I know and yes those libraries cannot be used from babashka either since the classes are missing.

👍 2