logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bernhard Mähr (JIRA) <j...@apache.org>
Subject [jira] [Comment Edited] (LOG4J2-904) RollingFile appender doesn't zip and create new files
Date Mon, 04 Jul 2016 14:21:11 GMT

    [ https://issues.apache.org/jira/browse/LOG4J2-904?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15361376#comment-15361376
] 

Bernhard Mähr edited comment on LOG4J2-904 at 7/4/16 2:20 PM:
--------------------------------------------------------------

I have now a working FileRenameAction which does a truncate even if the log-file is locked
some way and logs what it is doing and why it is failing. This implementation solved the problem
on my Windows machine even if deleting the log-File was noch possible. 

Please check and add to the source code.

{code:java}

    public static boolean execute(final File source, final File destination, final boolean
renameEmptyFiles) {
        if (renameEmptyFiles || (source.length() > 0)) {
            final File parent = destination.getParentFile();
            if ((parent != null) && !parent.exists()) {
                // LOG4J2-679: ignore mkdirs() result: in multithreaded scenarios,
                // if one thread succeeds the other thread returns false
                // even though directories have been created. Check if dir exists instead.
                parent.mkdirs();
                if (!parent.exists()) {
                    AbstractAction.LOGGER.error("Unable to create directory {}", parent.getAbsolutePath());
                    return false;
                }
            }
            try {
                try {
                    Files.move(Paths.get(source.getAbsolutePath()), Paths.get(destination.getAbsolutePath()),
StandardCopyOption.ATOMIC_MOVE,
                            StandardCopyOption.REPLACE_EXISTING);
                    AbstractAction.LOGGER.trace("Renamed file {} to {} with Files.move", source.getAbsolutePath(),
destination.getAbsolutePath());
                    return true;
                } catch (final IOException exMove) {
                    AbstractAction.LOGGER.error("Unable to move file {} to {}: {} {}", source.getAbsolutePath(),
destination.getAbsolutePath(),
                            exMove.getClass().getName(), exMove.getMessage());
                    final boolean result = source.renameTo(destination);
                    if (!result) {
                        try {
                            Files.copy(Paths.get(source.getAbsolutePath()), Paths.get(destination.getAbsolutePath()),
StandardCopyOption.REPLACE_EXISTING);
                            try {
                                Files.delete(Paths.get(source.getAbsolutePath()));
                                AbstractAction.LOGGER.trace("Renamed file {} to {} with Files.copy
and Files.delete", source.getAbsolutePath(),
                                        destination.getAbsolutePath());
                            } catch (final IOException exDelete) {
                                AbstractAction.LOGGER.error("Unable to delete file {}: {}
{}", source.getAbsolutePath(), exDelete.getClass().getName(),
                                        exDelete.getMessage());
                                try {
                                    new PrintWriter(source.getAbsolutePath()).close();
                                    AbstractAction.LOGGER.trace("Renamed file {} to {} with
Files.copy and overwrite old file", source.getAbsolutePath(),
                                            destination.getAbsolutePath());
                                } catch (final IOException exOwerwrite) {
                                    AbstractAction.LOGGER.error("Unable to overwrite file
{}: {} {}", source.getAbsolutePath(), exOwerwrite.getClass().getName(),
                                            exOwerwrite.getMessage());
                                }
                            }
                        } catch (final IOException exCopy) {
                            AbstractAction.LOGGER.error("Unable to copy file {} to {}: {}
{}", source.getAbsolutePath(), destination.getAbsolutePath(),
                                    exCopy.getClass().getName(), exCopy.getMessage());
                        }
                    } else {
                        AbstractAction.LOGGER.trace("Renamed file {} to {} with source.renameTo",
source.getAbsolutePath(), destination.getAbsolutePath());
                    }
                    return result;
                }
            } catch (final RuntimeException ex) {
                AbstractAction.LOGGER.error("Unable to rename file {} to {}: {} {}", source.getAbsolutePath(),
destination.getAbsolutePath(),
                        ex.getClass().getName(), ex.getMessage());
            }
        } else {
            try {
                source.delete();
            } catch (final Exception exDelete) {
                AbstractAction.LOGGER.error("Unable to delete empty file {}: {} {}", source.getAbsolutePath(),
exDelete.getClass().getName(),
                        exDelete.getMessage());
            }
        }

        return false;
    }

{code} 


was (Author: bmaehr):
I have now a working FileRenameAction which does a truncate even if the log-file is locked
some way and logs what it is doing and why it is failing. This implementation solved the problem
on my Windows machine even if deleting the log-File was noch possible. 

Please check and add to the source code.

{code:java}

    public static boolean execute(final File source, final File destination, final boolean
renameEmptyFiles) {
        if (renameEmptyFiles || (source.length() > 0)) {
            final File parent = destination.getParentFile();
            if ((parent != null) && !parent.exists()) {
                // LOG4J2-679: ignore mkdirs() result: in multithreaded scenarios,
                // if one thread succeeds the other thread returns false
                // even though directories have been created. Check if dir exists instead.
                parent.mkdirs();
                if (!parent.exists()) {
                    AbstractAction.LOGGER.error("Unable to create directory {}", parent.getAbsolutePath());
                    return false;
                }
            }
            try {
                try {
                    Files.move(Paths.get(source.getAbsolutePath()), Paths.get(destination.getAbsolutePath()),
StandardCopyOption.ATOMIC_MOVE,
                            StandardCopyOption.REPLACE_EXISTING);
                    AbstractAction.LOGGER.trace("Renamed file {} to {} with Files.move", source.getAbsolutePath(),
destination.getAbsolutePath());
                    return true;
                } catch (final IOException exMove) {
                    AbstractAction.LOGGER.error("Unable to move file {} to {}: {} {}", source.getAbsolutePath(),
destination.getAbsolutePath(),
                            exMove.getClass().getName(), exMove.getMessage());
                    final boolean result = source.renameTo(destination);
                    if (!result) {
                        try {
                            Files.copy(Paths.get(source.getAbsolutePath()), Paths.get(destination.getAbsolutePath()),
StandardCopyOption.REPLACE_EXISTING);
                            try {
                                Files.delete(Paths.get(source.getAbsolutePath()));
                                AbstractAction.LOGGER.trace("Renamed file {} to {} with Files.copy
and Files.delete", source.getAbsolutePath(),
                                        destination.getAbsolutePath());
                            } catch (final IOException exDelete) {
                                AbstractAction.LOGGER.error("Unable to delete file {}: {}
{}", source.getAbsolutePath(), exDelete.getClass().getName(),
                                        exDelete.getMessage());
                                try {
                                    new PrintWriter(source.getAbsolutePath()).close();
                                    AbstractAction.LOGGER.trace("Renamed file {} to {} with
Files.copy and overwrite old file", source.getAbsolutePath(),
                                            destination.getAbsolutePath());
                                } catch (final IOException exOwerwrite) {
                                    AbstractAction.LOGGER.error("Unable to overwrite file
{}: {} {}", source.getAbsolutePath(), exDelete.getClass().getName(),
                                            exDelete.getMessage());
                                }
                            }
                        } catch (final IOException exCopy) {
                            AbstractAction.LOGGER.error("Unable to copy file {} to {}: {}
{}", source.getAbsolutePath(), destination.getAbsolutePath(),
                                    exCopy.getClass().getName(), exCopy.getMessage());
                        }
                    } else {
                        AbstractAction.LOGGER.trace("Renamed file {} to {} with source.renameTo",
source.getAbsolutePath(), destination.getAbsolutePath());
                    }
                    return result;
                }
            } catch (final RuntimeException ex) {
                AbstractAction.LOGGER.error("Unable to rename file {} to {}: {} {}", source.getAbsolutePath(),
destination.getAbsolutePath(),
                        ex.getClass().getName(), ex.getMessage());
            }
        } else {
            try {
                source.delete();
            } catch (final Exception exDelete) {
                AbstractAction.LOGGER.error("Unable to delete empty file {}: {} {}", source.getAbsolutePath(),
exDelete.getClass().getName(),
                        exDelete.getMessage());
            }
        }

        return false;
    }

{code} 

> RollingFile appender doesn't zip and create new files
> -----------------------------------------------------
>
>                 Key: LOG4J2-904
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-904
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Appenders
>    Affects Versions: 2.1
>         Environment: Windows 7
>            Reporter: Stephen A. Weinfield
>
> Using TimeBasedTriggeringPolicy, with a filePattern of {{f:/Log4J/$$\{date:yyyy-MM}/IIB-%d\{MM-dd-yyyy}-%i.log.zip}}
and a fileName of f:/Log4J/IIB.log:
> on date change, directory is created but filename is not zipped. Also old file is not
cleared out, but just appended to.
> Log4j2.xml:
> {code}
> <Configuration status = "warn" name = "Log4j2_Configuration" monitorInterval = "60"
packages = "">
>     <Appenders>
>         <RollingFile name = "RollingFile" fileName = "f:/Log4J/IIB.log" filePattern
= "f:/Log4J/$${date:yyyy-MM}/IIB-%d{MM-dd-yyyy}-%i.log.zip" >
>             <PatternLayout>
>                 <Pattern>%d %p %c{1} %m%n%n</Pattern>
>             </PatternLayout>
>             <Policies>
>                 <TimeBasedTriggeringPolicy interval = "1" modulate = "true"/>
>             </Policies>
>             <DefaultRolloverStrategy fileIndex = "min" max = "7" compressionLevel
= "5"/>
>         </RollingFile>
>     </Appenders>
>     <Loggers>
>         <Root level = "trace">
>             <AppenderRef ref = "RollingFile" level = "warn"/>
>         </Root>
>     </Loggers>
> </Configuration>
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Mime
View raw message