philipbohun.com

Glojure by Example: Logging

The Go log package is not in the default map. A small function around fmt.Sprintf and a timestamp covers the same ground: a level, a clock, and a message.

(defn log [at level message]
  (println (fmt.Sprintf "%s %s %s"
                        (.Format (.UTC at) "2006-01-02T15:04:05Z")
                        level
                        message)))

(let [[at err] (time.Parse time.RFC3339 "2024-03-05T15:04:05Z")]
  (println (nil? err))
  (log at "INFO" "server starting")
  (log at "ERROR" "bind failed"))

The timestamp is passed in so the line does not depend on the clock.

$ glj logging.glj
true
2024-03-05T15:04:05Z INFO server starting
2024-03-05T15:04:05Z ERROR bind failed

Next example: HTTP Client.