Commit 9f312bb1 by 王召彬

test

parent 0faa68a7
<?php
/**
* Auto generated from CLS.proto at 2018-09-07 06:03:23
*
* cls package
*/
namespace Cls {
/**
* Log message
*/
class Log extends \ProtobufMessage
{
/* Field index constants */
const TIME = 1;
const CONTENTS = 2;
/* @var array Field descriptors */
protected static $fields = array(
self::TIME => array(
'name' => 'time',
'required' => true,
'type' => \ProtobufMessage::PB_TYPE_INT,
),
self::CONTENTS => array(
'name' => 'contents',
'repeated' => true,
'type' => '\Cls\Log_Content'
),
);
/**
* Constructs new message container and clears its internal state
*/
public function __construct()
{
$this->reset();
}
/**
* Clears message values and sets default ones
*
* @return null
*/
public function reset()
{
$this->values[self::TIME] = null;
$this->values[self::CONTENTS] = array();
}
/**
* Returns field descriptors
*
* @return array
*/
public function fields()
{
return self::$fields;
}
/**
* Sets value of 'time' property
*
* @param integer $value Property value
*
* @return null
*/
public function setTime($value)
{
return $this->set(self::TIME, $value);
}
/**
* Returns value of 'time' property
*
* @return integer
*/
public function getTime()
{
$value = $this->get(self::TIME);
return $value === null ? (integer)$value : $value;
}
/**
* Appends value to 'contents' list
*
* @param \Cls\Log_Content $value Value to append
*
* @return null
*/
public function appendContents(\Cls\Log_Content $value)
{
return $this->append(self::CONTENTS, $value);
}
/**
* Clears 'contents' list
*
* @return null
*/
public function clearContents()
{
return $this->clear(self::CONTENTS);
}
/**
* Returns 'contents' list
*
* @return \Cls\Log_Content[]
*/
public function getContents()
{
return $this->get(self::CONTENTS);
}
/**
* Returns 'contents' iterator
*
* @return \ArrayIterator
*/
public function getContentsIterator()
{
return new \ArrayIterator($this->get(self::CONTENTS));
}
/**
* Returns element from 'contents' list at given offset
*
* @param int $offset Position in list
*
* @return \Cls\Log_Content
*/
public function getContentsAt($offset)
{
return $this->get(self::CONTENTS, $offset);
}
/**
* Returns count of 'contents' list
*
* @return int
*/
public function getContentsCount()
{
return $this->count(self::CONTENTS);
}
}
}
\ No newline at end of file
<?php
/**
* Auto generated from CLS.proto at 2018-09-07 06:03:23
*
* cls package
*/
namespace Cls {
/**
* LogGroup message
*/
class LogGroup extends \ProtobufMessage
{
/* Field index constants */
const LOGS = 1;
const CONTEXTFLOW = 2;
const FILENAME = 3;
const SOURCE = 4;
/* @var array Field descriptors */
protected static $fields = array(
self::LOGS => array(
'name' => 'logs',
'repeated' => true,
'type' => '\Cls\Log'
),
self::CONTEXTFLOW => array(
'name' => 'contextFlow',
'required' => false,
'type' => \ProtobufMessage::PB_TYPE_STRING,
),
self::FILENAME => array(
'name' => 'filename',
'required' => false,
'type' => \ProtobufMessage::PB_TYPE_STRING,
),
self::SOURCE => array(
'name' => 'source',
'required' => false,
'type' => \ProtobufMessage::PB_TYPE_STRING,
),
);
/**
* Constructs new message container and clears its internal state
*/
public function __construct()
{
$this->reset();
}
/**
* Clears message values and sets default ones
*
* @return null
*/
public function reset()
{
$this->values[self::LOGS] = array();
$this->values[self::CONTEXTFLOW] = null;
$this->values[self::FILENAME] = null;
$this->values[self::SOURCE] = null;
}
/**
* Returns field descriptors
*
* @return array
*/
public function fields()
{
return self::$fields;
}
/**
* Appends value to 'logs' list
*
* @param \Cls\Log $value Value to append
*
* @return null
*/
public function appendLogs(\Cls\Log $value)
{
return $this->append(self::LOGS, $value);
}
/**
* Clears 'logs' list
*
* @return null
*/
public function clearLogs()
{
return $this->clear(self::LOGS);
}
/**
* Returns 'logs' list
*
* @return \Cls\Log[]
*/
public function getLogs()
{
return $this->get(self::LOGS);
}
/**
* Returns 'logs' iterator
*
* @return \ArrayIterator
*/
public function getLogsIterator()
{
return new \ArrayIterator($this->get(self::LOGS));
}
/**
* Returns element from 'logs' list at given offset
*
* @param int $offset Position in list
*
* @return \Cls\Log
*/
public function getLogsAt($offset)
{
return $this->get(self::LOGS, $offset);
}
/**
* Returns count of 'logs' list
*
* @return int
*/
public function getLogsCount()
{
return $this->count(self::LOGS);
}
/**
* Sets value of 'contextFlow' property
*
* @param string $value Property value
*
* @return null
*/
public function setContextFlow($value)
{
return $this->set(self::CONTEXTFLOW, $value);
}
/**
* Returns value of 'contextFlow' property
*
* @return string
*/
public function getContextFlow()
{
$value = $this->get(self::CONTEXTFLOW);
return $value === null ? (string)$value : $value;
}
/**
* Sets value of 'filename' property
*
* @param string $value Property value
*
* @return null
*/
public function setFilename($value)
{
return $this->set(self::FILENAME, $value);
}
/**
* Returns value of 'filename' property
*
* @return string
*/
public function getFilename()
{
$value = $this->get(self::FILENAME);
return $value === null ? (string)$value : $value;
}
/**
* Sets value of 'source' property
*
* @param string $value Property value
*
* @return null
*/
public function setSource($value)
{
return $this->set(self::SOURCE, $value);
}
/**
* Returns value of 'source' property
*
* @return string
*/
public function getSource()
{
$value = $this->get(self::SOURCE);
return $value === null ? (string)$value : $value;
}
}
}
\ No newline at end of file
<?php
/**
* Auto generated from CLS.proto at 2018-09-07 06:03:23
*
* cls package
*/
namespace Cls {
/**
* LogGroupList message
*/
class LogGroupList extends \ProtobufMessage
{
/* Field index constants */
const LOGGROUPLIST = 1;
/* @var array Field descriptors */
protected static $fields = array(
self::LOGGROUPLIST => array(
'name' => 'logGroupList',
'repeated' => true,
'type' => '\Cls\LogGroup'
),
);
/**
* Constructs new message container and clears its internal state
*/
public function __construct()
{
$this->reset();
}
/**
* Clears message values and sets default ones
*
* @return null
*/
public function reset()
{
$this->values[self::LOGGROUPLIST] = array();
}
/**
* Returns field descriptors
*
* @return array
*/
public function fields()
{
return self::$fields;
}
/**
* Appends value to 'logGroupList' list
*
* @param \Cls\LogGroup $value Value to append
*
* @return null
*/
public function appendLogGroupList(\Cls\LogGroup $value)
{
return $this->append(self::LOGGROUPLIST, $value);
}
/**
* Clears 'logGroupList' list
*
* @return null
*/
public function clearLogGroupList()
{
return $this->clear(self::LOGGROUPLIST);
}
/**
* Returns 'logGroupList' list
*
* @return \Cls\LogGroup[]
*/
public function getLogGroupList()
{
return $this->get(self::LOGGROUPLIST);
}
/**
* Returns 'logGroupList' iterator
*
* @return \ArrayIterator
*/
public function getLogGroupListIterator()
{
return new \ArrayIterator($this->get(self::LOGGROUPLIST));
}
/**
* Returns element from 'logGroupList' list at given offset
*
* @param int $offset Position in list
*
* @return \Cls\LogGroup
*/
public function getLogGroupListAt($offset)
{
return $this->get(self::LOGGROUPLIST, $offset);
}
/**
* Returns count of 'logGroupList' list
*
* @return int
*/
public function getLogGroupListCount()
{
return $this->count(self::LOGGROUPLIST);
}
}
}
\ No newline at end of file
<?php
/**
* Auto generated from CLS.proto at 2018-09-07 06:03:23
*
* cls package
*/
namespace Cls {
/**
* Content message embedded in Log message
*/
class Log_Content extends \ProtobufMessage
{
/* Field index constants */
const KEY = 1;
const VALUE = 2;
/* @var array Field descriptors */
protected static $fields = array(
self::KEY => array(
'name' => 'key',
'required' => true,
'type' => \ProtobufMessage::PB_TYPE_STRING,
),
self::VALUE => array(
'name' => 'value',
'required' => true,
'type' => \ProtobufMessage::PB_TYPE_STRING,
),
);
/**
* Constructs new message container and clears its internal state
*/
public function __construct()
{
$this->reset();
}
/**
* Clears message values and sets default ones
*
* @return null
*/
public function reset()
{
$this->values[self::KEY] = null;
$this->values[self::VALUE] = null;
}
/**
* Returns field descriptors
*
* @return array
*/
public function fields()
{
return self::$fields;
}
/**
* Sets value of 'key' property
*
* @param string $value Property value
*
* @return null
*/
public function setKey($value)
{
return $this->set(self::KEY, $value);
}
/**
* Returns value of 'key' property
*
* @return string
*/
public function getKey()
{
$value = $this->get(self::KEY);
return $value === null ? (string)$value : $value;
}
/**
* Sets value of 'value' property
*
* @param string $value Property value
*
* @return null
*/
public function setValue($value)
{
return $this->set(self::VALUE, $value);
}
/**
* Returns value of 'value' property
*
* @return string
*/
public function getValue()
{
$value = $this->get(self::VALUE);
return $value === null ? (string)$value : $value;
}
}
}
\ No newline at end of file
<?php
namespace Hdll\Services\Common\Logger;
<?php
namespace Hdll\Services\Common\Logger;
use Monolog\Handler\AbstractProcessingHandler;
use Swoft\App;
use Swoole\Coroutine;
/**
* 自定义日志处理器,记录到腾讯云Cls
*
* @uses LogHandler
*/
class LogHandler extends AbstractProcessingHandler
{
/**
* @var array 输出包含日志级别集合
*/
protected $levels = [];
/**
* 批量输出日志
*
* @param array $records 日志记录集合
*
* @return bool
*/
public function handleBatch(array $records)
{
$records = $this->recordFilter($records);
if (empty($records)) {
return true;
}
$lines = array_column($records, 'formatted');
$this->write($lines);
}
/**
* 输出到文件
*
* @param array $records 日志记录集合
*/
protected function write(array $records)
{
$messageText = implode("\n", $records) . "\n";
var_dump('__logs: ', $messageText);
/* if (App::isCoContext()) {
// 协程写
$this->coWrite($logFile, $messageText);
} else {
$this->syncWrite($logFile, $messageText);
} */
}
/**
* 记录过滤器
*
* @param array $records 日志记录集合
*
* @return array
*/
private function recordFilter(array $records)
{
$messages = [];
foreach ($records as $record) {
if (!isset($record['level'])) {
continue;
}
if (!$this->isHandling($record)) {
continue;
}
$record = $this->processRecord($record);
$record['formatted'] = $this->getFormatter()->format($record);
$messages[] = $record;
}
return $messages;
}
/**
* check是否输出日志
*
* @param array $record
*
* @return bool
*/
public function isHandling(array $record)
{
if (empty($this->levels)) {
return true;
}
return in_array($record['level'], $this->levels);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment