Skip to content

Commit

Permalink
Improve logging a little
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed Dec 15, 2018
1 parent f6d69ba commit ce84c39
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -12,7 +12,8 @@
"license": "MIT",
"require": {
"php": "^7.1",
"psr/log": "^1.0"
"psr/log": "^1.0",
"ext-json": "*"
},
"require-dev": {
"behat/behat": "^3.4",
Expand Down
16 changes: 15 additions & 1 deletion src/OrientatedItem.php
Expand Up @@ -8,14 +8,15 @@

namespace DVDoug\BoxPacker;

use JsonSerializable;
use function min;

/**
* An item to be packed.
*
* @author Doug Wright
*/
class OrientatedItem
class OrientatedItem implements JsonSerializable
{
/**
* @var Item
Expand Down Expand Up @@ -136,4 +137,17 @@ public function isStable(): bool
{
return $this->getTippingPoint() > 0.261;
}

/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return [
'item' => $this->item,
'width' => $this->width,
'length' => $this->length,
'depth' => $this->depth,
];
}
}
6 changes: 3 additions & 3 deletions src/Packer.php
Expand Up @@ -68,7 +68,7 @@ public function addItem(Item $item, int $qty = 1): void
for ($i = 0; $i < $qty; ++$i) {
$this->items->insert($item);
}
$this->logger->log(LogLevel::INFO, "added {$qty} x {$item->getDescription()}");
$this->logger->log(LogLevel::INFO, "added {$qty} x {$item->getDescription()}", ['item' => $item]);
}

/**
Expand Down Expand Up @@ -96,7 +96,7 @@ public function setItems(iterable $items): void
public function addBox(Box $box): void
{
$this->boxes->insert($box);
$this->logger->log(LogLevel::INFO, "added box {$box->getReference()}");
$this->logger->log(LogLevel::INFO, "added box {$box->getReference()}", ['box' => $box]);
}

/**
Expand Down Expand Up @@ -145,7 +145,7 @@ public function pack(): PackedBoxList
$packedBoxes = $redistributor->redistributeWeight($packedBoxes);
}

$this->logger->log(LogLevel::INFO, "packing completed, {$packedBoxes->count()} boxes");
$this->logger->log(LogLevel::INFO, "[PACKING COMPLETED], {$packedBoxes->count()} boxes");

return $packedBoxes;
}
Expand Down
2 changes: 1 addition & 1 deletion src/VolumePacker.php
Expand Up @@ -104,7 +104,7 @@ public function __construct(Box $box, ItemList $items)
*/
public function pack(): PackedBox
{
$this->logger->debug("[EVALUATING BOX] {$this->box->getReference()}");
$this->logger->debug("[EVALUATING BOX] {$this->box->getReference()}", ['box' => $this->box]);

while (count($this->items) > 0) {
$layerStartDepth = $this->getCurrentPackedDepth();
Expand Down
18 changes: 17 additions & 1 deletion tests/Test/TestBox.php
Expand Up @@ -9,8 +9,9 @@
namespace DVDoug\BoxPacker\Test;

use DVDoug\BoxPacker\Box;
use JsonSerializable;

class TestBox implements Box
class TestBox implements Box, JsonSerializable
{
/**
* @var string
Expand Down Expand Up @@ -163,4 +164,19 @@ public function getMaxWeight(): int
{
return $this->maxWeight;
}

/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return [
'reference' => $this->reference,
'innerWidth' => $this->innerWidth,
'innerLength' => $this->innerLength,
'innerDepth' => $this->innerDepth,
'emptyWeight' => $this->emptyWeight,
'maxWeight' => $this->maxWeight,
];
}
}
18 changes: 17 additions & 1 deletion tests/Test/TestItem.php
Expand Up @@ -9,9 +9,10 @@
namespace DVDoug\BoxPacker\Test;

use DVDoug\BoxPacker\Item;
use JsonSerializable;
use stdClass;

class TestItem implements Item
class TestItem implements Item, JsonSerializable
{
/**
* @var string
Expand Down Expand Up @@ -136,4 +137,19 @@ public function getKeepFlat(): bool
{
return $this->keepFlat;
}

/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return [
'description' => $this->description,
'width' => $this->width,
'length' => $this->length,
'depth' => $this->depth,
'weight' => $this->weight,
'keepFlat' => $this->keepFlat,
];
}
}

0 comments on commit ce84c39

Please sign in to comment.