Skip to content

Commit

Permalink
fix: swoft-cloud/swoft#1176 should clear count logs on disable log. f…
Browse files Browse the repository at this point in the history
…ormat log codes
  • Loading branch information
inhere committed Jan 17, 2020
1 parent 7133fad commit 0a51739
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 123 deletions.
2 changes: 0 additions & 2 deletions src/log/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
"swoft/stdlib": "~2.0.0"
},
"autoload": {
"classmap": [
],
"psr-4": {
"Swoft\\Log\\": "src/"
}
Expand Down
20 changes: 10 additions & 10 deletions src/log/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="test/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
bootstrap="test/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="test">
<directory suffix="Test.php">./test/unit</directory>
Expand All @@ -19,4 +19,4 @@
<directory suffix=".php">./src/*</directory>
</whitelist>
</filter>
</phpunit>
</phpunit>
2 changes: 1 addition & 1 deletion src/log/src/AutoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Swoft\Log;

use function dirname;
use Swoft\Helper\ComposerJSON;
use Swoft\SwoftComponent;
use function dirname;

/**
* Class AutoLoader
Expand Down
9 changes: 4 additions & 5 deletions src/log/src/CLogger.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php declare(strict_types=1);


namespace Swoft\Log;

use Monolog\Handler\HandlerInterface;
use function count;
use function debug_backtrace;
use Monolog\Handler\HandlerInterface;
use function sprintf;

/**
Expand Down Expand Up @@ -39,12 +38,12 @@ class CLogger extends \Monolog\Logger
*
* @var array
*/
protected static $levels = array(
protected static $levels = [
self::INFO => 'INFO',
self::DEBUG => 'DEBUG',
self::WARNING => 'WARNING',
self::ERROR => 'ERROR',
);
];

/**
* Logger constructor.
Expand All @@ -63,7 +62,7 @@ public function __construct()
*
* @return bool
*/
public function addRecord($level, $message, array $context = array()): bool
public function addRecord($level, $message, array $context = []): bool
{
if (!$this->enable) {
return true;
Expand Down
9 changes: 4 additions & 5 deletions src/log/src/Handler/CEchoHandler.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<?php declare(strict_types=1);


namespace Swoft\Log\Handler;


use DateTime;
use Monolog\Formatter\FormatterInterface;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use function sprintf;
use Swoft\Log\Logger as SwoftLogger;
use Toolkit\Cli\Color;
use Toolkit\Cli\ColorTag;
use function in_array;
use function sprintf;

/**
* Class CEchoHandler
Expand Down Expand Up @@ -120,12 +119,12 @@ public function setOutput(bool $output): void
*
* @return bool
*/
public function isHandling(array $record)
public function isHandling(array $record): bool
{
if (empty($this->levelValues)) {
return true;
}

return in_array($record['level'], $this->levelValues, true);
}
}
}
22 changes: 12 additions & 10 deletions src/log/src/Handler/CFileHandler.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php declare(strict_types=1);


namespace Swoft\Log\Handler;

use InvalidArgumentException;
use Swoft\Bean\BeanFactory;
use Swoft\Log\Helper\Log;
use Swoft\Log\Logger;
use function array_column;
use function array_map;
use function file_put_contents;
use function implode;
use const FILE_APPEND;

/**
* Class CFileHandler
Expand Down Expand Up @@ -42,7 +46,7 @@ protected function write(array $record): void
$records = [$record];

// Not init
if ($this->logFile[0] === '@') {
if (strpos($this->logFile, '@') === 0) {
$this->init();

$this->bootingRecords[] = $record;
Expand All @@ -57,17 +61,14 @@ protected function write(array $record): void
} else {
$records = array_column($records, 'formatted');
}
$messageText = implode("\n", $records) . "\n";

$message = implode("\n", $records) . "\n";
$logFile = $this->formatFile($this->logFile);

// Not all console log in coroutine
$res = file_put_contents($logFile, $messageText, FILE_APPEND);

if ($res === false) {
throw new InvalidArgumentException(
sprintf('Unable to append to log file: %s', $logFile)
);
$count = file_put_contents($logFile, $message, FILE_APPEND);
if ($count === false) {
throw new InvalidArgumentException(sprintf('Unable to append to log file: %s', $logFile));
}
}

Expand All @@ -76,7 +77,8 @@ protected function write(array $record): void
*/
public function setLevels(string $levels): void
{
$levelNames = explode(',', $levels);
$levelNames = explode(',', $levels);

$this->levelValues = Logger::getLevelByNames($levelNames);

$this->levels = $levels;
Expand Down
19 changes: 6 additions & 13 deletions src/log/src/Handler/FileHandler.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?php declare(strict_types=1);


namespace Swoft\Log\Handler;


use DateTime;
use InvalidArgumentException;
use Monolog\Handler\AbstractProcessingHandler;
use Swoft\Co;
use Swoft\Exception\SwoftException;
use Swoft\Log\Helper\Log;
use Swoft\Log\Logger as SwoftLogger;
use Swoft\Log\Logger;
use Swoft\Log\Logger as SwoftLogger;
use Swoft\Stdlib\Helper\JsonHelper;
use UnexpectedValueException;
use function alias;
Expand Down Expand Up @@ -73,7 +71,6 @@ public function init(): void
* @param array $records
*
* @return void
* @throws SwoftException
*/
public function handleBatch(array $records): void
{
Expand All @@ -89,8 +86,6 @@ public function handleBatch(array $records): void
* Write file
*
* @param array $records
*
* @throws SwoftException
*/
protected function write(array $records): void
{
Expand All @@ -109,9 +104,7 @@ protected function write(array $records): void
$res = Co::writeFile($logFile, $messageText, FILE_APPEND);

if ($res === false) {
throw new InvalidArgumentException(
sprintf('Unable to append to log file: %s', $logFile)
);
throw new InvalidArgumentException(sprintf('Unable to append to log file: %s', $logFile));
}
}

Expand Down Expand Up @@ -149,13 +142,14 @@ private function recordFilter(array $records): array
public function formatJson(array $record): string
{
unset($record['formatted'], $record['extra']);
if ($record['level'] == Logger::NOTICE) {
if ($record['level'] === Logger::NOTICE) {
unset($record['context']);
}

if ($record['datetime'] instanceof DateTime) {
$record['datetime'] = $record['datetime']->format('Y-m-d H:i:s');
}

return JsonHelper::encode($record, JSON_UNESCAPED_UNICODE);
}

Expand All @@ -169,9 +163,8 @@ private function createDir(): void
if ($logDir !== null && !is_dir($logDir)) {
$status = mkdir($logDir, 0777, true);
if ($status === false) {
throw new UnexpectedValueException(
sprintf('There is no existing directory at "%s" and its not buildable: ', $logDir)
);
$errMsg = sprintf('There is no existing directory at "%s" and its not buildable', $logDir);
throw new UnexpectedValueException($errMsg);
}
}
}
Expand Down
44 changes: 34 additions & 10 deletions src/log/src/Helper/CLog.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php declare(strict_types=1);


namespace Swoft\Log\Helper;

use Monolog\Formatter\LineFormatter;
use Swoft\Log\CLogger;
use Swoft\Log\Handler\CEchoHandler;
use Swoft\Log\Handler\CFileHandler;
use function is_array;
use function sprintf;

/**
* Class CLog
*
* @since 2.0
*/
class CLog
final class CLog
{
/**
* @var CLogger
Expand Down Expand Up @@ -66,12 +66,18 @@ public static function init(array $config): void
*/
public static function debug(string $message, ...$params): void
{
$context = [];

if ($params) {
$message = sprintf($message, ...$params);
if (is_array($params[0])) {
$context = $params[0];
} else {
$message = sprintf($message, ...$params);
}
}

if (SWOFT_DEBUG) {
self::$cLogger->debug($message, []);
self::$cLogger->debug($message, $context);
}
}

Expand All @@ -83,11 +89,17 @@ public static function debug(string $message, ...$params): void
*/
public static function info(string $message, ...$params): void
{
$context = [];

if ($params) {
$message = sprintf($message, ...$params);
if (is_array($params[0])) {
$context = $params[0];
} else {
$message = sprintf($message, ...$params);
}
}

self::$cLogger->info($message, []);
self::$cLogger->info($message, $context);
}

/**
Expand All @@ -98,11 +110,17 @@ public static function info(string $message, ...$params): void
*/
public static function warning(string $message, ...$params): void
{
$context = [];

if ($params) {
$message = sprintf($message, ...$params);
if (is_array($params[0])) {
$context = $params[0];
} else {
$message = sprintf($message, ...$params);
}
}

self::$cLogger->warning($message, []);
self::$cLogger->warning($message, $context);
}

/**
Expand All @@ -113,10 +131,16 @@ public static function warning(string $message, ...$params): void
*/
public static function error(string $message, ...$params): void
{
$context = [];

if ($params) {
$message = sprintf($message, ...$params);
if (is_array($params[0])) {
$context = $params[0];
} else {
$message = sprintf($message, ...$params);
}
}

self::$cLogger->error($message, []);
self::$cLogger->error($message, $context);
}
}
Loading

0 comments on commit 0a51739

Please sign in to comment.