Esempio n. 1
0
 /**
  * Add a syslog writer
  * 
  * @param array $params
  * @param object $formatter
  * @return void 
  */
 public function addSyslogWriter($params = array(), Zend_Log_Formatter_Interface $formatter = null)
 {
     $writer = new Zend_Log_Writer_Syslog($params);
     if ($formatter) {
         $writer->setFormatter($formatter);
     }
     $this->addWriter($writer);
 }
Esempio n. 2
0
 /**
  * Defined by Zend_Application_Resource_Resource
  *
  * @return App_Log
  */
 public function init()
 {
     $bootstrap = $this->getBootstrap();
     $config = $bootstrap->bootstrap('config')->getResource('config');
     $attachmentUploader = new Keboola\Log\DebugLogUploader($config->attachmentUploader);
     $log = new Keboola\Log\Log($attachmentUploader);
     $log->setEventItem('app', $config->app->name);
     $log->setEventItem('pid', getmypid());
     if (php_sapi_name() == 'cli') {
         if (!empty($_SERVER['argv'])) {
             $log->setEventItem('cliCommand', implode(' ', $_SERVER['argv']));
         }
     }
     $options = $this->getOptions();
     $syslogOptions = isset($options['syslog']) ? $options['syslog'] : array();
     $sysLogWriter = new \Zend_Log_Writer_Syslog($syslogOptions);
     $sysLogWriter->setFormatter(new Keboola\Log\Formatter\Json());
     $log->addWriter($sysLogWriter);
     \Zend_Registry::set('log', $log);
     return $log;
 }
Esempio n. 3
0
 /**
  * make new logger, configuration is taken from system_config, section $instanceName
  * @param string $instanceName
  * @param string $application
  */
 protected function __construct($instanceName, $application = null)
 {
     if ($instanceName === null) {
         throw new Exception(__METHOD__ . ': expected an instance name, got none');
     }
     $config = Config::getInstance(SYSTEM_CONFIG);
     $log_config = $config->{$instanceName};
     $this->log = new Zend_Log();
     if (isset($log_config->file) && intval($log_config->file->enabled) !== 0) {
         $file_logger = new Zend_Log_Writer_Stream($log_config->file->name);
         /**
         *
             		$format = Zend_Log_Formatter_Simple::DEFAULT_FORMAT;
             		$formatter = new Zend_Log_Formatter_Simple($format);
             		$file_logger->setFormatter($formatter);
         */
         if (isset($application) && $application != '') {
             $this->log->setEventItem('application', $application);
         }
         $formatter = new Zend_Log_Formatter_Simple('%syslog_time% %application%[%pid%]: %priorityName%: %message%' . PHP_EOL);
         $file_logger->setFormatter($formatter);
         $this->log->addWriter($file_logger);
     }
     if (isset($log_config->syslog) && intval($log_config->syslog->enabled) !== 0) {
         $param = array('facility' => $log_config->syslog->facility);
         if (isset($application) && $application != '') {
             $param['application'] = $application;
         }
         $sys_logger = new Zend_Log_Writer_Syslog($param);
         $formatter = new Zend_Log_Formatter_Simple('%priorityName%: %message%' . PHP_EOL);
         $sys_logger->setFormatter($formatter);
         $this->log->addWriter($sys_logger);
     }
     $filter = new Zend_Log_Filter_Priority(intval($log_config->priority));
     $this->log->addFilter($filter);
 }
Esempio n. 4
0
/**
 * Do general setup for a CLI script
 * @param array $pa_additional_parameters Additional command line parameters. You don't have to add
 * --log/-l for the log file and --log-level/-d for the Zend_Log log level. They're always set up automatically
 * @return Zend_Console_Getopt
 */
function caSetupCLIScript($pa_additional_parameters)
{
    require_once __CA_LIB_DIR__ . "/core/Zend/Console/Getopt.php";
    require_once __CA_LIB_DIR__ . "/core/Zend/Log.php";
    require_once __CA_LIB_DIR__ . "/core/Zend/Log/Writer/Stream.php";
    require_once __CA_LIB_DIR__ . "/core/Zend/Log/Writer/Syslog.php";
    require_once __CA_LIB_DIR__ . "/core/Zend/Log/Formatter/Simple.php";
    $va_available_cli_opts = array_merge(array("log|l-s" => "Path to log file. If omitted, we log into the system log. Note that we don't log DEBUG messages into the system log, even when the log level is set to DEBUG.", "log-level|d-s" => "Log level"), $pa_additional_parameters);
    try {
        $o_opts = new Zend_Console_Getopt($va_available_cli_opts);
        $o_opts->parse();
    } catch (Exception $e) {
        die("Invalid command line options: " . $e->getMessage() . PHP_EOL);
    }
    // set up logging
    $o_writer = null;
    if ($vs_log = $o_opts->getOption('log')) {
        // log to file
        try {
            $o_writer = new Zend_Log_Writer_Stream($vs_log);
            $o_writer->setFormatter(new Zend_Log_Formatter_Simple('%timestamp% %priorityName%: %message%' . PHP_EOL));
        } catch (Zend_Log_Exception $e) {
            // error while opening the file (usually permissions)
            $o_writer = null;
            print CLIUtils::textWithColor("Couldn't open log file. Now logging via system log.", "bold_red") . PHP_EOL . PHP_EOL;
        }
    }
    // default: log everything to syslog
    if (!$o_writer) {
        $o_writer = new Zend_Log_Writer_Syslog(array('application' => 'CollectiveAccess CLI', 'facility' => LOG_USER));
        // no need for timespamps in syslog ... the syslog itsself provides that
        $o_writer->setFormatter(new Zend_Log_Formatter_Simple('%priorityName%: %message%' . PHP_EOL));
    }
    // was a loglevel set via command line? -> add filter to Zend logger, otherwise use WARN
    $vs_level = $o_opts->getOption('log-level');
    switch ($vs_level) {
        case 'ERR':
            $o_filter = new Zend_Log_Filter_Priority(Zend_Log::ERR);
            break;
        case 'DEBUG':
            $o_filter = new Zend_Log_Filter_Priority(Zend_Log::DEBUG);
            break;
        case 'INFO':
            $o_filter = new Zend_Log_Filter_Priority(Zend_Log::INFO);
            break;
        case 'WARN':
        default:
            $o_filter = new Zend_Log_Filter_Priority(Zend_Log::WARN);
            break;
    }
    // set up global logger. can be used by importing 'global $g_logger' anywhere, but it's recommended to use the caCLILog() helper instead
    global $g_logger;
    $g_logger = new Zend_Log($o_writer);
    $g_logger->setTimestampFormat('D Y-m-d H:i:s');
    $g_logger->addFilter($o_filter);
    return $o_opts;
}
Esempio n. 5
0
 /**
  *
  */
 protected function _getLoggerInstance()
 {
     static $logger;
     if (!isset($logger)) {
         $logger = new Zend_Log();
         $formatter = new Zend_Log_Formatter_Simple("%timestamp% [" . getmypid() . "]: %priorityName% - %message%" . PHP_EOL);
         // show errror only on quiet mode
         if ($this->_isQuietMode()) {
             $filter = new Zend_Log_Filter_Priority(Zend_Log::ERR, '=');
             $logger->addFilter($filter);
         }
         // show debug only in debug mode
         if (!$this->_isDebugMode()) {
             $filter = new Zend_Log_Filter_Priority(Zend_Log::DEBUG, '!=');
             $logger->addFilter($filter);
         }
         // show notice only in verbose
         if (!$this->_isVerboseMode()) {
             $filter = new Zend_Log_Filter_Priority(Zend_Log::NOTICE, '!=');
             $logger->addFilter($filter);
         }
         // write on standart output
         $writer = new Zend_Log_Writer_Stream('php://output');
         $writer->setFormatter($formatter);
         $logger->addWriter($writer);
         // write on log file or syslog only if debug is disable
         if (!$this->_isDebugMode()) {
             // isset logFilePath write of specific file
             if ($logFilePath = $this->_getLogFilePath()) {
                 $writer = new Zend_Log_Writer_Stream($logFilePath);
                 $writer->setFormatter($formatter);
                 $logger->addWriter($writer);
                 // else write on syslog
             } else {
                 $writer = new Zend_Log_Writer_Syslog(array('application' => $this->_sctiptPath));
                 $writer->setFormatter($formatter);
                 $logger->addWriter($writer);
             }
         }
     }
     return $logger;
 }
Esempio n. 6
0
 protected function addSyslogWriterLogger(Zend_Log $logger, $config)
 {
     if (!(isset($config->active) && $config->active == 1)) {
         return;
     }
     // use always same application and facility, because php error will be written with same application and facility
     $syslogWriter = new \Zend_Log_Writer_Syslog(array('application' => 'cms@' . $this->getDomain(), 'facility' => LOG_LOCAL6));
     if (isset($config->format)) {
         $syslogWriter->setFormatter(new Zend_Log_Formatter_Simple($config->format));
     }
     if (isset($config->level)) {
         $syslogWriter->addFilter((int) $config->level);
     }
     $logger->addWriter($syslogWriter);
 }