Qiang Xue
13 years ago
9 changed files with 272 additions and 460 deletions
@ -1,146 +1,76 @@
|
||||
<?php |
||||
/** |
||||
* CEmailLogRoute class file. |
||||
* EmailTarget class file. |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright © 2008-2011 Yii Software LLC |
||||
* @copyright Copyright © 2008-2012 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\logging; |
||||
|
||||
/** |
||||
* CEmailLogRoute sends selected log messages to email addresses. |
||||
* EmailTarget sends selected log messages to the specified email addresses. |
||||
* |
||||
* The target email addresses may be specified via {@link setEmails emails} property. |
||||
* Optionally, you may set the email {@link setSubject subject}, the |
||||
* {@link setSentFrom sentFrom} address and any additional {@link setHeaders headers}. |
||||
* The target email addresses may be specified via [[emails]] property. |
||||
* Optionally, you may set the email [[subject]], [[sentFrom]] address and |
||||
* additional [[headers]]. |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @version $Id: CEmailLogRoute.php 3001 2011-02-24 16:42:44Z alexander.makarow $ |
||||
* @package system.logging |
||||
* @since 1.0 |
||||
* @since 2.0 |
||||
*/ |
||||
class CEmailLogRoute extends CLogRoute |
||||
class EmailTarget extends Target |
||||
{ |
||||
/** |
||||
* @var array list of destination email addresses. |
||||
*/ |
||||
private $_email = array(); |
||||
public $emails = array(); |
||||
/** |
||||
* @var string email subject |
||||
*/ |
||||
private $_subject; |
||||
public $subject; |
||||
/** |
||||
* @var string email sent from address |
||||
* @var string email sent-from address |
||||
*/ |
||||
private $_from; |
||||
public $sentFrom; |
||||
/** |
||||
* @var array list of additional headers to use when sending an email. |
||||
*/ |
||||
private $_headers = array(); |
||||
public $headers = array(); |
||||
|
||||
/** |
||||
* Sends log messages to specified email addresses. |
||||
* @param array $logs list of log messages |
||||
* Sends log [[messages]] to specified email addresses. |
||||
* @param boolean $final whether this method is called at the end of the current application |
||||
*/ |
||||
protected function processLogs($logs) |
||||
public function exportMessages($final) |
||||
{ |
||||
$message = ''; |
||||
foreach ($logs as $log) |
||||
$message .= $this->formatLogMessage($log[0], $log[1], $log[2], $log[3]); |
||||
$message = wordwrap($message, 70); |
||||
$subject = $this->getSubject(); |
||||
if ($subject === null) |
||||
$subject = Yii::t('yii', 'Application Log'); |
||||
foreach ($this->getEmails() as $email) |
||||
$this->sendEmail($email, $subject, $message); |
||||
$body = ''; |
||||
foreach ($this->messages as $message) { |
||||
$body .= $this->formatMessage($message); |
||||
} |
||||
$body = wordwrap($body, 70); |
||||
$subject = $this->subject === null ? Yii::t('yii', 'Application Log') : $this->subject; |
||||
foreach ($this->emails as $email) { |
||||
$this->sendEmail($subject, $body, $email, $this->sentFrom, $this->headers); |
||||
} |
||||
|
||||
$this->messages = array(); |
||||
} |
||||
|
||||
/** |
||||
* Sends an email. |
||||
* @param string $email single email address |
||||
* @param string $subject email subject |
||||
* @param string $message email content |
||||
*/ |
||||
protected function sendEmail($email, $subject, $message) |
||||
{ |
||||
$headers = $this->getHeaders(); |
||||
if (($from = $this->getSentFrom()) !== null) |
||||
$headers[] = "From: {$from}"; |
||||
mail($email, $subject, $message, implode("\r\n", $headers)); |
||||
} |
||||
|
||||
/** |
||||
* @return array list of destination email addresses |
||||
*/ |
||||
public function getEmails() |
||||
{ |
||||
return $this->_email; |
||||
} |
||||
|
||||
/** |
||||
* @param mixed $value list of destination email addresses. If the value is |
||||
* a string, it is assumed to be comma-separated email addresses. |
||||
*/ |
||||
public function setEmails($value) |
||||
{ |
||||
if (is_array($value)) |
||||
$this->_email = $value; |
||||
else |
||||
$this->_email = preg_split('/[\s,]+/', $value, -1, PREG_SPLIT_NO_EMPTY); |
||||
} |
||||
|
||||
/** |
||||
* @return string email subject. Defaults to CEmailLogRoute::DEFAULT_SUBJECT |
||||
*/ |
||||
public function getSubject() |
||||
{ |
||||
return $this->_subject; |
||||
} |
||||
|
||||
/** |
||||
* @param string $value email subject. |
||||
*/ |
||||
public function setSubject($value) |
||||
{ |
||||
$this->_subject = $value; |
||||
} |
||||
|
||||
/** |
||||
* @return string send from address of the email |
||||
*/ |
||||
public function getSentFrom() |
||||
{ |
||||
return $this->_from; |
||||
} |
||||
|
||||
/** |
||||
* @param string $value send from address of the email |
||||
*/ |
||||
public function setSentFrom($value) |
||||
{ |
||||
$this->_from = $value; |
||||
} |
||||
|
||||
/** |
||||
* @return array additional headers to use when sending an email. |
||||
* @since 1.1.4 |
||||
*/ |
||||
public function getHeaders() |
||||
{ |
||||
return $this->_headers; |
||||
} |
||||
|
||||
/** |
||||
* @param mixed $value list of additional headers to use when sending an email. |
||||
* If the value is a string, it is assumed to be line break separated headers. |
||||
* @since 1.1.4 |
||||
* @param string $body email body |
||||
* @param string $sentTo sent-to email address |
||||
* @param string $sentFrom sent-from email address |
||||
* @param array $headers additional headers to be used when sending the email |
||||
*/ |
||||
public function setHeaders($value) |
||||
protected function sendEmail($subject, $body, $sentTo, $sentFrom, $headers) |
||||
{ |
||||
if (is_array($value)) |
||||
$this->_headers = $value; |
||||
else |
||||
$this->_headers = preg_split('/\r\n|\n/', $value, -1, PREG_SPLIT_NO_EMPTY); |
||||
if ($sentFrom !== null) { |
||||
$headers[] = "From: {$sentFrom}"; |
||||
} |
||||
mail($email, $subject, $body, implode("\r\n", $headers)); |
||||
} |
||||
} |
@ -1,107 +0,0 @@
|
||||
<?php |
||||
/** |
||||
* CLogFilter class file |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright © 2008-2011 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
/** |
||||
* CLogFilter preprocesses the logged messages before they are handled by a log route. |
||||
* |
||||
* CLogFilter is meant to be used by a log route to preprocess the logged messages |
||||
* before they are handled by the route. The default implementation of CLogFilter |
||||
* prepends additional context information to the logged messages. In particular, |
||||
* by setting {@link logVars}, predefined PHP variables such as |
||||
* $_SERVER, $_POST, etc. can be saved as a log message, which may help identify/debug |
||||
* issues encountered. |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @version $Id: CLogFilter.php 3204 2011-05-05 21:36:32Z alexander.makarow $ |
||||
* @package system.logging |
||||
* @since 1.0.6 |
||||
*/ |
||||
class CLogFilter extends CComponent |
||||
{ |
||||
/** |
||||
* @var boolean whether to prefix each log message with the current user session ID. |
||||
* Defaults to false. |
||||
*/ |
||||
public $prefixSession = false; |
||||
/** |
||||
* @var boolean whether to prefix each log message with the current user |
||||
* {@link CWebUser::name name} and {@link CWebUser::id ID}. Defaults to false. |
||||
*/ |
||||
public $prefixUser = false; |
||||
/** |
||||
* @var boolean whether to log the current user name and ID. Defaults to true. |
||||
*/ |
||||
public $logUser = true; |
||||
/** |
||||
* @var array list of the PHP predefined variables that should be logged. |
||||
* Note that a variable must be accessible via $GLOBALS. Otherwise it won't be logged. |
||||
*/ |
||||
public $logVars = array('_GET', '_POST', '_FILES', '_COOKIE', '_SESSION', '_SERVER'); |
||||
|
||||
|
||||
/** |
||||
* Filters the given log messages. |
||||
* This is the main method of CLogFilter. It processes the log messages |
||||
* by adding context information, etc. |
||||
* @param array $logs the log messages |
||||
* @return array |
||||
*/ |
||||
public function filter(&$logs) |
||||
{ |
||||
if (!empty($logs)) |
||||
{ |
||||
if (($message = $this->getContext()) !== '') |
||||
array_unshift($logs, array($message, CLogger::LEVEL_INFO, 'application', YII_BEGIN_TIME)); |
||||
$this->format($logs); |
||||
} |
||||
return $logs; |
||||
} |
||||
|
||||
/** |
||||
* Formats the log messages. |
||||
* The default implementation will prefix each message with session ID |
||||
* if {@link prefixSession} is set true. It may also prefix each message |
||||
* with the current user's name and ID if {@link prefixUser} is true. |
||||
* @param array $logs the log messages |
||||
*/ |
||||
protected function format(&$logs) |
||||
{ |
||||
$prefix = ''; |
||||
if ($this->prefixSession && ($id = session_id()) !== '') |
||||
$prefix .= "[$id]"; |
||||
if ($this->prefixUser && ($user = Yii::app()->getComponent('user', false)) !== null) |
||||
$prefix .= '[' . $user->getName() . '][' . $user->getId() . ']'; |
||||
if ($prefix !== '') |
||||
{ |
||||
foreach ($logs as &$log) |
||||
$log[0] = $prefix . ' ' . $log[0]; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Generates the context information to be logged. |
||||
* The default implementation will dump user information, system variables, etc. |
||||
* @return string the context information. If an empty string, it means no context information. |
||||
*/ |
||||
protected function getContext() |
||||
{ |
||||
$context = array(); |
||||
if ($this->logUser && ($user = Yii::app()->getComponent('user', false)) !== null) |
||||
$context[] = 'User: ' . $user->getName() . ' (ID: ' . $user->getId() . ')'; |
||||
|
||||
foreach ($this->logVars as $name) |
||||
{ |
||||
if (!empty($GLOBALS[$name])) |
||||
$context[] = "\$ {$name}=" . var_export($GLOBALS[$name], true); |
||||
} |
||||
|
||||
return implode("\n\n", $context); |
||||
} |
||||
} |
Loading…
Reference in new issue