Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit 6c3a58e

Browse files
committed
update
1 parent c710b8d commit 6c3a58e

File tree

3 files changed

+242
-0
lines changed

3 files changed

+242
-0
lines changed

src/Helpers/Req.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ public static function buildQueryParams($data, $separator = '/')
164164
return $newArr;
165165
}
166166

167+
/**
168+
* @param $name
169+
* @param string $default
170+
* @return mixed
171+
*/
172+
public static function serverParam($name, $default = '')
173+
{
174+
return self::server($name, $default);
175+
}
176+
167177
/**
168178
* get $_SERVER value
169179
* @param string $name

src/Traits/LogProfileTrait.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-10-18
6+
* Time: 13:10
7+
*/
8+
9+
namespace Inhere\Library\Traits;
10+
11+
use Inhere\Library\Helpers\PhpHelper;
12+
use Monolog\Logger;
13+
14+
/**
15+
* Trait LogProfileTrait
16+
* @package Inhere\Library\Traits
17+
*
18+
* @method log(int $level, string $message, array $context = [])
19+
*/
20+
trait LogProfileTrait
21+
{
22+
/**
23+
* @var array
24+
*/
25+
private $profiles = [];
26+
27+
/**
28+
* mark data analysis start
29+
* @param $name
30+
* @param array $context
31+
* @param string $category
32+
*/
33+
public function profile($name, array $context = [], $category = 'application')
34+
{
35+
$data = [
36+
'_profile_stats' => [
37+
'startTime' => microtime(true),
38+
'startMem' => memory_get_usage(),
39+
],
40+
'_profile_start' => $context,
41+
'_profile_end' => null,
42+
];
43+
44+
$this->profiles[$category][$name] = $data;
45+
}
46+
47+
/**
48+
* mark data analysis end
49+
* @param string $name
50+
* @param string|null $title
51+
* @param array $context
52+
* @param string $category
53+
*/
54+
public function profileEnd($name, $title = null, array $context = [], $category = 'application')
55+
{
56+
if (isset($this->profiles[$category][$name])) {
57+
$data = $this->profiles[$category][$name];
58+
59+
$old = $data['_profile_stats'];
60+
$data['_profile_stats'] = PhpHelper::runtime($old['startTime'], $old['startMem']);
61+
$data['_profile_end'] = $context;
62+
63+
$title = $category . ' - ' . ($title ?: $name);
64+
65+
$this->log(Logger::DEBUG, $title, $data);
66+
}
67+
}
68+
69+
}

src/Traits/LogShortTrait.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/9/21
6+
* Time: 下午11:26
7+
*/
8+
9+
namespace Inhere\Library\Traits;
10+
11+
use Inhere\Library\Helpers\PhpHelper;
12+
use Inhere\Library\Helpers\Req;
13+
use Monolog\Logger;
14+
15+
/**
16+
* Class LogShortTrait
17+
* @package Sws\Components
18+
*/
19+
trait LogShortTrait
20+
{
21+
/**
22+
* System is unusable.
23+
*
24+
* @param string $message
25+
* @param array $context
26+
*
27+
* @return void
28+
*/
29+
public static function emergency($message, array $context = array())
30+
{
31+
self::log(Logger::EMERGENCY, $message, $context);
32+
}
33+
34+
/**
35+
* Action must be taken immediately.
36+
*
37+
* Example: Entire website down, database unavailable, etc. This should
38+
* trigger the SMS alerts and wake you up.
39+
*
40+
* @param string $message
41+
* @param array $context
42+
*
43+
* @return void
44+
*/
45+
public static function alert($message, array $context = array())
46+
{
47+
self::log(Logger::ALERT, $message, $context);
48+
}
49+
50+
/**
51+
* Critical conditions.
52+
*
53+
* Example: Application component unavailable, unexpected exception.
54+
*
55+
* @param string $message
56+
* @param array $context
57+
*
58+
* @return void
59+
*/
60+
public static function critical($message, array $context = array())
61+
{
62+
self::log(Logger::CRITICAL, $message, $context);
63+
}
64+
65+
/**
66+
* Runtime errors that do not require immediate action but should typically
67+
* be logged and monitored.
68+
*
69+
* @param string $message
70+
* @param array $context
71+
*
72+
* @return void
73+
*/
74+
public static function error($message, array $context = array())
75+
{
76+
self::log(Logger::ERROR, $message, $context);
77+
}
78+
79+
/**
80+
* Exceptional occurrences that are not errors.
81+
*
82+
* Example: Use of deprecated APIs, poor use of an API, undesirable things
83+
* that are not necessarily wrong.
84+
*
85+
* @param string $message
86+
* @param array $context
87+
*
88+
* @return void
89+
*/
90+
public static function warning($message, array $context = array())
91+
{
92+
self::log(Logger::WARNING, $message, $context);
93+
}
94+
95+
/**
96+
* Normal but significant events.
97+
*
98+
* @param string $message
99+
* @param array $context
100+
*
101+
* @return void
102+
*/
103+
public static function notice($message, array $context = array())
104+
{
105+
self::log(Logger::NOTICE, $message, $context);
106+
}
107+
108+
/**
109+
* Interesting events.
110+
*
111+
* Example: User logs in, SQL logs.
112+
*
113+
* @param string $message
114+
* @param array $context
115+
*
116+
* @return void
117+
*/
118+
public static function info($message, array $context = array())
119+
{
120+
self::log(Logger::INFO, $message, $context);
121+
}
122+
123+
/**
124+
* Detailed debug information.
125+
*
126+
* @param string $message
127+
* @param array $context
128+
*
129+
* @return void
130+
*/
131+
public static function debug($message, array $context = array())
132+
{
133+
self::log(Logger::DEBUG, $message, $context);
134+
}
135+
136+
/**
137+
* @param string $message
138+
* @param array $context
139+
*/
140+
public static function trace($message, array $context = array())
141+
{
142+
$tce = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
143+
144+
if ($info = $tce[1] ?? null) {
145+
$context['_called_at'] = sprintf('%s::%s Line %d', $info['class'], $info['function'], $tce[0]['line']);
146+
}
147+
148+
$context['_stats'] = PhpHelper::runtime(Req::server('request_time_float'), Req::server('request_memory'));
149+
150+
self::log(Logger::DEBUG, $message, $context);
151+
}
152+
153+
/**
154+
* Logs with an arbitrary level.
155+
*
156+
* @param mixed $level
157+
* @param string $message
158+
* @param array $context
159+
*
160+
* @return void
161+
*/
162+
abstract public static function log($level, $message, array $context = array());
163+
}

0 commit comments

Comments
 (0)