Search in sources :

Example 1 with RolloverFailure

use of ch.qos.logback.core.rolling.RolloverFailure in project cdap by caskdata.

the class FixedWindowRollingPolicy method rollover.

@Override
public void rollover() throws RolloverFailure {
    Location parentLocation = getParent(activeFileLocation);
    if (parentLocation == null) {
        return;
    }
    // If maxIndex <= 0, then there is no file renaming to be done.
    if (maxIndex >= 0) {
        try {
            // close outputstream of active location
            closeable.close();
            String fileName = fileNamePattern.convertInt(maxIndex);
            // maximum index otherwise we will end up deleting rolled over file
            if (processedIndex == maxIndex) {
                Location deleteLocation = parentLocation.append(fileName);
                // no need to proceed further if we are not able to delete location so throw exception
                if (deleteLocation.exists() && !deleteLocation.delete()) {
                    LOG.warn("Failed to delete location: {}", deleteLocation.toURI().toString());
                    throw new RolloverFailure(String.format("Not able to delete file: %s", deleteLocation.toURI().toString()));
                }
                processedIndex--;
            }
            for (int i = processedIndex; i >= minIndex; i--, processedIndex--) {
                String toRenameStr = fileNamePattern.convertInt(i);
                Location toRename = parentLocation.append(toRenameStr);
                // no point in trying to rename an non existent file
                if (toRename.exists()) {
                    Location newName = parentLocation.append(fileNamePattern.convertInt(i + 1));
                    // throw exception if rename fails, so that in next iteration of rollover, it will be retried
                    if (toRename.renameTo(newName) == null) {
                        LOG.warn("Failed to rename {} to {}", toRename.toURI().toString(), newName.toURI().toString());
                        throw new RolloverFailure(String.format("Failed to rename %s to %s", toRename.toURI().toString(), newName.toURI().toString()));
                    }
                } else {
                    LOG.trace("Skipping roll-over for inexistent file {}", toRename.toURI().toString());
                }
            }
            if (activeFileLocation.renameTo(parentLocation.append(fileNamePattern.convertInt(minIndex))) == null) {
                LOG.warn("Failed to rename location: {}", activeFileLocation.toURI().toString());
                throw new RolloverFailure(String.format("Not able to rename file: %s", activeFileLocation.toURI().toString()));
            }
            // reset max processed index after rename of active location has been processed successfully
            processedIndex = maxIndex;
        } catch (IOException e) {
            RolloverFailure f = new RolloverFailure(e.getMessage());
            f.addSuppressed(e);
            throw f;
        }
    }
}
Also used : RolloverFailure(ch.qos.logback.core.rolling.RolloverFailure) IOException(java.io.IOException) Location(org.apache.twill.filesystem.Location)

Example 2 with RolloverFailure

use of ch.qos.logback.core.rolling.RolloverFailure in project cdap by caskdata.

the class RollingLocationLogAppender method doAppend.

@Override
public void doAppend(ILoggingEvent eventObject) throws LogbackException {
    try {
        // logic from AppenderBase
        if (!this.started) {
            LOG.warn("Attempted to append to non started appender {}", this.getName());
            return;
        }
        // logic from AppenderBase
        if (getFilterChainDecision(eventObject) == FilterReply.DENY) {
            return;
        }
        String namespaceId = eventObject.getMDCPropertyMap().get(LocationManager.TAG_NAMESPACE_ID);
        if (namespaceId != null && !namespaceId.equals(NamespaceId.SYSTEM.getNamespace())) {
            LocationIdentifier logLocationIdentifier = locationManager.getLocationIdentifier(eventObject.getMDCPropertyMap());
            rollover(logLocationIdentifier, eventObject);
            OutputStream locationOutputStream = locationManager.getLocationOutputStream(logLocationIdentifier, filePath);
            setOutputStream(locationOutputStream);
            writeOut(eventObject);
        }
    } catch (IllegalArgumentException iae) {
        // this shouldn't happen
        LOG.error("Unrecognized context ", iae);
    } catch (IOException ioe) {
        throw new LogbackException("Exception while appending event. ", ioe);
    } catch (RolloverFailure rolloverFailure) {
        throw new LogbackException("Exception while rolling over. ", rolloverFailure);
    }
}
Also used : RolloverFailure(ch.qos.logback.core.rolling.RolloverFailure) OutputStream(java.io.OutputStream) IOException(java.io.IOException) LogbackException(ch.qos.logback.core.LogbackException)

Aggregations

RolloverFailure (ch.qos.logback.core.rolling.RolloverFailure)2 IOException (java.io.IOException)2 LogbackException (ch.qos.logback.core.LogbackException)1 OutputStream (java.io.OutputStream)1 Location (org.apache.twill.filesystem.Location)1