PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/en/3-4-error-and-logging.md

https://gitlab.com/lcp0578/blink
Markdown | 70 lines | 51 code | 19 blank | 0 comment | 0 complexity | 56c560ea0f0261a50139317babfa6d13 MD5 | raw file
  1. Error Handing and Logging
  2. =========================
  3. Logging
  4. -------
  5. Blink provides a PSR-3 compatible logging service that build upon Monolog logging library. Using this service, you can
  6. easily log various types of messages into different targets, such as files, databases and emails.
  7. In order to log your messages, you need configure the log service. In the following, this is the default configuration
  8. provided by Blink's seed project:
  9. ```php
  10. 'log' => [
  11. 'class' => 'blink\log\Logger',
  12. 'targets' => [
  13. 'file' => [
  14. 'class' => 'blink\log\StreamTarget',
  15. 'enabled' => true,
  16. 'stream' => 'php://stderr',
  17. 'level' => 'info',
  18. ]
  19. ],
  20. ],
  21. ```
  22. In this example, we configured a file logging target that will write all messages to `stderr` if the message level
  23. equals or less than *INFO*.
  24. Besides the configuration, it is very convenient to access the log service and write logs, this is the example:
  25. ```php
  26. // accessing the log service
  27. $log = app('log');
  28. // logging when system is unusable
  29. $log->emergency('my message');
  30. // logging when action must be taken immediately
  31. $log->alert('my message');
  32. // logging on critical conditions
  33. $log->critical('my message');
  34. // logging for runtime errors
  35. $log->error('my message');
  36. // logging for warnings
  37. $log->warning('my message');
  38. // logging for normal bug sighficant events
  39. $log->notice('my message');
  40. // logging for insteresting events
  41. $log->info('my message');
  42. // logging for detailed debug information
  43. $log->debug('my message');
  44. ```
  45. Error Handing
  46. -------------
  47. In Blink, all PHP errors are converted to `blink\core\ErrorException` exception automatically. By utilizing this feature,
  48. it is possible to using `try ... catch` block to catch PHP errors.
  49. Blink provides `errorHandler` service which implemented by `blink\core\ErrorHandler` class to handle exception and
  50. errors, `errorHandler` will report exceptions or errors to `log` service by default, it is possible implement your
  51. own errorHandler service to report errors in a different way such as Sentry.