p_ysk/app/command/LogAddZip.php

109 lines
3.5 KiB
PHP

<?php
namespace app\command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use ZipArchive;
class LogAddZip extends Command
{
protected static $defaultName = 'LogAddZip';
protected static $defaultDescription = 'LogAddZip';
/**
* @return void
*/
protected function configure()
{
$this->addArgument('name', InputArgument::OPTIONAL, 'Name description');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$dir = 'runtime/worker/';
$yesterday = date("Ymd", strtotime("-1 day"));
// worker 打包
$file = $dir . '/' . $yesterday . '.log';
$xt_file = $dir . '/xt_' . $yesterday . '.log';
$file_zip = $dir . '/' . $yesterday . '.zip';
$is_close = false;
$xt_is_close = false;
if(!file_exists($file_zip)) {
$zip = new ZipArchive();
if($zip->open($file_zip, ZipArchive::CREATE) === TRUE) {
if(file_exists($file)) {
if($zip->addFile($file, $yesterday . '.log')) {
$is_close = true;
print_r($yesterday . '.log' . "\r\n");
}
}
if(file_exists($xt_file)) {
if($zip->addFile($xt_file, 'xt_' . $yesterday . '.log')) {
$xt_is_close = true;
print_r('xt_' . $yesterday . '.log' . "\r\n");
}
}
if ($is_close || $xt_is_close) {
if($zip->close()) {
if($is_close) {
unlink($file);
}
if($xt_is_close) {
unlink($xt_file);
}
}
}
}
}
$dir_l = 'runtime/log/';
// 如果今天是1号
if(date('d') == '01') {
// 找到上个月以及最后一天
// 上个月
$lastmonth = date('Ym', strtotime('-1 month'));
// 最后一天
$day = date('t', strtotime('last month'));
$dir_log = $dir_l . $lastmonth . '/' . $day . '.log';
}else {
$day = date("d", strtotime("-1 day"));
$lastmonth = date('Ym');
$dir_log = $dir_l . $lastmonth . '/' . $day . '.log';
}
// log 打包
$file_log = $dir_log;
$file_log_zip = $dir_l . '/' . $lastmonth . '/' . $day . '.zip';
$is_log_close = false;
if(!file_exists($file_log_zip)) {
$zip = new ZipArchive();
if($zip->open($file_log_zip, ZipArchive::CREATE) === TRUE) {
if(file_exists($file_log)) {
if($zip->addFile($file_log, $day . '.log')) {
$is_log_close = true;
print_r($day . '.log' . "\r\n");
}
}
if ($is_log_close) {
if($zip->close()) {
unlink($file_log);
}
}
}
}
return self::SUCCESS;
}
}