Monolog follows the PSR3 interface specification and can easily be replaced with other log class libraries that follow the same specification. Monolog has a good extensibility, through the handler, formatter and processor these interfaces, can be a variety of Monolog class library extension and customization.
Basic usage
Monolog can be installed via GitHub or composer, and the following is the latest version to install using composer:
Composer require Monolog/monolog
What if you don't know what composer is? Please click here.
PHP version is required to be more than 5.3.
<?php
Use Monolog\logger;
Use Monolog\handler\streamhandler;
Create Log Channel
$log = new Logger (' name ');
$log->pushhandler (New Streamhandler (' Path/to/your.log ', logger::warning));
adding log records
$log->addwarning (' Foo ');
$log->adderror (' Bar ');
Core Concepts
Each logger instance contains a channel name (channel) and a stack of handler. When you add a record, the record is processed sequentially through the handler stack. Each handler can also decide whether to pass the record to the next handler in the next stack.
Through handler, we can implement some complex log operations. For example, if we put the streamhandler on the bottom of the stack, all the log records will eventually be written to the hard drive file. At the same time we put the mailhandler on top of the stack and send the error log through email by setting the log level. Handler has a $bubble attribute that defines whether handler intercepts records and does not let it flow to the next handler. So if we set the Mailhandler $bubble parameter to False, the log will be sent through the Mailhandler without the Streamhandler written to the hard drive when the error log occurs.
Logger can create multiple, each of which can define its own channel name and handler stack. Handler can be shared in multiple logger. The channel name will be reflected in the log so that we can view and filter the log records.
If you do not specify a log format (Formatter), Handler uses the default Formatter.
The level of the log is not customizable and currently uses the 8 levels defined in RFC 5424: Debug, info, notice, warning, error, critical, alert, and emergency. If you have additional requirements for logging, you can add content to the log record by Processo.
Log level
Debug (100): detailed debug information.
INFO (200): Critical events.
NOTICE (250): ordinary but important events.
WARNING (300): A non-error exception occurred.
Error (400): Run-time error, but does not need to be processed immediately.
Critica (500): Critical error.
Emergency (600): The system is not available.
Usage detailed
Multiple handler
<?php
Use Monolog\logger;
Use Monolog\handler\streamhandler;
Use Monolog\handler\firephphandler;
Create a Logger instance
$logger = new Logger (' My_logger ');
Add Handler
$logger->pushhandler (The new Streamhandler (__dir__). /my_app.log ', Logger::D ebug));
$logger->pushhandler (New Firephphandler ());
Start using
$logger->addinfo (' My logger are now ready ');
The first step is to create a logger instance in which the channel name is passed, which can be used to differentiate multiple logger instances.
The instance itself does not know how to process logging, which is handled through handler. Handler can be set up multiple, for example, the above example sets two handler, which can be handled in two different ways for logging.
It should be noted that since handler is stored in a stack, the handler added later is on the top of the stack and will be invoked first.
Add extra data
Monolog There are two ways to add additional information to the log.
The first method is to use the context, using the $context parameter, to pass in an array:
<?php
$logger->addinfo (' Adding a new user ', Array (' username ' => ' seldaek '));
The second method is to use processor. Processor can be any callable method that takes a log record as a parameter and then, after processing, modifies the extra part to return.
<?php
$logger->pushprocessor (function ($record) {
$record [' Extra '] [' dummy '] = ' Hello world! ';
return $record;
});
Processor is not necessarily bound to a logger instance, or it can be bound to a specific handler. Binds using the Pushprocessor method of the handler instance.
Use of the channel
The use of channel names can be used to classify logs, which is useful in large applications. With the channel name, you can easily brush the log records.
For example, we want to record the logs of different modules in the same log file, we can bind the same handler to different logger instances, and these instances use different channel names:
<?php
Use Monolog\logger;
Use Monolog\handler\streamhandler;
Use Monolog\handler\firephphandler;
Create Handler
$stream = new Streamhandler (__dir__. ' /my_app.log ', Logger::D ebug);
$firephp = new Firephphandler ();
Create the main logger of the application
$logger = new Logger (' My_logger ');
$logger->pushhandler ($stream);
$logger->pushhandler ($firephp);
Create a security-related logger with different channel names
$securityLogger = new Logger (' security ');
$securityLogger->pushhandler ($stream);
$securityLogger->pushhandler ($firephp);
Handler
Monolog has a lot of practical handler built into it, and they cover a wide range of usage scenarios, and here are some of the things you use:
Streamhandler: Writes the record into the PHP stream, mainly uses in the log file.
Sysloghandler: Write the records into Syslog.
Errorloghandler: Write the record in the PHP error log.
Nativemailerhandler: Use the PHP mail () function to send log records.
Sockethandler: Write log via socket.
<?php
Use Monolog\logger;
Use Monolog\handler\sockethandler;
Create the Logger
$logger = new Logger (' My_logger ');
Create the handler
$handler = new Sockethandler (' Unix:///var/log/httpd_app_log.socket ');
$handler->setpersistent (TRUE);
Now add the handler
$logger->pushhandler ($handler, logger::D ebug);
can now use your logger
$logger->addinfo (' My logger are now ready ');
Amqphandler: Write the records into a service compatible with the AMQP protocol.
Browserconsolehandler: Write log records to the console of the browser. Because you are using the browser's console object, you need to see if the browser supports it.
Redishandler: Write the records into Redis.
Mongodbhandler: Write the records into MONGO.
Elasticsearchhandler: Write the records to the Elasticsearch service.
Bufferhandler: Allows us to cache log records for a one-time processing.
Formatter
Similarly, here are a few formatter:
Lineformatter: Format the log record into a line of strings.
Htmlformatter: Format the log into an HTML form, mainly for mail.
Jsonformatter: Encodes the log records into JSON format.
Logstashformatter: Format the log record into the Logstash event JSON format.
Elasticaformatter: Format the log records into the data format used by Elasticsearch.
Processor
As mentioned earlier, processor can add additional information to the log records, Monolog also provides some useful processor:
Introspectionprocessor: Increases the file name and class name of the current script.
Webprocessor: Increases the URI, request method, and access IP information for the current request.
Memoryusageprocessor: Increases current memory usage information.
Memorypeakusageprocessor: Increases the information when memory usage peaks.