Skip to content

Commit 468f1ce

Browse files
committed
Merge pull request #115 from Rockstar04/logging
PSR-3 Compliant Logging
2 parents b5ce47a + 25a804d commit 468f1ce

File tree

10 files changed

+334
-183
lines changed

10 files changed

+334
-183
lines changed

bin/resque

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ if(!empty($REDIS_BACKEND)) {
3939
Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
4040
}
4141

42-
$logLevel = 0;
42+
$logLevel = false;
4343
$LOGGING = getenv('LOGGING');
4444
$VERBOSE = getenv('VERBOSE');
4545
$VVERBOSE = getenv('VVERBOSE');
4646
if(!empty($LOGGING) || !empty($VERBOSE)) {
47-
$logLevel = Resque_Worker::LOG_NORMAL;
47+
$logLevel = true;
4848
}
4949
else if(!empty($VVERBOSE)) {
50-
$logLevel = Resque_Worker::LOG_VERBOSE;
50+
$logLevel = true;
5151
}
5252

5353
$APP_INCLUDE = getenv('APP_INCLUDE');
@@ -59,6 +59,12 @@ if($APP_INCLUDE) {
5959
require_once $APP_INCLUDE;
6060
}
6161

62+
// See if the APP_INCLUDE containes a logger object,
63+
// If none exists, fallback to internal logger
64+
if (!isset($logger) && !is_object($logger)) {
65+
$logger = new Resque_Log($logLevel);
66+
}
67+
6268
$BLOCKING = getenv('BLOCKING') !== FALSE;
6369

6470
$interval = 5;
@@ -75,22 +81,23 @@ if(!empty($COUNT) && $COUNT > 1) {
7581

7682
$PREFIX = getenv('PREFIX');
7783
if(!empty($PREFIX)) {
78-
fwrite(STDOUT, '*** Prefix set to '.$PREFIX."\n");
84+
$logger->log(Psr\Log\LogLevel::INFO, 'Prefix set to {prefix}', array('prefix' => $PREFIX));
7985
Resque_Redis::prefix($PREFIX);
8086
}
8187

8288
if($count > 1) {
8389
for($i = 0; $i < $count; ++$i) {
8490
$pid = Resque::fork();
8591
if($pid == -1) {
86-
die("Could not fork worker ".$i."\n");
92+
$logger->log(Psr\Log\LogLevel::EMERGENCY, 'Could not fork worker {count}', array('count' => $i));
93+
die();
8794
}
8895
// Child, start the worker
8996
else if(!$pid) {
9097
$queues = explode(',', $QUEUE);
9198
$worker = new Resque_Worker($queues);
92-
$worker->logLevel = $logLevel;
93-
fwrite(STDOUT, '*** Starting worker '.$worker."\n");
99+
$worker->setLogger($logger);
100+
$logger->log(Psr\Log\LogLevel::NOTICE, 'Starting worker {worker}', array('worker' => $worker));
94101
$worker->work($interval, $BLOCKING);
95102
break;
96103
}
@@ -100,15 +107,15 @@ if($count > 1) {
100107
else {
101108
$queues = explode(',', $QUEUE);
102109
$worker = new Resque_Worker($queues);
103-
$worker->logLevel = $logLevel;
110+
$worker->setLogger($logger);
104111

105112
$PIDFILE = getenv('PIDFILE');
106113
if ($PIDFILE) {
107114
file_put_contents($PIDFILE, getmypid()) or
108115
die('Could not write PID information to ' . $PIDFILE);
109116
}
110117

111-
fwrite(STDOUT, '*** Starting worker '.$worker."\n");
118+
$logger->log(Psr\Log\LogLevel::NOTICE, 'Starting worker {worker}', array('worker' => $worker));
112119
$worker->work($interval, $BLOCKING);
113120
}
114121
?>

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"require": {
2121
"php": ">=5.3.0",
2222
"ext-pcntl": "*",
23-
"colinmollenhour/credis": "1.2.*"
23+
"colinmollenhour/credis": "1.2.*",
24+
"psr/log": "1.0.0"
2425
},
2526
"suggest": {
2627
"ext-proctitle": "Allows php-resque to rename the title of UNIX processes to show the status of a worker.",

composer.lock

Lines changed: 59 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Resque/Log.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Resque default logger PSR-3 compliant
4+
*
5+
* @package Resque/Stat
6+
* @author Chris Boulton <[email protected]>
7+
* @license http://www.opensource.org/licenses/mit-license.php
8+
*/
9+
class Resque_Log extends Psr\Log\AbstractLogger
10+
{
11+
public $verbose;
12+
13+
public function __construct($verbose = false) {
14+
$this->verbose = $verbose;
15+
}
16+
17+
/**
18+
* Logs with an arbitrary level.
19+
*
20+
* @param mixed $level PSR-3 log level constant, or equivalent string
21+
* @param string $message Message to log, may contain a { placeholder }
22+
* @param array $context Variables to replace { placeholder }
23+
* @return null
24+
*/
25+
public function log($level, $message, array $context = array())
26+
{
27+
if ($this->verbose) {
28+
fwrite(
29+
STDOUT,
30+
'[' . $level . '] [' . strftime('%T %Y-%m-%d') . '] ' . $this->interpolate($message, $context) . PHP_EOL
31+
);
32+
return;
33+
}
34+
35+
if (!($level === Psr\Log\LogLevel::INFO || $level === Psr\Log\LogLevel::DEBUG)) {
36+
fwrite(
37+
STDOUT,
38+
'[' . $level . '] ' . $this->interpolate($message, $context) . PHP_EOL
39+
);
40+
}
41+
}
42+
43+
/**
44+
* Fill placeholders with the provided context
45+
* @author Jordi Boggiano [email protected]
46+
*
47+
* @param string $message Message to be logged
48+
* @param array $context Array of variables to use in message
49+
* @return string
50+
*/
51+
public function interpolate($message, array $context = array())
52+
{
53+
// build a replacement array with braces around the context keys
54+
$replace = array();
55+
foreach ($context as $key => $val) {
56+
$replace['{' . $key . '}'] = $val;
57+
}
58+
59+
// interpolate replacement values into the message and return
60+
return strtr($message, $replace);
61+
}
62+
}

0 commit comments

Comments
 (0)