From b1cf051a73a6b560fc6cece8ca26af6e407e6c83 Mon Sep 17 00:00:00 2001
From: Paweł 'Zibi' Zaremba <pzaremba@infoza.pl>
Date: Mon, 26 May 2014 10:25:15 +0200
Subject: [PATCH] Adding rotateByCopy to yii\log\FileTarget #3562

---
 framework/CHANGELOG.md       |  1 +
 framework/log/FileTarget.php | 15 ++++++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md
index 477d6b9..127720a 100644
--- a/framework/CHANGELOG.md
+++ b/framework/CHANGELOG.md
@@ -57,6 +57,7 @@ Yii Framework 2 Change Log
 - Enh #3518: `yii\helpers\Html::encode()` now replaces invalid code sequences with "?" (DaSourcerer)
 - Enh #3521: Added `yii\filters\HttpCache::sessionCacheLimiter` (qiangxue)
 - Enh #3542: Removed requirement to specify `extensions` in application config (samdark)
+- Enh #3562: Adding rotateByCopy to yii\log\FileTarget (pawzar)
 - Enh #3574: Add integrity check support for SQLite (zeeke)
 - Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue)
 - Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue)
diff --git a/framework/log/FileTarget.php b/framework/log/FileTarget.php
index 2666d0d..c1bf807 100644
--- a/framework/log/FileTarget.php
+++ b/framework/log/FileTarget.php
@@ -51,6 +51,11 @@ class FileTarget extends Target
      * but read-only for other users.
      */
     public $dirMode = 0775;
+    /**
+     * @var boolean Whether to rotate primary log by copy and truncate
+     * which is more compatible with log tailers. Defaults to false.
+     */
+    public $rotateByCopy = false;
 
     /**
      * Initializes the route.
@@ -115,7 +120,15 @@ class FileTarget extends Target
                 if ($i === $this->maxLogFiles) {
                     @unlink($rotateFile);
                 } else {
-                    @rename($rotateFile, $file . '.' . ($i + 1));
+                    if ($this->rotateByCopy) {
+                        @copy($rotateFile, $file . '.' . ($i + 1));
+                        if ($fp = @fopen($rotateFile, 'a')) {
+                            @ftruncate($fp, 0);
+                            @fclose($fp);
+                        }
+                    } else {
+                        @rename($rotateFile, $file . '.' . ($i + 1));
+                    }
                 }
             }
         }
--
libgit2 0.27.1