Skip to content

Instantly share code, notes, and snippets.

@vvvvalvalval
Last active September 1, 2018 22:29
Show Gist options
  • Save vvvvalvalval/43301209d95df789ffe4 to your computer and use it in GitHub Desktop.
Save vvvvalvalval/43301209d95df789ffe4 to your computer and use it in GitHub Desktop.
Clojure Logging: routing Timbre logs to Papertrail via log4j
log4j.rootLogger=INFO, syslog
log4j.appender.syslog=org.apache.log4j.net.SyslogAppender
log4j.appender.syslog.Facility=LOCAL7
log4j.appender.syslog.FacilityPrinting=false
log4j.appender.syslog.Header=true
log4j.appender.syslog.SyslogHost=<PAPERTRAIL_HOST>.papertrailapp.com:<PAPERTRAIL_PORT>
log4j.appender.syslog.layout=org.apache.log4j.PatternLayout
log4j.appender.syslog.layout.ConversionPattern==%p: (%F:%L) %x %m %n
(ns logging "In this namespace, we define a Timbre appender which routes logs to log4j 1.2 via tools.logging.
Timbre --(via Timbre appender)--> tools.logging --(via factory)--> Log4j --(via Log4j Syslog appender)--> Papertrail."
(:require [taoensso.timbre :as timbre]
[environ.core :refer [env]]
[clojure.tools.logging :as log]))
(def log-fn "Timbre appender :fn"
(let [log4j-factory (clojure.tools.logging.impl/log4j-factory)
levels #{:trace, :debug, :info, :warn, :error, :fatal}]
(fn [{:keys [hostname_ timestamp_ ?err_ ?ns-str level output-fn] :as data}]
(log/log log4j-factory
(or ?ns-str "?ns")
(get levels level :info)
(or (force ?err_) nil)
(output-fn data)))
))
(def log4j-appender "Timber appender which outputs to log4j."
{:enabled? true
:async? false
:min-level :info
:output-fn (fn [{:keys [msg_]}] (str (force msg_)))
:fn (if (env :dev) #(log-fn %) log-fn)})
(defn init! "Initializes the Timbre config." []
(timbre/merge-config!
{:level (if (env :dev) :trace :info)
:appenders {:log4j (assoc log4j-appender
:enabled? (env :log-to-papertrail))}}
))
(defproject timbre-papertrail "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.7.0"]
[log4j/log4j "1.2.17" :exclusions [javax.mail/mail
javax.jms/jms
com.sun.jmdk/jmxtools
com.sun.jmx/jmxri]]
[org.clojure/tools.logging "0.3.1"]
[com.taoensso/timbre "4.1.4"]
[environ "1.0.0"]])

Sending Timbre logs to Papertrail

Papertrail has no direct support for Clojure/Timbre, but it's easy enough to set it up using traditional Java logging tools.

This Gist demonstrates one way to set up your logging in a Clojure application to send Timbre logs to Papertrail.

The general idea is:

Timbre --(via Timbre appender)--> tools.logging --(via factory)--> Log4j --(via Log4j Syslog appender)--> Papertrail

To use this, don't forget to replace <PAPERTRAIL_HOST> and <PAPERTRAIL_PORT> with the appropriate values in your log4j.properties.

It's easy to use something else than log4j (e.g logback) as long as it is support by both Papertrail and tools.logging.

To be more realistic, this code assumes there's a LOG_TO_PAPERTRAIL environment variable / property which determines whether to use log4j or not. Of course, you can remove this part if you want.

Useful links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment