/** * retrieves or creates a new logger by its name. A logger is added to the * hierarchy and its parent is assigned during the retrieval. * WARNING - to accomodate the use of namespaces on loggers, the parent logger has to be added first. * @param string $name the name of the logger. * @return Logger the new or existing Logger instance. */ public function getLogger($name, $config) { if (!isset($this->root)) { $this->root = new Root(); $this->setThreshold(Level::all()); } $this->root->init(); if (!isset($this->loggers[$name])) { $logger = new Logger($name); $entities = explode('.', $name); $first = array_shift($entities); $parent = $first != $name && isset($this->loggers[$first]) ? $this->loggers[$first] : $this->root; $logger->setParent($parent); if (count($entities) > 0) { foreach ($entities as $e) { $p = $first . '.' . $e; if (isset($this->loggers[$p]) && $p != $name) { $logger->setParent($this->loggers[$p]); } $first .= '.' . $e; } } $this->loggers[$name] = $logger; $this->setInitialConfiguration($this->loggers[$name], $config); } return $this->loggers[$name]; }
/** * @see \PHPLog\FilterAbstract::decide() */ public function decide(Event $event) { //if level max is not a Level. if (!$this->levelMax instanceof Level) { return FilterAbstract::NEUTRAL; } //if level min is not a level. if (!$this->levelMin instanceof Level) { return FilterAbstract::NEUTRAL; } //if level min is equal to all or is greater than max. if ($this->levelMin->equals(Level::all()) || $this->levelMin->isGreaterOrEqualTo($this->levelMax)) { return FilterAbstract::NEUTRAL; } //if max if equal to off. if ($this->levelMax->equals(Level::off())) { return FilterAbstract::NEUTRAL; } //check that the event level is between the range. $lmi = $this->levelMin->getIntLevel(); $lma = $this->levelMax->getIntLevel(); $e = $event->getLevel()->getIntLevel(); if ($e >= $lmi && $e <= $lma) { //in the range. return $this->acceptOnMatch ? FilterAbstract::ACCEPT : FilterAbstract::DENY; } return FilterAbstract::NEUTRAL; }
/** * Constructor - initializes this logger with a level and a * default EchoWriter writer (which means all logs are printed on screen) * @param Level $level [optional] the level in which to set the root logger. * defaulted to ALL. */ public function __construct(Level $level = null) { parent::__construct('root'); if ($level == null) { $level = Level::all(); } $this->setLevel($level); }
/** * @override * Constructor - Initailizes the Mail transport and the mailer class and checks the validity of the * emails in config. * @param array $config the configuration for this writer. */ public function init(Configuration $config) { $transport = $config->transport; if (!$transport instanceof Configuration || count($transport) == 0) { throw new \Exception('no transport config provided'); } //check we have a transport type. if (!isset($transport->type) || !in_array($transport->type, array(Mail::TRANSPORT_TYPE_MAIL, Mail::TRANSPORT_TYPE_SENDMAIL, Mail::TRANSPORT_TYPE_SMTP))) { throw new \Exception('no valid transport type defined'); } //get the transport parameters. $params = clone $transport; unset($param->type); $type = $tranport->type; //check we have enough overall args. if (count($params) == 0 && $type != Mail::TRANSPORT_TYPE_MAIL) { throw new \Exception('not enough provided arguments for transport type: ' . $type); } //check we have a username, password and host if the type is SMTP if ($type == Mail::TRANSPORT_TYPE_SMTP && (!array_key_exists('username', $params) || !array_key_exists('password', $params) || !array_key_exists('host', $params))) { throw new \Exception('smtp needs a username, password and host'); } //determine that the host/command is set dependant on the type. if ($type == Mail::TRANSPORT_TYPE_SENDMAIL && !array_key_exists('command', $params)) { throw new \Exception('sendmail needs a command to execute.'); } //determine if an optional port is set for SMTP. if ($type == Mail::TRANSPORT_TYPE_SMTP && (!array_key_exists('port', $params) || !is_numeric($params['port']))) { $params['port'] = 25; } //determine if an optional security is set for SMTP. if ($type == Mail::TRANSPORT_TYPE_SMTP && (!array_key_exists('ssl', $params) || !is_bool($params['ssl']))) { $bool = isset($params['ssl']) ? filter_var($params['ssl'], FILTER_VALIDATE_BOOLEAN) : false; $params['ssl'] = $bool; } $ssl = $params['ssl'] ? 'ssl' : null; //initialize the transport. $className = "\\Swift_{$type}Transport"; $transport = $type == Mail::TRANSPORT_TYPE_SENDMAIL ? $className::newInstance($params['command']) : ($type == Mail::TRANSPORT_TYPE_SMTP ? $className::newInstance($params['host'], $params['port'], $ssl) : $className::newInstance()); //if transport is SMTP, set the username and password. if ($type == Mail::TRANSPORT_TYPE_SMTP) { $transport->setUsername($params['username'])->setPassword($params['password']); } //initialize the mailer. $this->transport = $transport; $this->mailer = \Swift_Mailer::newInstance($transport); //check the to and from emails for validity. if (!isset($config['to']) || !isset($config['from'])) { throw new \Exception('to or from email is not set.'); } foreach (array($config['to'], $config['from']) as $email) { if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new \Exception('could not validate to and from emails.'); } } $this->to = $config['to']; $this->from = $config['from']; //set the default subject if one is not set. if (!array_key_exists('subject', $config) || strlen($config['subject']) == 0) { $config['subject'] = 'PHPLog Mail Writer'; } $this->subject = $config['subject']; //finally see if the users set any custom threshold for just this writer. $this->threshold = $config->threshold instanceof Level ? $config['threshold'] : Level::all(); //set the layout. $this->getLayoutConfig()->set('pattern', $this->pattern, true); $this->setLayout(new Pattern()); }
/** * Constructor - sets the name and the default logging level. * @param string $name the name of this logger. */ public function __construct($name) { $this->name = $name; //default to all logs. $this->setLevel(Level::all()); }