Combu Server  3.1.1
PHP API Documentation
AppLog.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
10 class AppLog {
11 
12  private static $FilePath = null;
13  private static $MaxFileSize = 0;
14 
15  public static function Initialize($filePath = null, $maxFileSize = null) {
16  if ($filePath) {
17  self::$FilePath = $filePath;
18  } else if (defined('LOG_FILENAME')) {
19  self::$FilePath = LOG_FOLDER . LOG_FILENAME;
20  } else {
21  self::$FilePath = LOG_FOLDER . "app.log";
22  }
23  if ($maxFileSize !== null) {
24  self::$MaxFileSize = $maxFileSize;
25  } else if (defined('LOG_MAXFILESIZE')) {
26  self::$MaxFileSize = LOG_MAXFILESIZE;
27  }
28  }
29 
30  private static function WriteToFile($text) {
31  $dir = dirname(self::$FilePath);
32  if (!file_exists($dir) && is_writable(dirname($dir))) {
33  try
34  {
35  mkdir($dir);
36  } catch (Exception $ex) {
37 
38  }
39  }
40  if (file_exists($dir) && is_writable($dir)) {
41  if (self::$MaxFileSize > 0) {
42  if (@filesize(self::$FilePath) > self::$MaxFileSize) {
43  $ext = Utils::GetFilenameExtension(basename(self::$FilePath));
44  $suffix = "_" . date("d-m-Y_H-i-s") . $ext;
45  $bakFile = substr(self::$FilePath, 0, strlen($ext) - 1) . $suffix;
46  @rename(self::$FilePath, $bakFile);
47  }
48  }
49  file_put_contents(self::$FilePath, $text, FILE_APPEND);
50  }
51  }
52 
53  private static function SanatizeText($log) {
54  $text = $log;
55  $text = str_replace("\n", " ", $text);
56  $text = str_replace("\r", " ", $text);
57  $text = str_replace(" ", " ", $text);
58  $text = trim($text);
59  return $text;
60  }
61 
68  private static function AppendLog($prefix, $log) {
69  global $AppId;
70  $text = ($AppId->IsValid() ? "(" . $AppId->Name . ") " : "") .
71  date("d/m/Y H:i:s") .
72  " [$prefix] " .
73  self::SanatizeText($log) .
74  "\n";
75  self::WriteToFile($text);
76  }
77 
78  public static function Info($log) {
79  self::AppendLog("INFO", $log);
80  }
81 
82  public static function Warning($log) {
83  self::AppendLog("WARNING", $log);
84  }
85 
86  public static function Error($log) {
87  self::AppendLog("ERROR", $log);
88  }
89 
90 }
static Initialize($filePath=null, $maxFileSize=null)
Definition: AppLog.php:15
static Warning($log)
Definition: AppLog.php:82
static Info($log)
Definition: AppLog.php:78
static Error($log)
Definition: AppLog.php:86
Definition: Account.php:3