Skip to content

Commit

Permalink
logger add enable
Browse files Browse the repository at this point in the history
  • Loading branch information
stelin committed Mar 18, 2018
1 parent 44f209f commit 400d1ff
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
11 changes: 11 additions & 0 deletions src/Exception/PoolException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Swoft\Exception;

/**
* PoolException
*/
class PoolException extends Exception
{

}
35 changes: 24 additions & 11 deletions src/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ class Logger extends \Monolog\Logger
*/
protected $messages = [];


/**
* @var array
*/
protected $processors = [];

/**
* @var bool
*/
protected $enable = false;


/**
* @var array 日志级别对应名称
Expand Down Expand Up @@ -97,6 +104,10 @@ public function __construct()
*/
public function addRecord($level, $message, array $context = array())
{
if (!$this->enable) {
return true;
}

$levelName = static::getLevelName($level);

if (!static::$timezone) {
Expand Down Expand Up @@ -166,7 +177,7 @@ public function formateRecord($message, $context, $level, $levelName, $ts, $extr
*/
public function pushLog($key, $val)
{
if (!(is_string($key) || is_numeric($key))) {
if (!$this->enable || !(is_string($key) || is_numeric($key))) {
return;
}

Expand All @@ -190,7 +201,7 @@ public function pushLog($key, $val)
*/
public function profileStart($name)
{
if (is_string($name) == false || empty($name)) {
if (!$this->enable || is_string($name) == false || empty($name)) {
return;
}
$cid = Coroutine::tid();
Expand All @@ -204,19 +215,19 @@ public function profileStart($name)
*/
public function profileEnd($name)
{
if (is_string($name) == false || empty($name)) {
if (!$this->enable || is_string($name) == false || empty($name)) {
return;
}

$cid = Coroutine::tid();
if (!isset($this->profiles[$cid][$name])) {
$this->profiles[$cid][$name] = [
'cost' => 0,
'total' => 0
'total' => 0,
];
}

$this->profiles[$cid][$name]['cost'] += microtime(true) - $this->profileStacks[$cid][$name]['start'];
$this->profiles[$cid][$name]['cost'] += microtime(true) - $this->profileStacks[$cid][$name]['start'];
$this->profiles[$cid][$name]['total'] = $this->profiles[$cid][$name]['total'] + 1;
}

Expand Down Expand Up @@ -310,18 +321,17 @@ public function getTrace($message)
{
$traces = debug_backtrace();
$count = count($traces);

$ex = '';
if ($count >= 4) {
$info = $traces[3];
if ($count >= 7) {
$info = $traces[6];
if (isset($info['file'], $info['line'])) {
$filename = basename($info['file']);
$lineNum = $info['line'];
$ex = "$filename:$lineNum";
}
}
if ($count >= 5) {
$info = $traces[4];
if ($count >= 8) {
$info = $traces[7];
if (isset($info['class'], $info['type'], $info['function'])) {
$ex .= ',' . $info['class'] . $info['type'] . $info['function'];
} elseif (isset($info['function'])) {
Expand Down Expand Up @@ -364,6 +374,9 @@ public function flushLog()
*/
public function appendNoticeLog($flush = false)
{
if (!$this->enable) {
return;
}
$cid = Coroutine::tid();
$ts = $this->getLoggerTime();

Expand Down
5 changes: 5 additions & 0 deletions src/Pool/ConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Swoft\App;
use Swoft\Core\RequestContext;
use Swoft\Exception\ConnectionException;
use Swoft\Exception\PoolException;
use Swoft\Helper\PoolHelper;
use Swoole\Coroutine\Channel;

Expand Down Expand Up @@ -42,6 +43,10 @@ abstract class ConnectionPool implements PoolInterface
*/
public function init()
{
if (empty($this->poolConfig)) {
throw new PoolException('You must to set poolConfig by @Inject!');
}

if (App::isWorkerStatus()) {
$this->channel = new Channel($this->poolConfig->getMaxActive());
} else {
Expand Down

0 comments on commit 400d1ff

Please sign in to comment.