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
-
- Require __dir__. '/vendor/autoload.php ';
- Use Monolog\logger;
- Use Monolog\handler\streamhandler;
- Use Monolog\handler\bufferhandler;
- Use Monolog\errorhandler;
- Use Monolog\processor\memoryusageprocessor;
- $logger = new Logger (' Error_logger ');
- $stream = new Streamhandler (__dir__. ') /error.log ', logger::error);
- $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.
- Errorhandler::register ($logger);
- 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
- Use Monolog\logger;
- Use Monolog\handler\abstractprocessinghandler;
- Class Pdohandler extends Abstractprocessinghandler
- {
- Private $initialized = false;
- Private $pdo;
- Private $statement;
- Public function __construct (PDO $pdo, $level = Logger::D ebug, $bubble = True)
- {
- $this->pdo = $pdo;
- Parent::__construct ($level, $bubble);
- }
- protected function write (array $record)
- {
- if (! $this->initialized) {
- $this->initialize ();
- }
- $this->statement->execute (Array (
- ' Channel ' = $record [' Channel '],
- ' Level ' = $record [' Level '],
- ' Message ' = $record [' Formatted '],
- ' Time ' = $record [' DateTime ']->format (' U '),
- ));
- }
- Private Function Initialize ()
- {
- $this->pdo->exec (
- ' CREATE TABLE IF not EXISTS monolog '
- .' (channel VARCHAR (255), level integer, Message longtext, time integer UNSIGNED) '
- );
- $this->statement = $this->pdo->prepare (
- ' INSERT into Monolog (channel, level, message, Time) VALUES (: Channel,: Level,: Message,: Time) '
- );
- $this->initialized = true;
- }