Hello! Here is my take on rolling file publisher for and AWESOME mulog library (https://github.com/BrunoBonacci/mulog)
It uses cognitec Transit formatter (https://github.com/cognitect/transit-clj) to store log records as Clojure data.
It supports log rotation based on maximum size or maximum age of a file.
You can start the publisher using the following code:
(mu/start-publisher!
{:type :custom
:fqn-function "ifarafontov.transit-publisher/transit-file-publisher"
:file-path "logz/log.json"
:rotate-size {:mb 10}
})
Accepted arguments:
Key | Default value | Description |
---|---|---|
:file-path | "./app.log.json" | A path to logs directory, including file name. As there is no garanty in getting file's creation time on all filesystems (i.e. ext4) in Java - I decided to use file name for storing it. A current active log name will be formed as concatenation of: number of milliseconds from the epoch of 1970-01-01T00:00:00Z., an underscore character, and the file name (last element) from :file-path argument. Example: 1604991687740_log.json When a file will be rotated - it will be given a name consisting of a file name from :file-path element, a dot character, and a current local date presented as "YYYYMMdd_hhmmss" string. Example: log.json.20201110_115350 |
:rotate-age | nil (no rotation) | A map with keyset #{:seconds, :minutes, :hours, :days, :weeks} and a positive integer values. Example: {:seconds 2 :days 5} In case of an unexpected key or a non positive integer value - will throw AssertionError |
:rotate-size | nil (no rotation) | A map with keyset #{:kb, :mb, :gb } and a positive integer values. Example: {:kb 2 :gb 5} In case of an unexpected key or a non positive integer value - will throw AssertionError |
:transit-format | :json | one of :json :json-verbose :msgpack. Check Transit documentation. |
:transit-handlers | nil | A map of custom transit handlers (writers). Check Transit documentation. Also see empty-folder-test in ifarafontov.transit-publisher-no-rotate-integration-test namespace |
:transform | identity | A user-defined function to be applied to log records before writing to file. Receives a single log entry map. Can be used for mapping and/or filtering. Return nil if you do not want to log a particular record. Example: (do not log records with :dont-log key) #(when-not (:dont-log (set (keys %))) %) |
- Throwables will be converted to maps via Throwable->Map
- Transform function will be applied before converting Throwables - so you can provide custom conversion if needed
- There is a handy function read-all-transit that can be used for reading log files in ifarafontov.transit-publisher namespace. It expects :file-name,
:transit-fromat, :transit-handlers (readers) and :transform (for example
#(select-keys % [:keys :that :are :interesting])
).
Copyright © 2020 ifarafontov-clj
Distributed under the Eclipse Public License version 1.0.