Skip to content

Commit 1745963

Browse files
authored
Merge pull request #16853 from niden-code/T16852-logger-adjustments
T16852 logger adjustments
2 parents e84b8bc + 3174c91 commit 1745963

File tree

7 files changed

+85
-52
lines changed

7 files changed

+85
-52
lines changed

CHANGELOG-5.0.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
- Changed `Phalcon\Filter\Sanitize\IP` to optimize the sanatization of IP address [#16838](https://github.com/phalcon/cphalcon/issues/16838)
77
- Changed `Phalcon\Encryption\Security\JWT\Builder::setPassphrase()` to require digits and special characters [#16847](https://github.com/phalcon/cphalcon/issues/16847)
88
- Changed `Phalcon\Encryption\Security\JWT\Builder::getAudience()` to return an empty array if not set [#16846](https://github.com/phalcon/cphalcon/issues/16846)
9-
- Changed `Phalcon\Encryption\Security\Random::base()` to use 16 bits by default [#16845](https://github.com/phalcon/cphalcon/issues/16845)
9+
- Changed `Phalcon\Encryption\Security\Random::base()` to use 16 bits by default [#16845](https://github.com/phalcon/cphalcon/issues/16845)
10+
- Changed `Phalcon\Logger\Logger` to use lowercase when reporting log levels (previously uppercase) [#16852](https://github.com/phalcon/cphalcon/issues/16852)
11+
- Changed `Phalcon\Logger\Adapter\Stream` to use a more efficient way to write messages in the logger instead of opening and closing the stream per message [#16852](https://github.com/phalcon/cphalcon/issues/16852)
12+
- Changed `Phalcon\Logger\Adapter\Syslog` to use the `Enum` instead of `Logger` constants [#16852](https://github.com/phalcon/cphalcon/issues/16852)
1013

1114
### Added
1215

phalcon/Logger/AbstractLogger.zep

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ abstract class AbstractLogger
321321
* If someone uses "critical" as the level (string)
322322
*/
323323
if (typeof level === "string") {
324-
let levelName = strtoupper(level),
324+
let levelName = strtolower(level),
325325
levels = array_flip(this->getLevels());
326326

327327
if (isset(levels[levelName])) {
@@ -346,15 +346,15 @@ abstract class AbstractLogger
346346
protected function getLevels() -> array
347347
{
348348
return [
349-
self::ALERT : "ALERT",
350-
self::CRITICAL : "CRITICAL",
351-
self::DEBUG : "DEBUG",
352-
self::EMERGENCY : "EMERGENCY",
353-
self::ERROR : "ERROR",
354-
self::INFO : "INFO",
355-
self::NOTICE : "NOTICE",
356-
self::WARNING : "WARNING",
357-
self::CUSTOM : "CUSTOM"
349+
self::ALERT : "alert",
350+
self::CRITICAL : "critical",
351+
self::DEBUG : "debug",
352+
self::EMERGENCY : "emergency",
353+
self::ERROR : "error",
354+
self::INFO : "info",
355+
self::NOTICE : "notice",
356+
self::WARNING : "warning",
357+
self::CUSTOM : "custom"
358358
];
359359
}
360360
}

phalcon/Logger/Adapter/Stream.zep

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,26 @@ use Phalcon\Logger\Item;
2323
* $logger = new \Phalcon\Logger\Adapter\Stream('app/logs/test.log');
2424
*
2525
* $logger->log('This is a message');
26-
* $logger->log(\Phalcon\Logger::ERROR, 'This is an error');
26+
* $logger->log(\Phalcon\Logger\Enum::ERROR, 'This is an error');
2727
* $logger->error('This is another error');
2828
*
2929
* $logger->close();
3030
*```
3131
*
32+
* @property resource|null $handler
3233
* @property string $mode
3334
* @property string $name
3435
* @property array $options
3536
*/
3637
class Stream extends AbstractAdapter
3738
{
39+
/**
40+
* Stream handler resource
41+
*
42+
* @var resource|null
43+
*/
44+
protected handler = null;
45+
3846
/**
3947
* The file open mode. Defaults to 'ab'
4048
*
@@ -85,6 +93,14 @@ class Stream extends AbstractAdapter
8593
*/
8694
public function close() -> bool
8795
{
96+
var handler;
97+
98+
if (this->handler !== null) {
99+
let handler = this->handler,
100+
this->handler = null;
101+
return this->phpFclose(handler);
102+
}
103+
88104
return true;
89105
}
90106

@@ -105,28 +121,34 @@ class Stream extends AbstractAdapter
105121
*/
106122
public function process(<Item> item) -> void
107123
{
108-
var handler, message;
109-
110-
let handler = this->phpFopen(this->name, this->mode);
111-
112-
if !is_resource(handler) {
113-
throw new LogicException(
114-
"The file '" .
115-
this->name .
116-
"' cannot be opened with mode '" .
117-
this->mode .
118-
"'"
119-
);
124+
var message;
125+
126+
if (!is_resource(this->handler)) {
127+
let this->handler = this->phpFopen(this->name, this->mode);
128+
129+
if (!is_resource(this->handler)) {
130+
let this->handler = null;
131+
132+
throw new LogicException(
133+
"The file '" .
134+
this->name .
135+
"' cannot be opened with mode '" .
136+
this->mode .
137+
"'"
138+
);
139+
}
120140
}
121141

122-
/**
123-
* Not much we can do about locking
124-
*/
125-
flock(handler, LOCK_EX);
126142
let message = this->getFormattedItem(item) . PHP_EOL;
127-
fwrite(handler, message);
143+
this->phpFwrite(this->handler, message);
144+
}
128145

129-
fclose(handler);
146+
/**
147+
* @todo to be removed when we get traits
148+
*/
149+
protected function phpFclose(var handle) -> bool
150+
{
151+
return fclose(handle);
130152
}
131153

132154
/**
@@ -136,4 +158,12 @@ class Stream extends AbstractAdapter
136158
{
137159
return fopen(filename, mode);
138160
}
161+
162+
/**
163+
* @todo to be removed when we get traits
164+
*/
165+
protected function phpFwrite(var handle, string message)
166+
{
167+
return fwrite(handle, message);
168+
}
139169
}

phalcon/Logger/Adapter/Syslog.zep

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
namespace Phalcon\Logger\Adapter;
1212

1313
use LogicException;
14+
use Phalcon\Logger\Enum;
1415
use Phalcon\Logger\Item;
15-
use Phalcon\Logger\Logger;
1616

1717

1818
/**
@@ -141,15 +141,15 @@ class Syslog extends AbstractAdapter
141141
array levels;
142142

143143
let levels = [
144-
Logger::ALERT : LOG_ALERT,
145-
Logger::CRITICAL : LOG_CRIT,
146-
Logger::CUSTOM : LOG_ERR,
147-
Logger::DEBUG : LOG_DEBUG,
148-
Logger::EMERGENCY : LOG_EMERG,
149-
Logger::ERROR : LOG_ERR,
150-
Logger::INFO : LOG_INFO,
151-
Logger::NOTICE : LOG_NOTICE,
152-
Logger::WARNING : LOG_WARNING
144+
Enum::ALERT : LOG_ALERT,
145+
Enum::CRITICAL : LOG_CRIT,
146+
Enum::CUSTOM : LOG_ERR,
147+
Enum::DEBUG : LOG_DEBUG,
148+
Enum::EMERGENCY : LOG_EMERG,
149+
Enum::ERROR : LOG_ERR,
150+
Enum::INFO : LOG_INFO,
151+
Enum::NOTICE : LOG_NOTICE,
152+
Enum::WARNING : LOG_WARNING
153153
];
154154

155155
if !fetch result, levels[level] {

tests/unit/Logger/Logger/ConstructCest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ public function loggerConstructStreamWithJsonConstants(UnitTester $I)
9797
$I->openFile($fileName);
9898

9999
$expected = sprintf(
100-
'{"level":"DEBUG","message":"This is a message","timestamp":"%s"}' . PHP_EOL .
101-
'{"level":"ERROR","message":"This is an error","timestamp":"%s"}' . PHP_EOL .
102-
'{"level":"ERROR","message":"This is another error","timestamp":"%s"}',
100+
'{"level":"debug","message":"This is a message","timestamp":"%s"}' . PHP_EOL .
101+
'{"level":"error","message":"This is an error","timestamp":"%s"}' . PHP_EOL .
102+
'{"level":"error","message":"This is another error","timestamp":"%s"}',
103103
date('c', $time),
104104
date('c', $time),
105105
date('c', $time)

tests/unit/Logger/Logger/LevelsCest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use function file_get_contents;
2525
use function logsDir;
2626
use function preg_match;
27-
use function strtoupper;
27+
use function strtolower;
2828

2929
class LevelsCest
3030
{
@@ -65,15 +65,15 @@ public function loggerAlert(UnitTester $I, Example $example)
6565
$I->seeInThisFile($logString);
6666

6767
// Check if the level is in the log file
68-
$I->seeInThisFile('[' . strtoupper($level) . ']');
68+
$I->seeInThisFile('[' . strtolower($level) . ']');
6969

7070
// Check time content
7171
$content = file_get_contents($fileName);
7272

7373
// Get time part
7474
$matches = [];
7575
preg_match(
76-
'/\[(.*)\]\[' . strtoupper($level) . '\]/',
76+
'/\[(.*)\]\[' . strtolower($level) . '\]/',
7777
$content,
7878
$matches
7979
);

tests/unit/Logger/Logger/LogCest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
use function logsDir;
2323
use function sprintf;
24-
use function strtoupper;
24+
use function strtolower;
2525
use function uniqid;
2626

2727
class LogCest
@@ -80,7 +80,7 @@ public function loggerLog(UnitTester $I)
8080
foreach ($levels as $levelName) {
8181
$expected = sprintf(
8282
'[%s] Message %s',
83-
strtoupper($levelName),
83+
strtolower($levelName),
8484
$levelName
8585
);
8686

@@ -155,7 +155,7 @@ public function loggerLogLogLevel(UnitTester $I)
155155
foreach ($levelsYes as $levelName) {
156156
$expected = sprintf(
157157
'[%s] Message %s',
158-
strtoupper($levelName),
158+
strtolower($levelName),
159159
$levelName
160160
);
161161
$I->seeInThisFile($expected);
@@ -164,7 +164,7 @@ public function loggerLogLogLevel(UnitTester $I)
164164
foreach ($levelsNo as $levelName) {
165165
$expected = sprintf(
166166
'[%s] Message %s',
167-
strtoupper($levelName),
167+
strtolower($levelName),
168168
$levelName
169169
);
170170
$I->dontSeeInThisFile($expected);
@@ -212,7 +212,7 @@ public function loggerLogLogInterpolator(UnitTester $I)
212212
$I->openFile($fileName);
213213

214214
$expected = sprintf(
215-
'log message-[DEBUG]-%s:%s',
215+
'log message-[debug]-%s:%s',
216216
$context['server'],
217217
$context['user']
218218
);
@@ -262,7 +262,7 @@ public function loggerLogLogInterpolatorCustom(UnitTester $I)
262262
$I->openFile($fileName);
263263

264264
$expected = sprintf(
265-
'log message-[DEBUG]-%s:%s',
265+
'log message-[debug]-%s:%s',
266266
$context['server'],
267267
$context['user']
268268
);

0 commit comments

Comments
 (0)