Awesome PHP monolog

Source: Internet
Author: User
Tags php framework
Monolog is a relatively full and easily extensible logging component under PHP. At present, including Symfony, Laravel, cakephp and many other well-known PHP framework are built-in monolog. Require Monolog/monolog ' ~1.7 ' can be installed via GitHub clone Https://github.com/Seldaek/monolog.git or composer. The Monolog code results are as follows:

errorhandler.php (Set program error hander, exception hander to Mogolog takeover)
formatter/(built-in log display format)
handler/(various log processing classes, such as writing files, sending emails, writing sockets, writing queues, etc.)
logger.php (log processing interface)
processor/(built-in processing log class)
registry.php--


An example with an online error log can replace the default error log processing method

[PHP] view plain copy

    1. Require __dir__. '/vendor/autoload.php ';
    2. Use Monolog\logger;
    3. Use Monolog\handler\streamhandler;
    4. Use Monolog\handler\bufferhandler;
    5. Use Monolog\errorhandler;
    6. Use Monolog\processor\memoryusageprocessor;
    7. $logger = new Logger (' Error_logger ');
    8. $stream = new Streamhandler (__dir__. ') /error.log ', logger::error);
    9. $logger->pushhandler (New Bufferhandler ($stream, logger::D Ebug, True, true));// Use Bufferhandler to set the number of logs in the same request up to 10 and then write the file again.
    10. Errorhandler::register ($logger);
    11. Code ...


Monolog is written entirely in terms of object-oriented thinking, it has a psr-3 prs-4 rule that conforms to the fig, so it is very convenient to expand, and here is an example of a log-in database written on the author's document

[PHP] view plain copy

  1. Use Monolog\logger;
  2. Use Monolog\handler\abstractprocessinghandler;
  3. Class Pdohandler extends Abstractprocessinghandler
  4. {
  5. Private $initialized = false;
  6. Private $pdo;
  7. Private $statement;
  8. Public function __construct (PDO $pdo, $level = Logger::D ebug, $bubble = True)
  9. {
  10. $this->pdo = $pdo;
  11. Parent::__construct ($level, $bubble);
  12. }
  13. protected function write (array $record)
  14. {
  15. if (! $this->initialized) {
  16. $this->initialize ();
  17. }
  18. $this->statement->execute (Array (
  19. ' Channel ' = $record [' Channel '],
  20. ' Level ' = $record [' Level '],
  21. ' Message ' = $record [' Formatted '],
  22. ' Time ' = $record [' DateTime ']->format (' U '),
  23. ));
  24. }
  25. Private Function Initialize ()
  26. {
  27. $this->pdo->exec (
  28. ' CREATE TABLE IF not EXISTS monolog '
  29. .' (channel VARCHAR (255), level integer, Message longtext, time integer UNSIGNED) '
  30. );
  31. $this->statement = $this->pdo->prepare (
  32. ' INSERT into Monolog (channel, level, message, Time) VALUES (: Channel,: Level,: Message,: Time) '
  33. );
  34. $this->initialized = true;
  35. }
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.