yep / workflow-logger
Workflow logger
Installs: 5 328
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.6
- monolog/monolog: ^1.5
- psr/log: ^1.0
Requires (Dev)
- phpunit/phpunit: ^5
- symfony/var-dumper: ^3.2
- tracy/tracy: ^2.3
- yep/reflection: ^1.0
Suggests
- symfony/var-dumper: To use SymfonyVarDumper install symfony/var-dumper=^3.2
- tracy/tracy: To use TracyDumper install tracy/tracy=^2.3
README
Workflow logger
Packagist
Reflection is available on Packagist.org, just add the dependency to your composer.json.
{ "require" : { "yep/workflow-logger": "^1.2" } }
or run Composer command:
php composer.phar require yep/workflow-logger
What Yep/WorkflowLogger do?
It helps to log workflows! 😳
Try to imagine this situation ↓↓↓
<?php class SomeImportantManagerClass { public function doSomeImportantJob($importantParameter) { $foo = $this->doSomethingImportant($importantParameter); if($foo > 1) { $this->doSomeMagicButReallyImportantMagic(); } } ... } $someImportantManagerClass = new SomeImportantManagerClass(); $someImportantVariable = 1; $someImportantManagerClass->doSomeImportantJob($someImportantVariable);
Question: How do you know, that each method done exactly what you expect?
Answer: I don't know, but I can add Logger! 😊
Reaction: 👍
So we will add Monolog\Logger ↓↓↓
<?php class SomeImportantManagerClass { /** @var Monolog\Logger */ private $logger; public function __construct(Monolog\Logger $logger) { $this->logger = $logger; } public function doSomeImportantJob($importantParameter) { $this->logger->info('Im in!'); $foo = $this->doSomethingImportant($importantParameter); $this->logger->info('I just done something important!', ['foo' => $foo]); if($foo > 1) { $result = $this->doSomeMagicButReallyImportantMagic(); $this->logger->alert('Abracadabra #copperfield', ['result' => $result, 'foo' => $foo]); } else { $this->logger->error('No Abracadabra #sadCopperfield', ['importantParameter' => $importantParameter, 'foo' => $foo]); } } ... } $importantLogger = new Monolog\Logger('ImportantLogger'); $someImportantManagerClass = new SomeImportantManagerClass($importantLogger); $someImportantVariable = 1; $someImportantManagerClass->doSomeImportantJob($someImportantVariable);
Question: How many log items we will have?
Answer: 3! 😋
Reaction: Yes, Correct! 👍
Question: But what should we do if we want only one log record? 😏
Answer: Dunno... 😱
Reaction: Really? So, have a look below! 😎
We will "improve" our logging ↓↓↓
<?php class SomeImportantManagerClass { /** @var Monolog\Logger */ private $logger; public function __construct(Monolog\Logger $logger) { $this->logger = $logger; } public function doSomeImportantJob($importantParameter) { $logMessage = "Im in!\n"; $logContext = ['importantParameter' => $importantParameter]; $foo = $this->doSomethingImportant($importantParameter); $logMessage .= "I just done something important!\n"; $logContext['foo'] = $foo; if($foo > 1) { $result = $this->doSomeMagicButReallyImportantMagic(); $logMessage .= "Abracadabra #copperfield\n"; $logContext['result'] = $result; } else { $logMessage .= "No Abracadabra #sadCopperfield\n"; } $this->logger->info($logMessage, $logContext); } ... }
Question: Much better! What do you think?
Answer: But but but moooom, in this case I can log these messages only with one type and I don't know, for which one is the context data...
Reaction: Yop, you are right... 😅
However, you can use our WorkflowLogger!
Now the real magic with Yep\WorkflowLogger\Logger! 😎 ↓↓↓
<?php class SomeImportantManagerClass { /** @var Yep\WorkflowLogger\Logger */ private $logger; public function __construct(Yep\WorkflowLogger\Logger $logger) { $this->logger = $logger; } public function doSomeImportantJob($importantParameter) { $workflow = $this->logger->workflow(); $workflow->info('Im in!'); $foo = $this->doSomethingImportant($importantParameter); $workflow->info('I just done something important!', ['foo' => $foo]); if($foo > 1) { $result = $this->doSomeMagicButReallyImportantMagic(); $workflow->alert('Abracadabra #copperfield', ['result' => $result, 'foo' => $foo]); } else { $workflow->error('No Abracadabra #sadCopperfield', ['importantParameter' => $importantParameter, 'foo' => $foo]); } $workflow->finish('Finished one of many important workflows', ['nextStep' => 'improve!']); } } // $dumper = new Yep\WorkflowLogger\ContextDumper\PrintRDumper(); // $dumper = new Yep\WorkflowLogger\ContextDumper\TracyDumper(); $dumper = new Yep\WorkflowLogger\ContextDumper\SymfonyVarDumper(); $formatter = new Yep\WorkflowLogger\Formatter\StandardFormatter($dumper, 2); $importantLogger = new Yep\WorkflowLogger\Logger('ImportantLogger', $formatter); $someImportantManagerClass = new SomeImportantManagerClass($importantLogger); $someImportantVariable = 1; $someImportantManagerClass->doSomeImportantJob($someImportantVariable);
Log result
[2017-10-02 01:52:20] ImportantLogger.WORKFLOW: Finished one of many important workflows
Workflow:
[2017-10-02 22:43:46.624555] INFO: Im in!
[2017-10-02 22:43:46.624578] INFO: I just done something important! :sunglasses:
Context:
[
"foo" => 2
]
[2017-10-02 22:43:46.624591] ALERT: Abracadabra #copperfield
Context:
[
"result" => "Alohomora"
"foo" => 2
]
{"nextStep":"improve!"} []
Reaction: 😳 😱
Hints
- You can use the same workflow more times until is locked during finish or manually by lock method.
If you want to get the same workflow just call$logger->workflow($key)
withkey
as the first argument.
Every time you will call that method, logger will give you the same workflow until is locked. 😎 - If you want to use
\Monolog\Formatter\ChromePHPFormatter
,\Monolog\Formatter\GelfMessageFormatter
,\Monolog\Formatter\WildfireFormatter
or similar with freezed log Levels, you have to use\Yep\WorkflowLogger\MonologFormatterAdapter
. - Do you know, that you can indent the workflow message? Yep, you really can. Just use second argument of
Yep\WorkflowLogger\Formatter\StandardFormatter($dumper, $indent)
. 😉
That's all. I hope you like it. 😙