MongoLog::setCallback
(PECL mongo >= 1.3.0)
MongoLog::setCallback — Sets a callback function to be invoked for events
Description
This function will set a callback function to be invoked for events in lieu of emitting of PHP notice.
Parameters
-
log_function
-
The callback function to be invoked on events. It should have the following prototype:
log_function ( int$module
, int$level
, string$message
)-
module
- One of the MongoLog module constants.
-
level
- One of the MongoLog level constants.
-
message
- The log message itself.
-
Examples
Example #1 MongoLog::setCallback() example
<?php
function module2string($module)
{
switch ($module) {
case MongoLog::RS: return "REPLSET";
case MongoLog::CON: return "CON";
case MongoLog::IO: return "IO";
case MongoLog::SERVER: return "SERVER";
case MongoLog::PARSE: return "PARSE";
default: return "UNKNOWN";
}
}
function level2string($level)
{
switch ($level) {
case MongoLog::WARNING: return "WARN";
case MongoLog::INFO: return "INFO";
case MongoLog::FINE: return "FINE";
default: return "UNKNOWN";
}
}
function callback($module, $level, $message)
{
echo date("Y-m-d H:i:s - ");
printf("%s (%s): %s\n", module2string($module), level2string($level), $message);
}
MongoLog::setLevel(MongoLog::ALL);
MongoLog::setModule(MongoLog::ALL);
// We specify the function name here, but any callable (e.g. anonymous function) will work
MongoLog::setCallback("callback");
new MongoClient();
?>
The above example will output something similar to:
2013-07-09 09:41:42 - PARSE (INFO): Parsing localhost:27017 2013-07-09 09:41:42 - PARSE (INFO): - Found node: localhost:27017 2013-07-09 09:41:42 - PARSE (INFO): - Connection type: STANDALONE 2013-07-09 09:41:42 - CON (INFO): mongo_get_read_write_connection: finding a STANDALONE connection 2013-07-09 09:41:42 - CON (INFO): connection_create: creating new connection for localhost:27017 2013-07-09 09:41:42 - CON (INFO): stream_connect: Not establishing SSL for localhost:27017 2013-07-09 09:41:42 - CON (INFO): get_server_flags: start 2013-07-09 09:41:42 - CON (FINE): send_packet: read from header: 36 2013-07-09 09:41:42 - CON (FINE): send_packet: data_size: 95 2013-07-09 09:41:42 - CON (FINE): get_server_flags: setting maxBsonObjectSize to 16777216 2013-07-09 09:41:42 - CON (FINE): get_server_flags: setting maxMessageSizeBytes to 48000000 2013-07-09 09:41:42 - CON (INFO): is_ping: pinging localhost:27017;-;.;1543 2013-07-09 09:41:42 - CON (FINE): send_packet: read from header: 36 2013-07-09 09:41:42 - CON (FINE): send_packet: data_size: 17 2013-07-09 09:41:42 - CON (INFO): is_ping: last pinged at 1373359302; time: 0ms 2013-07-09 09:41:42 - REPLSET (FINE): finding candidate servers 2013-07-09 09:41:42 - REPLSET (FINE): - all servers 2013-07-09 09:41:42 - REPLSET (FINE): filter_connections: adding connections: 2013-07-09 09:41:42 - REPLSET (FINE): - connection: type: STANDALONE, socket: 42, ping: 0, hash: localhost:27017;-;.;1543 2013-07-09 09:41:42 - REPLSET (FINE): filter_connections: done 2013-07-09 09:41:42 - REPLSET (FINE): limiting by seeded/discovered servers 2013-07-09 09:41:42 - REPLSET (FINE): - connection: type: STANDALONE, socket: 42, ping: 0, hash: localhost:27017;-;.;1543 2013-07-09 09:41:42 - REPLSET (FINE): limiting by seeded/discovered servers: done 2013-07-09 09:41:42 - REPLSET (FINE): limiting by credentials 2013-07-09 09:41:42 - REPLSET (FINE): - connection: type: STANDALONE, socket: 42, ping: 0, hash: localhost:27017;-;.;1543 2013-07-09 09:41:42 - REPLSET (FINE): limiting by credentials: done 2013-07-09 09:41:42 - REPLSET (FINE): sorting servers by priority and ping time 2013-07-09 09:41:42 - REPLSET (FINE): - connection: type: STANDALONE, socket: 42, ping: 0, hash: localhost:27017;-;.;1543 2013-07-09 09:41:42 - REPLSET (FINE): sorting servers: done 2013-07-09 09:41:42 - REPLSET (FINE): selecting near servers 2013-07-09 09:41:42 - REPLSET (FINE): selecting near servers: nearest is 0ms 2013-07-09 09:41:42 - REPLSET (FINE): - connection: type: STANDALONE, socket: 42, ping: 0, hash: localhost:27017;-;.;1543 2013-07-09 09:41:42 - REPLSET (FINE): selecting near server: done 2013-07-09 09:41:42 - REPLSET (INFO): pick server: random element 0 2013-07-09 09:41:42 - REPLSET (INFO): - connection: type: STANDALONE, socket: 42, ping: 0, hash: localhost:27017;-;.;1543
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-mongolog.setcallback.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.