-
Notifications
You must be signed in to change notification settings - Fork 15
Logging
The Daemon provides a simple and robust logging mechanism that includes 4 methods for logging various types of messages. And a LogTrait to allow other classes to use the daemon log without any special setup. All logging methods accept a variable list of arguments and are processed by sprintf. So all messages can use placeholders, like %s or %d or anything else sprintf supports.
-
log(string $msg, $args[]...)
Log an informational message.
The eventDaemonEvents::ON_LOGwill be triggered. If any listening handlers stop propagation on theLogEventthen no message will be logged. -
error(string|Exception $msg, $args[]...)
Log an error message. The first parameter$msgcan be astringorException. If an Exception is given then the full stack trace of the exception is appended to the message.
The eventDaemonEvents::ON_ERRORwill be triggered. If any listening handlers stop propagation on theErrorEventthen no message will be logged. Useslog()to do the actual output. -
fatalError(string|Exception $msg, $args[]...)
A special method that will log an error just likeerror()does, but will also trigger arestartof the daemon if enabled and certain criteria is met.
Useserror()to do the actual output. No matter what, the daemon willexit(1)when you call this method. So only call it for REAL fatal errors! -
debug([int $level = 1], string $msg, $args[]...)
debug($msg, $args[]...)
Log a debug message. This method has an extra parameter that allows you to set the level of the debug message. By default the level is 1. If you do not specify a level then the first parameter can be the $msg instead. If you do special the $level it has to be an integer (1) and not a string representing an integer ("1"). Useslog()to do the actual output.
The php-daemon library includes a LogTrait trait that allows any class to use and be able to easily use the above methods (except fatalError()) to log to the daemon. This trait also includes a special method setLogArguments($args[], $type = 'log') that allows your class to override how the final message is logged. By default the LogTrait will prepend the base class name in front of all messages.
For example:
class MyClass {
use Lifo\Daemon\LogTrait;
public function __construct() {
$this->debug("instance was created!");
}
}