Clojure:
Writing to standard error

How to:

In Clojure, you can write to stderr using the *err* stream. Here’s a basic example:

(.write *err* "This is an error message.\n")

Note that after writing a message, you should flush the stream to ensure the message is immediately output:

(flush)

Sample output to stderr:

This is an error message.

If you’re handling exceptions, you might want to print stack traces to stderr. Use printStackTrace for this:

(try
  ;; Code that might throw an exception
  (/ 1 0)
  (catch Exception e
    (.printStackTrace e *err*)))

For more structured error logging, third-party libraries like timbre can be configured to log to stderr. Here’s a basic setup and usage:

First, add timbre to your dependencies. Then configure it to use stderr:

(require '[taoensso.timbre :as timbre])

(timbre/set-config! [:appenders :standard-out :enabled?] false) ;; Disable stdout logging
(timbre/set-config! [:appenders :spit :enabled?] false) ;; Disable file logging
(timbre/set-config! [:appenders :stderr :min-level] :error) ;; Enable stderr for errors

(timbre/error "An error occurred while processing your request.")

This will direct error-level messages to stderr, making them distinct from standard application output.