Skip to content

Commit 7bb1487

Browse files
committed
Merge pull request #32 from tamagokun/logger_interface
Logger interface Closes #31
2 parents 9fa87e0 + 4c1740e commit 7bb1487

File tree

3 files changed

+88
-74
lines changed

3 files changed

+88
-74
lines changed

lib/Rackem/Logger.php

+80-55
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,84 @@
33

44
class Logger
55
{
6-
const FATAL = 5;
7-
const ERROR = 4;
8-
const WARN = 3;
9-
const INFO = 2;
10-
const DEBUG = 1;
11-
12-
public $level=2, $stream, $formatter, $datetime_format;
13-
14-
public function __construct($stream)
15-
{
16-
$this->stream = is_string($stream)? fopen($stream, 'a') : $stream;
17-
$this->datetime_format = "D M d H:i:s Y";
18-
$this->formatter = function($severity, $datetime, $progname, $msg) {
19-
return "[$datetime] $severity -- $progname: $msg\n";
20-
};
21-
}
22-
23-
public function close()
24-
{
25-
if(is_resource($this->stream)) fclose($this->stream);
26-
}
27-
28-
public function info($msg) { return $this->log(Logger::INFO,func_get_args()); }
29-
public function debug($msg) { return $this->log(Logger::DEBUG,func_get_args()); }
30-
public function warn($msg) { return $this->log(Logger::WARN,func_get_args()); }
31-
public function error($msg) { return $this->log(Logger::ERROR,func_get_args()); }
32-
public function fatal($msg) { return $this->log(Logger::FATAL,func_get_args()); }
33-
34-
//protected
35-
protected function log($level,$args)
36-
{
37-
$prog = "";
38-
$message = "";
39-
$block = null;
40-
foreach($args as $arg)
41-
{
42-
if(is_string($arg)) $message = $arg;
43-
if(is_callable($arg)) $block = $arg;
44-
}
45-
if($level < $this->level) return false;
46-
$formatter = $this->formatter;
47-
if(!is_null($block))
48-
{
49-
$prog = $message;
50-
$message = $block();
51-
}
52-
return fwrite($this->stream, $formatter($this->severity_label($level),@date($this->datetime_format),$prog,$message));
53-
}
54-
55-
protected function severity_label($level)
56-
{
57-
$ref = new \ReflectionClass($this);
58-
foreach($ref->getConstants() as $key=>$value) if($value == $level) return $key;
59-
return "";
60-
}
6+
const EMERGENCY = 'emergency';
7+
const ALERT = 'alert';
8+
const CRITICAL = 'critical';
9+
const ERROR = 'error';
10+
const WARNING = 'warning';
11+
const NOTICE = 'notice';
12+
const INFO = 'info';
13+
const DEBUG = 'debug';
14+
15+
public $stream, $formatter, $datetime_format;
16+
17+
public function __construct($stream)
18+
{
19+
$this->stream = is_string($stream)? fopen($stream, 'a') : $stream;
20+
$this->datetime_format = "D M d H:i:s Y";
21+
$this->formatter = function($severity, $datetime, $msg) {
22+
return "[$datetime] $severity -- $msg\n";
23+
};
24+
}
25+
26+
public function close()
27+
{
28+
if(is_resource($this->stream)) fclose($this->stream);
29+
}
30+
31+
public function emergency($message, array $context = array())
32+
{
33+
$this->log(static::EMERGENCY, $message, $context);
34+
}
35+
36+
public function alert($message, array $context = array())
37+
{
38+
$this->log(static::ALERT, $message, $context);
39+
}
40+
41+
public function critical($message, array $context = array())
42+
{
43+
$this->log(static::CRITICAL, $message, $context);
44+
}
45+
46+
public function error($message, array $context = array())
47+
{
48+
$this->log(static::ERROR, $message, $context);
49+
}
50+
51+
public function warning($message, array $context = array())
52+
{
53+
$this->log(static::WARNING, $message, $context);
54+
}
55+
56+
public function notice($message, array $context = array())
57+
{
58+
$this->log(static::NOTICE, $message, $context);
59+
}
60+
61+
public function info($message, array $context = array())
62+
{
63+
$this->log(static::INFO, $message, $context);
64+
}
65+
66+
public function debug($message, array $context = array())
67+
{
68+
$this->log(static::DEBUG, $message, $context);
69+
}
70+
71+
public function log($level, $message, array $context = array())
72+
{
73+
if(!defined(__CLASS__.'::'.strtoupper($level))) {
74+
throw new \InvalidArgumentException("Log level $level is not defined.");
75+
}
76+
77+
if(is_callable($message)) $message = $message($context);
78+
$this->write($level, $message, $this->formatter);
79+
}
80+
81+
//protected
82+
protected function write($level, $message, $format)
83+
{
84+
fwrite( $this->stream, $format(strtoupper($level), @date($this->datetime_format), $message) );
85+
}
6186
}

lib/Rackem/RackLogger.php

+7-18
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
<?php
22
namespace Rackem;
33

4-
class RackLogger
4+
class RackLogger extends Middleware
55
{
6-
protected $app,$level;
7-
8-
public function __construct($app, $level = \Rackem\Logger::INFO)
9-
{
10-
$this->app = $app;
11-
$this->level = empty($level)? \Rackem\Logger::INFO : $level;
12-
}
13-
14-
public function call($env)
15-
{
16-
$logger = new \Rackem\Logger($env['rack.errors']);
17-
$logger->level = $this->level;
18-
19-
$env['rack.logger'] = $logger;
20-
return $this->app->call($env);
21-
}
22-
}
6+
public function call($env)
7+
{
8+
$env['rack.logger'] = new \Rackem\Logger($env['rack.errors']);
9+
return $this->app->call($env);
10+
}
11+
}

lib/Rackem/Server/Connection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function read()
128128
list($request, $headers) = $this->parse_header(substr($this->buffer, 0, $end));
129129
list($version, $status, $phrase) = explode(' ', $request, 3);
130130
$this->status = $status;
131-
$this->response_length = (int)$headers['Content-Length'][0];
131+
$this->response_length = isset($headers['Content-Length'])? (int)$headers['Content-Length'][0] : 0;
132132
}
133133

134134
/*

0 commit comments

Comments
 (0)