2014-07-29 20:07:20 +10:00
|
|
|
<?php namespace System\Models;
|
|
|
|
|
2016-04-16 07:44:18 +10:00
|
|
|
use App;
|
2014-08-06 18:18:10 +10:00
|
|
|
use Str;
|
2015-08-01 11:15:17 +10:00
|
|
|
use October\Rain\Database\Model;
|
2015-04-18 07:50:56 +10:00
|
|
|
use Exception;
|
2014-07-29 20:07:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Model for logging system errors and debug trace messages
|
2014-10-03 18:00:21 +10:00
|
|
|
*
|
|
|
|
* @package october\system
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
2014-07-29 20:07:20 +10:00
|
|
|
*/
|
2014-07-30 17:18:59 +10:00
|
|
|
class EventLog extends Model
|
2014-07-29 20:07:20 +10:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string The database table used by the model.
|
|
|
|
*/
|
2014-07-30 17:18:59 +10:00
|
|
|
protected $table = 'system_event_logs';
|
2014-07-29 20:07:20 +10:00
|
|
|
|
2014-07-31 19:35:51 +10:00
|
|
|
/**
|
|
|
|
* @var array List of attribute names which are json encoded and decoded from the database.
|
|
|
|
*/
|
|
|
|
protected $jsonable = ['details'];
|
|
|
|
|
2015-08-01 11:15:17 +10:00
|
|
|
/**
|
|
|
|
* Returns true if this logger should be used.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function useLogging()
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
class_exists('Model') &&
|
2015-09-30 05:26:12 +10:00
|
|
|
Model::getConnectionResolver() &&
|
2016-04-16 07:44:18 +10:00
|
|
|
App::hasDatabase() &&
|
2017-02-08 05:43:40 +11:00
|
|
|
!defined('OCTOBER_NO_EVENT_LOGGING') &&
|
2017-02-23 08:04:20 +11:00
|
|
|
LogSetting::get('log_events')
|
2015-08-01 11:15:17 +10:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-07-29 20:07:20 +10:00
|
|
|
/**
|
|
|
|
* Creates a log record
|
|
|
|
* @param string $message Specifies the message text
|
|
|
|
* @param string $level Specifies the logging level
|
|
|
|
* @param string $details Specifies the error details string
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public static function add($message, $level = 'info', $details = null)
|
|
|
|
{
|
|
|
|
$record = new static;
|
|
|
|
$record->message = $message;
|
|
|
|
$record->level = $level;
|
2014-07-31 19:35:51 +10:00
|
|
|
|
2014-10-18 11:58:50 +02:00
|
|
|
if ($details !== null) {
|
2014-07-31 19:35:51 +10:00
|
|
|
$record->details = (array) $details;
|
2014-10-18 11:58:50 +02:00
|
|
|
}
|
2014-07-31 19:35:51 +10:00
|
|
|
|
2015-04-18 07:50:56 +10:00
|
|
|
try {
|
|
|
|
$record->save();
|
|
|
|
}
|
|
|
|
catch (Exception $ex) {}
|
2014-07-29 20:07:20 +10:00
|
|
|
|
|
|
|
return $record;
|
|
|
|
}
|
|
|
|
|
2014-07-31 19:35:51 +10:00
|
|
|
/**
|
|
|
|
* Beautify level value.
|
|
|
|
* @param string $level
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLevelAttribute($level)
|
|
|
|
{
|
|
|
|
return ucfirst($level);
|
|
|
|
}
|
|
|
|
|
2014-08-06 18:18:10 +10:00
|
|
|
/**
|
|
|
|
* Creates a shorter version of the message attribute,
|
|
|
|
* extracts the exception message or limits by 100 characters.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSummaryAttribute()
|
|
|
|
{
|
2014-10-18 11:58:50 +02:00
|
|
|
if (preg_match("/with message '(.+)' in/", $this->message, $match)) {
|
2014-08-06 18:18:10 +10:00
|
|
|
return $match[1];
|
2014-10-18 11:58:50 +02:00
|
|
|
}
|
2014-08-06 18:18:10 +10:00
|
|
|
|
|
|
|
return Str::limit($this->message, 100);
|
|
|
|
}
|
2014-10-18 11:58:50 +02:00
|
|
|
}
|