yep/workflow-logger

v1.3.0 2017-10-02 20:50 UTC

This package is auto-updated.

Last update: 2024-10-11 19:43:33 UTC


README

Build Status Scrutinizer Code Quality Scrutinizer Code Coverage Latest Stable Version Total Downloads License

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! :bowtie:

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) with key 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. 😙