-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Does it support a rolling file appender? #22
Comments
Not yet, but certainly open to adding that capability to the file output writer. |
I've experimented with the output writer and I got it working using the FileAppender. @greglook : does dialog offer any helpers to implement a lifecycle for this or do we need to roll our own ? (ns dialog.examples.logback
(:require [dialog.logger :as log]
[dialog.format.simple :as simple])
(:import (java.nio.charset StandardCharsets)
(ch.qos.logback.core ContextBase FileAppender)
(ch.qos.logback.core.encoder Encoder)))
(defn encoder
"In Logback, an encoder converts a log event to byte array."
[output]
(let [formatter (simple/formatter output)]
(reify Encoder
;; no header
(headerBytes [_this] nil)
(encode [_this event]
(let [event-str (formatter event)
event-str (str event-str "\n")]
(.getBytes event-str StandardCharsets/UTF_8)))
(footerBytes [_this] nil))))
(defn file-appender
"Create a ^FileAppender from dialog output map."
[opts]
(let [{:keys [appender-name file-name context encoder
immediate-flush]
:or {appender-name "file-appender"
file-name "dialog-file-appender.log"
immediate-flush true}} opts
appender (doto (FileAppender.)
(.setEncoder encoder)
(.setName appender-name)
(.setFile file-name)
(.setContext context)
(.setImmediateFlush immediate-flush)
(.start))]
appender))
(defn file-appender-logger
"dialog function to write log events using ^FileAppender."
[output]
(let [context (ContextBase.)
encoder (encoder output)
output (assoc output
:context context
:encoder encoder)
appender (file-appender output)]
(fn write-event
[event _message]
(.doAppend appender event))))
(comment
(log/initialize!)
(log/info "Hello")
) dialog.edn configuration file {:level :debug
:levels {"vault" :info}
:outputs {:logback {:type dialog.examples.logback/file-appender-logger
:format :simple
:timestamp :short
:file-name "target/dialog-logback.log"}}} deps.edn {:paths ["src" "resources"]
:deps {;; run clj -X:deps prep
amperity/dialog {:local/root "../../"}
ch.qos.logback/logback-core {:mvn/version "1.2.6"}}
} |
Ah, an interesting question - given the original goal of simplicity and basic stdout/file outputs, this wasn't a concern. I think there's room to move to a writer protocol which could provide this kind of advanced capability. 🤔 By "logger reloading" I'm assuming you mean a full reinitialization, not just changing the current logger levels. It seems like this |
Thanks. Probably this will work okish by adding a call to One way to do it now would be to register all the initialized appenders in an atom and make sure to stop them before / after initialize! . If you have any ideas to make this work while keeping it simple that would be great. I would imagine that the output map could define a set of special keys like Then I'm going to let the issue open for a bit more time so I can test RollingFileAppender, but IMO it's good to know we can reuse logback appenders. |
I was thinking of something along the lines of: (defprotocol Writer
(write-event
[writer event message]
"Write an event with a formatted message to the output.")
(close
[writer]
"Shut down the output writer.")) Then to support the current use-cases in the library: (extend-protocol Writer
clojure.lang.Fn
(write-event
[f event message]
(f event message))
(close
[f]
nil)) Then of course there would need to be some logic in the It occurs to me that one way you could handle this with the current logic is to provide a custom |
Thanks. I'll focus on delivering sentry first and do the migration and then maybe give this a try.
This might work if you build your app with this in mind. |
For long running apps that write to disk it might be good to have rolling file appenders.
There is a good overview of these in https://www.baeldung.com/java-logging-rolling-file-appenders .
I'm also wondering if it's possible to use existing slf4j appender implementations in dialog ?!
This might be hard if they are bundled with slf4j implementation - like logback has it's appenders.
But there are appenders out there that might not be.
The text was updated successfully, but these errors were encountered: