Hawk - PHP documentation
  • Namespace
  • Class
  • Tree

Namespaces

  • Hawk
    • View
      • Plugins

Classes

  • Hawk\App
  • Hawk\ButtonInput
  • Hawk\Cache
  • Hawk\CheckboxInput
  • Hawk\ColorInput
  • Hawk\Conf
  • Hawk\Controller
  • Hawk\Crypto
  • Hawk\DatabaseSessionHandler
  • Hawk\DatetimeInput
  • Hawk\DB
  • Hawk\DBExample
  • Hawk\DeleteInput
  • Hawk\Dialogbox
  • Hawk\EmailInput
  • Hawk\ErrorHandler
  • Hawk\Event
  • Hawk\FileInput
  • Hawk\FileSystem
  • Hawk\FloatInput
  • Hawk\Form
  • Hawk\FormFieldset
  • Hawk\FormInput
  • Hawk\GenericModel
  • Hawk\GifImage
  • Hawk\HawkApi
  • Hawk\HawkUpdater
  • Hawk\HiddenInput
  • Hawk\HtmlInput
  • Hawk\HTTPRequest
  • Hawk\Icon
  • Hawk\Image
  • Hawk\IntegerInput
  • Hawk\ItemList
  • Hawk\ItemListField
  • Hawk\JpegImage
  • Hawk\Lang
  • Hawk\Language
  • Hawk\LeftSidebarTab
  • Hawk\Less
  • Hawk\Logger
  • Hawk\Mail
  • Hawk\MenuItem
  • Hawk\Model
  • Hawk\NoSidebarTab
  • Hawk\NumberInput
  • Hawk\ObjectInput
  • Hawk\Option
  • Hawk\Panel
  • Hawk\PasswordInput
  • Hawk\Permission
  • Hawk\Plugin
  • Hawk\PluginInstaller
  • Hawk\PngImage
  • Hawk\ProfileQuestion
  • Hawk\ProfileQuestionValue
  • Hawk\RadioInput
  • Hawk\Request
  • Hawk\Response
  • Hawk\RightSidebarTab
  • Hawk\Role
  • Hawk\RolePermission
  • Hawk\Route
  • Hawk\Router
  • Hawk\SelectInput
  • Hawk\Session
  • Hawk\Singleton
  • Hawk\SubmitInput
  • Hawk\Tabs
  • Hawk\TextareaInput
  • Hawk\TextInput
  • Hawk\Theme
  • Hawk\TimeInput
  • Hawk\Upload
  • Hawk\User
  • Hawk\View
  • Hawk\View\Plugins\Accordion
  • Hawk\View\Plugins\Button
  • Hawk\View\Plugins\Form
  • Hawk\View\Plugins\Icon
  • Hawk\View\Plugins\Import
  • Hawk\View\Plugins\Panel
  • Hawk\View\Plugins\Tabs
  • Hawk\View\Plugins\Text
  • Hawk\View\Plugins\Uri
  • Hawk\View\Plugins\Widget
  • Hawk\ViewPlugin
  • Hawk\Widget
  • Hawk\WysiwygInput

Traits

  • Hawk\Utils

Exceptions

  • Hawk\AppStopException
  • Hawk\DBExampleException
  • Hawk\DBException
  • Hawk\FileSystemException
  • Hawk\HawkApiException
  • Hawk\ImageException
  • Hawk\MailException
  • Hawk\UploadException
  • Hawk\ViewException
  1 <?php
  2 /**
  3  * Logger.php
  4  *
  5  * @author  Elvyrra
  6  * @license http://rem.mit-license.org/ MIT
  7  */
  8 
  9 namespace Hawk;
 10 
 11 /**
 12  * This class is used to log data in /logs directory.
 13  * You can use it to log the action of the users on the application, for example to make stats.
 14  *
 15  * @package Core
 16  */
 17 final class Logger extends Singleton{
 18     const LEVEL_DEBUG = 'debug';
 19     const LEVEL_INFO = 'info';
 20     const LEVEL_NOTICE = 'notice';
 21     const LEVEL_WARNING = 'warning';
 22     const LEVEL_ERROR = 'error';
 23 
 24     const MAX_FILE_SIZE = 204800;
 25     const MAX_FILES_BY_LEVEL = 9;
 26 
 27     /**
 28      * The logger instance
 29      *
 30      * @var Logger
 31      */
 32     protected static $instance;
 33 
 34     /**
 35      * The file resources
 36      */
 37     private $resources = array();
 38 
 39 
 40     /**
 41      * Open a log file
 42      *
 43      * @param string $level The level of the log file
 44      */
 45     private function open($level){
 46         $basename = $level . '.log';
 47         $filename = LOG_DIR . $basename;
 48 
 49         if(is_file($filename) && filesize($filename) >= self::MAX_FILE_SIZE) {
 50             // Archive the last file and create a new one
 51 
 52             // rename all archives already existing (keep only last 9 archives)
 53             $archives = array_reverse(glob($filename . '.*.zip'));
 54             foreach($archives as $archive){
 55                 preg_match('/^' . preg_quote($basename, '/') . '\.(\d+)\.zip$/', basename($archive), $match);
 56                 if($match[1] > self::MAX_FILES_BY_LEVEL) {
 57                     unlink($archive);
 58                 }
 59                 else{
 60                     rename($archive, $filename . '.' . ($match[1] + 1) . '.zip');
 61                 }
 62             }
 63 
 64             // Create the new archive
 65             $zip = new \ZipArchive;
 66             $zip->open($filename . '.0.zip', \ZipArchive::CREATE);
 67             $zip->addFile($filename);
 68             $zip->close();
 69 
 70             unlink($filename);
 71         }
 72         $this->resources[$level] = fopen($filename, 'a+');
 73     }
 74 
 75     /**
 76      * Write log
 77      *
 78      * @param string $level   The log level : 'debug', 'info', 'notice', 'warning', 'error'
 79      * @param string $message The message to write
 80      */
 81     private function write($level, $message){
 82 
 83         if(defined('ENABLE_LOG') && !ENABLE_LOG) {
 84             return;
 85         }
 86 
 87         if(empty($this->resources[$level])) {
 88             $this->open($level);
 89         }
 90 
 91         $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
 92         $trace = (object) $trace[1];
 93 
 94         $data = array(
 95             'date' => date_create()->format('Y-m-d H:i:s'),
 96             'uri' => App::request()->getUri(),
 97             'trace' => $trace->file . ':' . $trace->line,
 98             'message' => $message,
 99         );
100 
101         $input =  implode(' - ', $data) . PHP_EOL;
102         fwrite($this->resources[$level], $input);
103     }
104 
105     /**
106      * Log info data.
107      * Use this function to log user action like form submission
108      *
109      * @param string $message The message to write
110      */
111     public function info($message){
112         $this->write(self::LEVEL_INFO, $message);
113     }
114 
115     /**
116      * Log debug data.
117      * This function is used to log script execution steps
118      *
119      * @param string $message The message to write
120      */
121     public function debug($message){
122         $this->write(self::LEVEL_DEBUG, $message);
123     }
124 
125     /**
126      * Log notice data.
127      * This function is used to log anormal non blocking usage
128      *
129      * @param string $message The message to write
130      */
131     public function notice($message){
132         $this->write(self::LEVEL_NOTICE, $message);
133     }
134 
135     /**
136      * Log warning data.
137      * This function is used to log actions that didn't work because of user bad action (eg form badly completed)
138      *
139      * @param string $message The message to write
140      */
141     public function warning($message){
142         $this->write(self::LEVEL_WARNING, $message);
143     }
144 
145     /**
146      * Log error data.
147      * This function is used to log actions that didn't work because of a script error
148      *
149      * @param string $message The message to write
150      */
151     public function error($message){
152         $this->write(self::LEVEL_ERROR, $message);
153     }
154 }
Hawk - PHP documentation API documentation generated by ApiGen