Completed
Push — development ( 86ad30...7b6aa4 )
by Sebastian
05:00
created

include/classes/logger.class.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php 
2
3
class Logger {
4
  private $KLogger;
5
  private $logging = false;
6
  public function __construct($config) {
7
    if ($config['logging']['enabled'] && $config['logging']['level'] > 0) {
8
      $this->KLogger = KLogger::instance($config['logging']['path'] . '/website', $config['logging']['level']);
9
      $this->logging = true;
10
      $this->floatStartTime = microtime(true);
11
    }
12
  }
13
  public function log($strType, $strMessage) {
0 ignored issues
show
log uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
log uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
14
    // Logmask, we add some infos into the KLogger
15
    $strMask = "[ %12s ] [ %8s | %-8s ] [ %7.7s ] : %s";
16
    $strIPAddress = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'unknown';
17
    $strPage = isset($_REQUEST['page']) ? $_REQUEST['page'] : 'none';
18
    $strAction = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'none';
19
    $strMessage = sprintf($strMask, $strIPAddress, $strPage, $strAction, number_format(round((microtime(true) - $this->floatStartTime) * 1000, 2), 2), $strMessage);
20
    if ($this->logging) {
21
      switch ($strType) {
22
      case 'emerg':
23
        $this->KLogger->LogEmerg($strMessage);
24
        break;
25
      case 'alert':
26
        $this->KLogger->LogAlert($strMessage);
27
        break;
28
      case 'crit':
29
        $this->KLogger->LogCrit($strMessage);
30
        break;
31
      case 'error':
32
        $this->KLogger->LogError($strMessage);
33
        break;
34
      case 'warn':
35
        $this->KLogger->LogWarn($strMessage);
36
        break;
37
      case 'notice':
38
        $this->KLogger->LogNotice($strMessage);
39
        break;
40
      case 'info':
41
        $this->KLogger->LogInfo($strMessage);
42
        break;
43
      case 'fatal':
44
        $this->KLogger->LogFatal($strMessage);
45
        break;
46
      case 'debug':
47
        $this->KLogger->LogDebug($strMessage);
48
        break;
49
      case '':
50
        $this->KLogger->LogFatal($strMessage);
51
        break;
52
      }
53
      return true;
54
    } else {
55
      return true;
56
    }
57
  }
58
}
59
$log = new Logger($config);
60