Skip to content

Commit 8bc2105

Browse files
author
Dmitry Tarasov
committed
Add standalone direct logger with protocol stub
1 parent e63c95f commit 8bc2105

3 files changed

Lines changed: 91 additions & 0 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"license": "MIT",
88
"require": {
99
"php": ">=5.3.0",
10+
"psr/log": "~1.0",
1011
"monolog/monolog": "dev-master",
1112
"apache/log4php": "dev-master"
1213
},
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Stackify\Log\Standalone;
4+
5+
use Stackify\Log\Standalone\MessageBuilder;
6+
7+
use Psr\Log\AbstractLogger;
8+
9+
class Logger extends AbstractLogger
10+
{
11+
12+
/**
13+
* @var \DateTimeZone
14+
*/
15+
protected $timezone;
16+
17+
/**
18+
* @var \Stackify\Log\MessageBuilder\BuilderInterface
19+
*/
20+
private $builder;
21+
22+
public function __construct()
23+
{
24+
$this->builder = new MessageBuilder();
25+
$this->timezone = new \DateTimeZone(date_default_timezone_get() ?: 'UTC');
26+
}
27+
28+
public function log($level, $message, array $context = array()) {
29+
$logEvent = array(
30+
'message' => (string) $message,
31+
'context' => $context,
32+
'level' => $level,
33+
'datetime' => new \DateTime('now', $this->timezone),
34+
);
35+
$logEvent['formatted'] = $this->builder->getSyslogMessage($logEvent);
36+
$this->write($logEvent);
37+
}
38+
39+
protected function write($logEvent)
40+
{
41+
// @TODO this is stub implementation
42+
echo $logEvent['formatted'];
43+
}
44+
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Stackify\Log\Standalone;
4+
5+
use Stackify\Log\Entities\LogMsg;
6+
use Stackify\Log\MessageBuilder\AbstractBuilder;
7+
8+
class MessageBuilder extends AbstractBuilder
9+
{
10+
11+
protected function getLoggerName()
12+
{
13+
return 'Stackify PHP Logger';
14+
}
15+
16+
protected function getLoggerVersion()
17+
{
18+
return '1.0';
19+
}
20+
21+
/**
22+
* @return \Stackify\Log\Entities\LogMsg
23+
*/
24+
protected function createLogMsg($logEvent)
25+
{
26+
$logMsg = new LogMsg(
27+
$logEvent['level'],
28+
$logEvent['message'],
29+
$logEvent['datetime']
30+
);
31+
$context = $logEvent['context'];
32+
$exception = $this->popException($context);
33+
if (null !== $exception) {
34+
$error = $this->createErrorFromException($logEvent['datetime'], $exception);
35+
$logMsg->Ex = $error;
36+
$logMsg->SrcLine = $exception->getLine();
37+
$logMsg->SrcMethod = $error->Error->SourceMethod;
38+
}
39+
if (!empty($context)) {
40+
$logMsg->data = $this->encodeJSON($context);
41+
}
42+
return $logMsg;
43+
}
44+
45+
}

0 commit comments

Comments
 (0)