public function testSetAndEnableRequiredMessage() { // Remove all the appenders $this->logger->removeAppender('console'); $this->logger->removeAppender('splunk'); $this->logger->removeAppender('null'); // Create a new one $appender = new Console(); // Remove the console stream and replace it with a memory stream. $streamProp = new ReflectionProperty($appender, 'fp'); $memStream = \fopen('php://memory', 'rw'); $streamProp->setAccessible(true); $streamProp->setValue($appender, $memStream); // now add the new Console, with memory stream $this->logger->addAppender($appender); $this->logger->setAndEnableRequiredMessage('this is a required message'); // log something $expected = 'Test Log Message' . PHP_EOL; $this->logger->info($expected, []); $expected = 'this is a required message Test Log Message'; // see what happened! \fseek($memStream, 0); $actual = \stream_get_contents($memStream); $this->assertEquals(trim(preg_replace('/\\s+/', ' ', $expected)), trim(preg_replace('/\\s+/', ' ', $actual))); $pos = ftell($memStream); $this->logger->setAndEnableRequiredMessage('this is a required {MESSAGE}', true); $this->logger->setAndEnableRequiredContext(['some' => 'thing']); // log something $expected = 'Test Log Message' . PHP_EOL; $this->logger->info($expected, []); $expected = 'this is a required Test Log Message'; // see what happened! \fseek($memStream, $pos); $actual = \stream_get_contents($memStream); $this->assertEquals(trim(preg_replace('/\\s+/', ' ', $expected)), trim(preg_replace('/\\s+/', ' ', $actual))); }
#!/usr/bin/env php <?php use Piton\Logger; error_reporting(E_ALL); ini_set('display_errors', true); date_default_timezone_set(@date_default_timezone_get()); set_time_limit(0); define('APPLICATION_ENV', 'testing'); require_once __DIR__ . '/../vendor/autoload.php'; $logger = Logger::defaultConsoleFactory(); print "First, lets just see what we get by default.\n"; runLogMessages('Default Log Messages'); print "\nThat is great, but I would really like to see the level in each message!\n"; runLogMessages('{LOGLEVEL} Messages with Log Levels!'); print "\nOk, but I don't see debug and trace, I really want to see all the messages.\n"; $logger->setLevel('all'); runLogMessages('{LOGLEVEL} Then just use the built in LOGLEVEL tag!'); $logger->setLevel('fatal'); print "\nFine, yer a clever fellow. But, where are the timestamps?\n"; runLogMessages('{TIMESTAMP} {LOGLEVEL} Here they are, just use the built in TIMESTAMP tag!'); print "\nSo, everytime I send a message, I have to put this in it?\n"; $logger->setRequiredMessage('{TIMESTAMP} {LOGLEVEL}'); $logger->enableRequiredMessage(); runLogMessages('Set and enable the required message, it will prepend to any message you send. Then, just send your message.'); print "\nWhat if I want the loglevel and timestamp after the message tough guy?\n"; $logger->contextualizeMessage = TRUE; $logger->setRequiredMessage('{MESSAGE} {LOGLEVEL} {TIMESTAMP}'); runLogMessages('Oh, you mean like this?'); print "\nI have to make three different calls in order to set, enable the required message as well as contextualize the dynamic message?\n"; $logger->setAndEnableRequiredMessage('{TIMESTAMP} {LOGLEVEL} {MESSAGE} ', TRUE); runLogMessages('Nope, just do this.');