2

magento2 uses monolog for logging.i'm able to log strings.but i'm not able to log objects and arrays.can you give anyone syntax for this.i'm getting following error.

Object of class 'some class' could not be converted to string.

$object_manager->get('Psr\Log\LoggerInterface')->info("some_text");
2

ORIGINAL ANSWER:

Try this: put print_r($array-or-object, true) as parameter to info.

UPDATE

Alternatively You can log extra data using the second param, ie (for category data)

$object_manager->get('Psr\Log\LoggerInterface')->info("category_data", array('category_data', $category->getData);

But the array will be converted to json which isn't the most "readable" format, so for a basic log I still using print_r for readability.

BTW: Json output it's perfect if you use "logstash" or another parser.

LOG 4 DUMMIES:

If you are trying to log an entire Quote Object which has a lot of objects inside (and many recursion too) you must either log an specific part or use xdebug/zend_debug to handle such "heavy" data, otherwise you'll probably exhaust the allocated memory for the request.

  • i have checked with category object and its not working.. – zed Blackbeard Mar 1 '16 at 6:45
  • This does not work as the object is too big for memory – kevando Mar 28 '16 at 19:43
  • 1
    @zedBlackbeard you can log simple objects that way, if you want to log some category data can use print_r($category->getData(),true); – MauroNigrele Mar 28 '16 at 20:32
  • @kevando The size of the object doesn't make the answer a bad one, you can log that way plain objects/STD or arrays without any problem, but if the object is too big you can't log it this way or any other. You just can't write a complete book in a single page, try to use your brain before your fingers. – MauroNigrele 1 hour ago – MauroNigrele Mar 28 '16 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.