But in this case the appender is created without problems and both errors occurs later when logging an event. (I configured FileAppender with createOnDemand="true" to acheive this effect.)
On Thu, May 4, 2017 at 3:40 PM, Ralph Goers <[email protected]> wrote: > There is a difference between errors that occur when creating the Appender > vs errors that occur in logging events. If an error occurs creating the > Appender the rest of the configuration should be processed so that at least > some logging works. If the error is too bad then the configuration will > fail and the previous configuration will remain in affect. > > An error that occurs when logging is entirely different. In that case the > Appender should throw an exception. It will be caught by the > AppenderControl and only propagated to the application if the Appender is > configured to have errors percolate. Otherwise it will be caught and > ignored. > > Ralph > > > On May 4, 2017, at 6:01 AM, Mikael Ståldal <[email protected]> > wrote: > > > > AbstractOutputStreamAppender have this code: > > > > public void append(final LogEvent event) { > > try { > > tryAppend(event); > > } catch (final AppenderLoggingException ex) { > > error("Unable to write to stream " + manager.getName() + " for > > appender " + getName() + ": " + ex); > > throw ex; > > } > > } > > > > > > Is that really the proper way of doing it? When I try this: > > > > <Configuration status="WARN"> > > <Appenders> > > <File name="File" fileName="/etc/test.log" createOnDemand="true"> > > <PatternLayout pattern="%date %5level %X %logger{15}: > %message%n"/> > > </File> > > </Appenders> > > <Loggers> > > <Root level="INFO"> > > <AppenderRef ref="File"/> > > </Root> > > </Loggers> > > </Configuration> > > > > I get two errors when it fails to create the file in /etc: > > > > 2017-05-04 14:50:37,566 main ERROR Unable to write to stream > /etc/test.log > > for appender File: > > org.apache.logging.log4j.core.appender.AppenderLoggingException: Error > > writing to stream /etc/test.log > > 2017-05-04 14:50:37,570 main ERROR An exception occurred processing > > Appender File > > org.apache.logging.log4j.core.appender.AppenderLoggingException: Error > > writing to stream /etc/test.log > > at > > org.apache.logging.log4j.core.appender.OutputStreamManager. > writeToDestination(OutputStreamManager.java:258) > > at > > org.apache.logging.log4j.core.appender.FileManager.writeToDestination( > FileManager.java:177) > > at > > org.apache.logging.log4j.core.appender.OutputStreamManager.flushBuffer( > OutputStreamManager.java:288) > > at > > org.apache.logging.log4j.core.appender.OutputStreamManager. > flush(OutputStreamManager.java:297) > > at > > org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender. > directEncodeEvent(AbstractOutputStreamAppender.java:179) > > at > > org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender. > tryAppend(AbstractOutputStreamAppender.java:170) > > at > > org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender. > append(AbstractOutputStreamAppender.java:161) > > at > > org.apache.logging.log4j.core.config.AppenderControl.tryCallAppender( > AppenderControl.java:156) > > at > > org.apache.logging.log4j.core.config.AppenderControl. > callAppender0(AppenderControl.java:129) > > at > > org.apache.logging.log4j.core.config.AppenderControl. > callAppenderPreventRecursion(AppenderControl.java:120) > > at > > org.apache.logging.log4j.core.config.AppenderControl. > callAppender(AppenderControl.java:84) > > at > > org.apache.logging.log4j.core.config.LoggerConfig. > callAppenders(LoggerConfig.java:448) > > at > > org.apache.logging.log4j.core.config.LoggerConfig. > processLogEvent(LoggerConfig.java:433) > > at > > org.apache.logging.log4j.core.config.LoggerConfig.log( > LoggerConfig.java:417) > > at > > org.apache.logging.log4j.core.config.LoggerConfig.log( > LoggerConfig.java:403) > > at > > org.apache.logging.log4j.core.config.AwaitCompletionReliabilityStra > tegy.log(AwaitCompletionReliabilityStrategy.java:63) > > at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:146) > > at > > org.apache.logging.log4j.spi.AbstractLogger.logMessageSafely( > AbstractLogger.java:2091) > > at > > org.apache.logging.log4j.spi.AbstractLogger.logMessage( > AbstractLogger.java:1988) > > at > > org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled( > AbstractLogger.java:1960) > > at org.apache.logging.log4j.spi.AbstractLogger.info > > (AbstractLogger.java:1262) > > at logtest.Main.main(Main.java:29) > > Caused by: java.io.FileNotFoundException: /etc/test.log (Permission > denied) > > at java.io.FileOutputStream.open0(Native Method) > > at java.io.FileOutputStream.open(FileOutputStream.java:270) > > at java.io.FileOutputStream.<init>(FileOutputStream.java:213) > > at java.io.FileOutputStream.<init>(FileOutputStream.java:133) > > at > > org.apache.logging.log4j.core.appender.FileManager.createOutputStream( > FileManager.java:120) > > at > > org.apache.logging.log4j.core.appender.OutputStreamManager. > getOutputStream(OutputStreamManager.java:166) > > at > > org.apache.logging.log4j.core.appender.OutputStreamManager. > writeToDestination(OutputStreamManager.java:256) > > ... 21 more > > > > > > (AbstractWriterAppender seems to have a similar problem.) > > > > Wouldn't it be better with something like this: > > > > public void append(final LogEvent event) { > > try { > > tryAppend(event); > > } catch (final Exception ex) { > > error("Unable to write to stream " + manager.getName() + " for > > appender " + getName(), event, ex); > > } > > } > > > > And then change the relevant Manager code to not catch exceptions. > > > > -- > > [image: MagineTV] > > > > *Mikael Ståldal* > > Senior software developer > > > > *Magine TV* > > [email protected] > > Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com > > > > Privileged and/or Confidential Information may be contained in this > > message. If you are not the addressee indicated in this message > > (or responsible for delivery of the message to such a person), you may > not > > copy or deliver this message to anyone. In such case, > > you should destroy this message and kindly notify the sender by reply > > email. > > > -- [image: MagineTV] *Mikael Ståldal* Senior software developer *Magine TV* [email protected] Grev Turegatan 3 | 114 46 Stockholm, Sweden | www.magine.com Privileged and/or Confidential Information may be contained in this message. If you are not the addressee indicated in this message (or responsible for delivery of the message to such a person), you may not copy or deliver this message to anyone. In such case, you should destroy this message and kindly notify the sender by reply email.
