Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Dmitriy Lanets
Added: Aug 18, 2018 2:15 PM
Views: 3
Tags: php
логирование в файл
  1. <?php
  2. // Before: composer require monolog/monolog
  3. // composer autoloader
  4. require_once 'vendor/autoload.php';
  5. // Shortcuts for simpler usage
  6. use \Monolog\Logger;
  7. use \Monolog\Formatter\LineFormatter;
  8. use \Monolog\Handler\StreamHandler;
  9. // Common logger
  10. $log = new Logger('files');
  11. // Line formatter without empty brackets in the end
  12. $formatter = new LineFormatter(null, null, false, true);
  13. // Debug level handler
  14. $debugHandler = new StreamHandler('debug.log', Logger::DEBUG);
  15. $debugHandler->setFormatter($formatter);
  16. // Error level handler
  17. $errorHandler = new StreamHandler('error.log', Logger::ERROR);
  18. $errorHandler->setFormatter($formatter);
  19. // This will have both DEBUG and ERROR messages
  20. $log->pushHandler($debugHandler);
  21. // This will have only ERROR messages
  22. $log->pushHandler($errorHandler);
  23. // The actual logging
  24. $log->debug('I am debug');
  25. $log->error('I am error', array('productId' => 123));