Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Timestamp log filter #6058

Closed

Conversation

nikolaposa
Copy link

Log filter which provides ability to filter log events based on the time when they were triggered.

It can be configured by specifying idate()-compliant format character and desired value or a DateTime instance for direct comparison with the log event's timestamp. Comparison operator must also be supplied in either cases.

Example usage:

use Zend\Log\Logger;
use Zend\Log\Writer\Stream as StreamWriter;
use Zend\Log\Filter\Timestamp as TimestampFilter;

$logger = new Logger();

$writer = new StreamWriter('/path/to/first/logfile');
//Log only events between 10 and 13 hours
$writer->addFilter(new TimestampFilter(10, 'H', '>='));
$writer->addFilter(new TimestampFilter(12, 'H', '<='));
$logger->addWriter($writer);


if ($this->value instanceof DateTime) {
return version_compare((string) $timestamp, (string) $this->value->getTimestamp(), $this->operator);
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need else, just return directly because already return early in previous if

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do understand your point, but I prefer to use explicit else in these kind of situations, in order to "highlight" that two different strategies/directions. On the other hand, in case of raw boolean return statements, I use that "no else" approach, with that last, fallback return statement at the very end of a method declaration.

But if you insist, I'll alter this...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already did a lot of work at the pass for removing else because already return early, see #3286

$this->dateFormatChar = $dateFormatChar;
}

$this->operator = ($operator) ?: '<=';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the operator be validated?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I frankly haven't seen any validation on $operator in Zend\Log\Filter\Priority, which is also based on the version_compare() function. Do you think that I should at least add some type check (is string)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikolaposa what are the allowed operator here? The PHP docs mention The possible operators are: <, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne respectively..

The fact that the priority filter doesn't have strict checks doesn't mean that we can't apply them here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, no problem, I'll put some validation there.

@Ocramius Ocramius added this to the 2.4.0 milestone Apr 2, 2014
$value = iterator_to_array($value);
}
if (is_array($value)) {
extract($value, EXTR_IF_EXISTS);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not use extract; test for values in the array instead, and set the locally declared variables based on what you find in the array.

…Testing string versions of comparison operators.
@nikolaposa
Copy link
Author

Why is this build failing? I suppose that the problem is in some other commit, because test failure points to "ZendTest\Cache\Storage\Adapter\FilesystemTest::testClearExpired". What should be done in such situations, in order to make my commit "green"? Should I rebase with master and then force push?

@Ocramius
Copy link
Member

Ocramius commented May 2, 2014

@nikolaposa it's a race condition that happens quite often in the builds, sadly :(

@nikolaposa
Copy link
Author

Do I need to take any action on this issue?

btw Can this situation be prevented somehow? Maybe with more frequent master rebasing?

@Ocramius
Copy link
Member

Ocramius commented May 2, 2014

@nikolaposa the bug has not yet been solved, so rebasing won't help anyway. No action required for that failure

@Ocramius Ocramius self-assigned this May 2, 2014
@Ocramius
Copy link
Member

Ocramius commented May 2, 2014

@nikolaposa one thing that is needed here is documentation for the new feature - think you can prepare a PR for that? I've scheduled merging for this once that's done.

@nikolaposa
Copy link
Author

Should I create/submit that pull request to the ZF2 docs project (https://github.com/zendframework/zf2-documentation) or in some other way?

@Ocramius
Copy link
Member

Ocramius commented May 2, 2014

@nikolaposa that is the correct way :-)

Ocramius added a commit to zendframework/zf2-documentation that referenced this pull request May 2, 2014
freax pushed a commit to freax/zf2 that referenced this pull request Nov 27, 2014
freax pushed a commit to freax/zf2 that referenced this pull request Nov 27, 2014
freax pushed a commit to freax/zf2 that referenced this pull request Nov 27, 2014
freax pushed a commit to freax/zf2 that referenced this pull request Nov 27, 2014
freax pushed a commit to freax/zf2 that referenced this pull request Nov 27, 2014
freax pushed a commit to freax/zf2 that referenced this pull request Nov 27, 2014
freax pushed a commit to freax/zf2 that referenced this pull request Nov 27, 2014
freax pushed a commit to freax/zf2 that referenced this pull request Nov 27, 2014
freax pushed a commit to freax/zf2 that referenced this pull request Nov 27, 2014
freax pushed a commit to freax/zf2 that referenced this pull request Nov 27, 2014
freax pushed a commit to freax/zf2 that referenced this pull request Nov 27, 2014
freax pushed a commit to freax/zf2 that referenced this pull request Nov 27, 2014
freax pushed a commit to freax/zf2 that referenced this pull request Nov 27, 2014
freax pushed a commit to freax/zf2 that referenced this pull request Nov 27, 2014
@nikolaposa nikolaposa mentioned this pull request Jan 1, 2015
gianarb pushed a commit to zendframework/zend-log that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-log that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-log that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-log that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-log that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-log that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-log that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-log that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-log that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-log that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-log that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-log that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-log that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-log that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-log that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants