From 7ffa575d8bee2430fd60793b8344b3f69a5e5cde Mon Sep 17 00:00:00 2001 From: Mischa Braam Date: Tue, 5 Dec 2023 15:12:52 +0100 Subject: [PATCH] Apply newyslog to prevent large logfiles --- cli/ValetPlus/Extended/Configuration.php | 49 ++++++++++++++++++++++++ cli/stubs/newsyslog.d/valet-plus.conf | 4 ++ 2 files changed, 53 insertions(+) create mode 100644 cli/stubs/newsyslog.d/valet-plus.conf diff --git a/cli/ValetPlus/Extended/Configuration.php b/cli/ValetPlus/Extended/Configuration.php index 0dcac83b..e06e4e6d 100644 --- a/cli/ValetPlus/Extended/Configuration.php +++ b/cli/ValetPlus/Extended/Configuration.php @@ -10,6 +10,10 @@ class Configuration extends ValetConfiguration { /** @var string */ protected const CUSTOM_DRIVERS_STUB_DIR = __DIR__ . '/../../stubs/drivers/custom/'; + /** @var string */ + protected const LOG_ROTATE_STUB_FILE = __DIR__ . '/../../stubs/newsyslog.d/valet-plus.conf'; + /** @var string */ + protected const LOG_ROTATE_FILE = '/etc/newsyslog.d/valet-plus.conf'; /** * @inheritdoc @@ -17,7 +21,16 @@ class Configuration extends ValetConfiguration public function createDriversDirectory(): void { parent::createDriversDirectory(); + $this->installCustomDrivers(); + } + /** + * Installs custom drivers not supported by Valet. + * + * @return void + */ + protected function installCustomDrivers(): void + { $driversDirectory = VALET_HOME_PATH . '/Drivers/'; foreach (scandir(static::CUSTOM_DRIVERS_STUB_DIR) as $driver) { if (!is_dir($driver)) { @@ -28,4 +41,40 @@ public function createDriversDirectory(): void } } } + + /** + * @inheritdoc + */ + public function createLogDirectory(): void + { + parent::createLogDirectory(); + $this->installNewsysLogRotation(); + } + + /** + * Installing configuration files for log file rotation. + * + * This log file rotation is using the default MacOS newsyslog. It places a config file in the newsyslog.d directory + * which is automatically picked up and processed. You can run the following command to see what newsyslog will + * rotate. + * + * Command: `sudo newsyslog -vn` + * + * See https://www.real-world-systems.com/docs/newsyslog.1.html + * + * @return void + */ + protected function installNewsysLogRotation(): void + { + // Place config file to rotate log files in Valet+ log path. + $contents = $this->files->get(static::LOG_ROTATE_STUB_FILE); + $contents = str_replace('VALET_HOME_PATH', VALET_HOME_PATH, $contents); + + $this->files->putAsUser( + static::LOG_ROTATE_FILE, + $contents + ); + + //@todo; Set up log rotation for BREW_PREFIX . '/var/log/' to rotate elasticsearch logs + } } diff --git a/cli/stubs/newsyslog.d/valet-plus.conf b/cli/stubs/newsyslog.d/valet-plus.conf new file mode 100644 index 00000000..75cab655 --- /dev/null +++ b/cli/stubs/newsyslog.d/valet-plus.conf @@ -0,0 +1,4 @@ +# logfilename [owner:group] mode count size when flags [/pid_file] [sig_num] +VALET_HOME_PATH/Log/mysql.log 644 10 20480 * JN +VALET_HOME_PATH/Log/nginx-error.log 644 10 20480 * JN +VALET_HOME_PATH/Log/php-fpm.log 644 10 20480 * JN