Ability to add log arguments per Log.Entry basis without using formatted message
See original GitHub issueWe use LogStage for structured logging. For that we use JSON console sink, its output is parsed by Filebeats
and then redirected to Graylog
. All works fine except one thing: we want to be able to add additional diagnostic key-value arguments on a per Log.Entry
basis, using LogIO
facility, but in a such way that arguments don’t have place in rendered log message.
For example, if our application will try to export some domain entity to external system and failed to do that, we may want to log entity’s ID in message and attach formatted entity attributes (which may be quite complex in structure) in a form of JSON for diagnostic and debug purposes. Right now we must explicitly include formatted entity attributes in log message:
def importEntity(entity: Entity): IO[Unit] = /* ... */
importEntity(someEntity).handleErrorWith {
case NonFatal(error) => LogIO[IO].log(Level.Error)(
s"Failed to import entity ${someEntity.id -> "ID"}: $error. ${entity.asJson.pretty(Printer.spaces2) -> "entityAsJSON"}").void
}
It works, but such log message will be rather ugly and log entry will duplicate content of entityAsJSON
twice: in the log message and in rendered arguments. It also means additional overhead for network traffic and processing of incoming log entries by Graylog
.
Do you think it’s meaningful use case?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
Sorry for late answer, thank you for suggestions! I decided to follow second advice from @kaishh and wrote simple wrapper around
LogIO
:It is typeclass for monad with logging capability (MTL style), which treats log messages not as simple strings but as ADTs - I prefer this approach in my current project. And it has simpler interface (1 method versus 4 in
LogIO
), so it’s easy to write fake implementation for testing purposes - you need to implement single method.Now I can use that typeclass this way:
It seems all works fine, thank you! 😃
Please note that this API is already available in latest snapshot. Also I would recommend you to join our gitter channel to follow our announcements.