public function testWriteContentWithPlainTextMessage()
 {
     $this->createHandler('myToken', 'channel1', 'Monolog', false);
     $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
     fseek($this->res, 0);
     $content = fread($this->res, 1024);
     $this->assertRegexp('/text=test1/', $content);
 }
 /**
  * @param ContainerInterface $container
  *
  * @return LoggerInterface
  *
  * @throws \Interop\Container\Exception\NotFoundException
  * @throws \Interop\Container\Exception\ContainerException
  * @throws \Exception
  * @throws \InvalidArgumentException
  * @throws \Monolog\Handler\MissingExtensionException
  */
 public function __invoke(ContainerInterface $container) : LoggerInterface
 {
     $config = $container->get('config');
     $logger = new Logger('app', [new StreamHandler('data/log/app.log', Logger::INFO)], [new PsrLogMessageProcessor()]);
     if (isset($config['monolog']['slack'])) {
         $slackHandler = new SlackHandler($config['monolog']['slack']['token'], $config['monolog']['slack']['channel'], $config['monolog']['slack']['name']);
         $slackHandler->setLevel(Logger::NOTICE);
         $logger->pushHandler($slackHandler);
     }
     return $logger;
 }
 private function createHandler($token = 'myToken', $channel = 'channel1', $username = '******', $useAttachment = true)
 {
     $constructorArgs = array($token, $channel, $username, $useAttachment, Logger::DEBUG, true);
     $this->res = fopen('php://memory', 'a');
     $this->handler = $this->getMock('\\Monolog\\Handler\\SlackHandler', array('fsockopen', 'streamSetTimeout', 'closeSocket'), $constructorArgs);
     $reflectionProperty = new \ReflectionProperty('\\Monolog\\Handler\\SocketHandler', 'connectionString');
     $reflectionProperty->setAccessible(true);
     $reflectionProperty->setValue($this->handler, 'localhost:1234');
     $this->handler->expects($this->any())->method('fsockopen')->will($this->returnValue($this->res));
     $this->handler->expects($this->any())->method('streamSetTimeout')->will($this->returnValue(true));
     $this->handler->expects($this->any())->method('closeSocket')->will($this->returnValue(true));
     $this->handler->setFormatter($this->getIdentityFormatter());
 }
 protected function registerCustomLogger()
 {
     $config = $this->app['config']->get('logifier.slack');
     if (!$config['enabled']) {
         return;
     }
     $monolog = Log::getMonolog();
     $handler = new SlackHandler($config['token'], $config['channel'], $config['username'], true, null, $config['warning_level'], true, false, true);
     $handler->pushProcessor(new WebProcessor());
     $handler->setFormatter(new HtmlFormatter());
     $user = \Auth::user();
     if ($user) {
         $monolog->addInfo('USER ID: ' . $user->id);
     }
     $monolog->pushHandler($handler);
 }
Example #5
0
File: Log.php Project: jawngee/Stem
 public static function initialize($config = null)
 {
     self::$logger = new Logger('stem');
     if ($config == null) {
         self::$logger->pushHandler(new ErrorLogHandler());
         return;
     }
     foreach ($config as $level => $handlers) {
         switch ($level) {
             case 'info':
                 $logLevel = Logger::INFO;
                 break;
             case 'notice':
                 $logLevel = Logger::NOTICE;
                 break;
             case 'warning':
                 $logLevel = Logger::WARNING;
                 break;
             case 'error':
                 $logLevel = Logger::ERROR;
                 break;
             case 'critical':
                 $logLevel = Logger::CRITICAL;
                 break;
             case 'alert':
                 $logLevel = Logger::ALERT;
                 break;
             case 'emergency':
                 $logLevel = Logger::EMERGENCY;
                 break;
             default:
                 $logLevel = Logger::DEBUG;
                 break;
         }
         foreach ($handlers as $handler => $handlerConfig) {
             $outputHandler = null;
             if ($handler == 'phperror') {
                 $outputHandler = new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, $logLevel);
             } else {
                 if ($handler == 'browser') {
                     $outputHandler = new BrowserConsoleHandler($logLevel);
                 } else {
                     if ($handler == 'syslog' && isset($handlerConfig['ident'])) {
                         $outputHandler = new SyslogHandler($handlerConfig['ident'], LOG_USER, $logLevel);
                     } else {
                         if ($handler == 'syslog_udp' && isset($handlerConfig['host'])) {
                             $port = isset($handlerConfig['port']) ? $handlerConfig['port'] : 514;
                             $outputHandler = new SyslogUdpHandler($handlerConfig['host'], $port, LOG_USER, $logLevel);
                         } else {
                             if ($handler == 'mail') {
                                 $to = isset($handlerConfig['to']) ? $handlerConfig['to'] : null;
                                 $subject = isset($handlerConfig['subject']) ? $handlerConfig['subject'] : 'Log';
                                 $from = isset($handlerConfig['from']) ? $handlerConfig['from'] : null;
                                 if ($to && $from) {
                                     $outputHandler = new NativeMailerHandler($to, $subject, $from, $logLevel);
                                 }
                             } else {
                                 if ($handler == 'slack') {
                                     $token = isset($handlerConfig['token']) ? $handlerConfig['to'] : null;
                                     $channel = isset($handlerConfig['channel']) ? $handlerConfig['channel'] : null;
                                     $username = isset($handlerConfig['username']) ? $handlerConfig['username'] : '******';
                                     $useAttachment = isset($handlerConfig['useAttachment']) ? $handlerConfig['useAttachment'] : true;
                                     $iconEmoji = isset($handlerConfig['iconEmoji']) ? $handlerConfig['iconEmoji'] : null;
                                     if ($token && $channel) {
                                         $outputHandler = new SlackHandler($token, $channel, $username, $useAttachment, $iconEmoji, $logLevel);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             if ($outputHandler) {
                 $formatter = null;
                 if (isset($handlerConfig['format']) && isset($handlerConfig['format']['output'])) {
                     $date = isset($handlerConfig['format']['date']) ? $handlerConfig['format']['date'] : null;
                     $allowInlineLineBreaks = isset($handlerConfig['format']['line-breaks']) ? $handlerConfig['format']['line-breaks'] : false;
                     $output = $handlerConfig['format']['output'];
                     $formatter = new LineFormatter($output, $date . $allowInlineLineBreaks);
                 }
                 if ($formatter) {
                     $outputHandler->setFormatter($formatter);
                 }
                 self::$logger->pushHandler($outputHandler);
             }
         }
     }
 }
 /**
  * Prepares content data
  *
  * @param  array $record
  *
  * @return array
  */
 protected function prepareContentData($record)
 {
     $record['message'] = '<' . $this->mentionTo . '>' . "\n" . $record['message'];
     return parent::prepareContentData($record);
 }