winter/modules/system/models/EventLog.php

90 lines
2.1 KiB
PHP
Raw Normal View History

<?php namespace System\Models;
2016-04-16 07:44:18 +10:00
use App;
use Str;
use October\Rain\Database\Model;
use Exception;
/**
* 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
*/
class EventLog extends Model
{
/**
* @var string The database table used by the model.
*/
protected $table = 'system_event_logs';
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'];
/**
* 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() &&
!defined('OCTOBER_NO_EVENT_LOGGING') &&
2017-02-23 08:04:20 +11:00
LogSetting::get('log_events')
);
}
/**
* 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
try {
$record->save();
}
catch (Exception $ex) {}
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);
}
/**
* 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)) {
return $match[1];
2014-10-18 11:58:50 +02:00
}
return Str::limit($this->message, 100);
}
2014-10-18 11:58:50 +02:00
}