Example #1
0
$di['loader'] = $loader;
$di['logger'] = function () use($config) {
    $isCli = PHP_SAPI == 'cli';
    $prefix = $config->application->logsDir . ($isCli ? 'cli-' : '');
    $logger = new MultipleStream();
    $appender = new FileAppender($prefix . 'error.log');
    $appender->setLogLevel(Logger::ERROR);
    $logger->push($appender);
    $appender = new FileAppender($prefix . 'default.log');
    $appender->setLogLevel(Logger::INFO);
    $logger->push($appender);
    if ($isCli) {
        $appender = new ConsoleAppender('php://stderr');
        $formatter = new LineFormatter("%date% [%type%] %message%\n");
        $appender->setFormatter($formatter);
        $appender->setLogLevel(Logger::ERROR);
        $logger->push($appender);
    }
    return $logger;
};
$di['db'] = function () use($di, $config) {
    Model::setup(['notNullValidations' => false]);
    $conn = new DbAdapter($config->database->toArray());
    if (isset($config->eventListeners->db)) {
        $em = $di['eventsManager'];
        foreach ($config->eventListeners->db as $listener => $options) {
            if (is_numeric($listener)) {
                $listener = $options;
                $options = null;
            }
            $em->attach('db', $di->get($listener, [$options]));
Example #2
0
 /**
  * 日志处理
  */
 protected function initLogger()
 {
     $config = $this->config;
     $this->di['logger'] = function () use($config) {
         $logLevel = $this->debug ? Logger::DEBUG : Logger::ERROR;
         if ($config->offsetExists('logger')) {
             try {
                 if ($config->logger->offsetExists('path') == false) {
                     throw new \Exception('logger path not in config.');
                 }
                 $path = $config->logger->path;
                 $path = str_replace('{{date}}', date("Ymd"), $path);
                 if ($config->logger->offsetExists('formatter')) {
                     $formatter = new LineFormatter($config->logger->formatter);
                 } else {
                     $formatter = new LineFormatter('%date%[%type%] - %message%');
                 }
                 $logger = new LoggerFile($path);
                 $logger->setFormatter($formatter);
                 $logger->setLogLevel($logLevel);
                 return $logger;
             } catch (\Exception $e) {
             }
         }
         $logger = new LoggerStream("php://stderr");
         $logger->setLogLevel($logLevel);
         return $logger;
     };
 }