7

I'm using logback behind slf4j and would like to know how to obtain an OutputStream reference from a org.slf4j.Logger instance. The use case is Shrinkwrap's Archive.writeTo(OutputStream, ...) method.

I'm aware that it's possible to workaround the need for this reference by creating a ByteArrayOutputStream, pass it and write its content to the logger. And I'm aware of that stdout and stderr can be redirected to a logger in general. That being said, I'm looking for a direct answer to the question. If there's none, I'll suggest adding a mechanism to slf4j.

I'm using slf4j API 1.7.5 and logback 1.2.2.

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
  • I don't think it makes sense to add an API that accepts large amounts of binary data without log levels and clear message demarcations. As an application that wants to log messages, it is up to you to produce meaningful, individual log messages (each with a level, a "normal-sized" message, maybe some mapped context attributes). An OutputStream is just a dumb sink for binary data. – Thilo Oct 13 '17 at 04:58
  • Providing an OutputStream reference as argument to `Logger.[level]` has been suggested at https://jira.qos.ch/browse/LBCLASSIC-118 – Kalle Richter Oct 13 '17 at 05:03
  • That would need to work as you suggested, though: You'd get an OutputStream from the logger. If you pass an OutputStream to the logger, all it can do with that is write into it. There is no way to read from an OutputStream. – Thilo Oct 13 '17 at 05:23

1 Answers1

0

Assuming this is still line-oriented textual data (*) you could provide an OutputStream implementation that forwards whatever you write into it to a Logger instance. You'd need some way to figure out where individual messages start and end. You could even look at the contents and assign log levels and such.

(*) if it is not, it seem to not make a lot of sense to send it to a logging framework.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • This is very similar to the workaround you did not like (or this answer on the linked thread https://stackoverflow.com/a/11187462/14955), though. – Thilo Oct 13 '17 at 04:54
  • 1
    Now I get it... since `OutputStream.write` only has one `int` argument and there's no other method one needs a subclass handling the transformation into line-oriented data. – Kalle Richter Oct 13 '17 at 04:57
  • @ClaudeBrisson There is no problem and there's no indication that there is one in the answer (which I marked as correct) or the conversation. Please clarify. – Kalle Richter Apr 03 '19 at 18:42