CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Team
    • Issues (Github)
    • YouTube Channel
    • Get Involved
    • Bakery
    • Featured Resources
    • Newsletter
    • Certification
    • My CakePHP
    • CakeFest
    • Facebook
    • Twitter
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.8 Red Velvet API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 3.8
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Cake
    • Auth
      • Storage
    • Cache
      • Engine
    • Collection
      • Iterator
    • Command
    • Console
      • Exception
    • Controller
      • Component
      • Exception
    • Core
      • Configure
        • Engine
      • Exception
      • Retry
    • Database
      • Driver
      • Exception
      • Expression
      • Schema
      • Statement
      • Type
    • Datasource
      • Exception
    • Error
      • Middleware
    • Event
      • Decorator
    • Filesystem
    • Form
    • Http
      • Client
        • Adapter
        • Auth
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
      • Formatter
      • Middleware
      • Parser
    • Log
      • Engine
    • Mailer
      • Exception
      • Transport
    • Network
      • Exception
    • ORM
      • Association
      • Behavior
        • Translate
      • Exception
      • Locator
      • Rule
    • Routing
      • Exception
      • Filter
      • Middleware
      • Route
    • Shell
      • Helper
      • Task
    • TestSuite
      • Fixture
      • Stub
    • Utility
      • Exception
    • Validation
    • View
      • Exception
      • Form
      • Helper
      • Widget
  • None

Classes

  • BaseLog
  • ConsoleLog
  • FileLog
  • SyslogLog
  1: <?php
  2: /**
  3:  * CakePHP(tm) :  Rapid Development Framework (https://cakephp.org)
  4:  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * For full copyright and license information, please see the LICENSE.txt
  8:  * Redistributions of files must retain the above copyright notice.
  9:  *
 10:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 11:  * @link          https://cakefoundation.org CakePHP(tm) Project
 12:  * @since         1.3.0
 13:  * @license       https://opensource.org/licenses/mit-license.php MIT License
 14:  */
 15: namespace Cake\Log\Engine;
 16: 
 17: use Cake\Core\Configure;
 18: use Cake\Utility\Text;
 19: 
 20: /**
 21:  * File Storage stream for Logging. Writes logs to different files
 22:  * based on the level of log it is.
 23:  */
 24: class FileLog extends BaseLog
 25: {
 26:     /**
 27:      * Default config for this class
 28:      *
 29:      * - `levels` string or array, levels the engine is interested in
 30:      * - `scopes` string or array, scopes the engine is interested in
 31:      * - `file` Log file name
 32:      * - `path` The path to save logs on.
 33:      * - `size` Used to implement basic log file rotation. If log file size
 34:      *   reaches specified size the existing file is renamed by appending timestamp
 35:      *   to filename and new log file is created. Can be integer bytes value or
 36:      *   human readable string values like '10MB', '100KB' etc.
 37:      * - `rotate` Log files are rotated specified times before being removed.
 38:      *   If value is 0, old versions are removed rather then rotated.
 39:      * - `mask` A mask is applied when log files are created. Left empty no chmod
 40:      *   is made.
 41:      *
 42:      * @var array
 43:      */
 44:     protected $_defaultConfig = [
 45:         'path' => null,
 46:         'file' => null,
 47:         'types' => null,
 48:         'levels' => [],
 49:         'scopes' => [],
 50:         'rotate' => 10,
 51:         'size' => 10485760, // 10MB
 52:         'mask' => null,
 53:     ];
 54: 
 55:     /**
 56:      * Path to save log files on.
 57:      *
 58:      * @var string|null
 59:      */
 60:     protected $_path;
 61: 
 62:     /**
 63:      * The name of the file to save logs into.
 64:      *
 65:      * @var string|null
 66:      */
 67:     protected $_file;
 68: 
 69:     /**
 70:      * Max file size, used for log file rotation.
 71:      *
 72:      * @var int|null
 73:      */
 74:     protected $_size;
 75: 
 76:     /**
 77:      * Sets protected properties based on config provided
 78:      *
 79:      * @param array $config Configuration array
 80:      */
 81:     public function __construct(array $config = [])
 82:     {
 83:         parent::__construct($config);
 84: 
 85:         if (!empty($this->_config['path'])) {
 86:             $this->_path = $this->_config['path'];
 87:         }
 88:         if ($this->_path !== null &&
 89:             Configure::read('debug') &&
 90:             !is_dir($this->_path)
 91:         ) {
 92:             mkdir($this->_path, 0775, true);
 93:         }
 94: 
 95:         if (!empty($this->_config['file'])) {
 96:             $this->_file = $this->_config['file'];
 97:             if (substr($this->_file, -4) !== '.log') {
 98:                 $this->_file .= '.log';
 99:             }
100:         }
101: 
102:         if (!empty($this->_config['size'])) {
103:             if (is_numeric($this->_config['size'])) {
104:                 $this->_size = (int)$this->_config['size'];
105:             } else {
106:                 $this->_size = Text::parseFileSize($this->_config['size']);
107:             }
108:         }
109:     }
110: 
111:     /**
112:      * Implements writing to log files.
113:      *
114:      * @param string $level The severity level of the message being written.
115:      *    See Cake\Log\Log::$_levels for list of possible levels.
116:      * @param string $message The message you want to log.
117:      * @param array $context Additional information about the logged message
118:      * @return bool success of write.
119:      */
120:     public function log($level, $message, array $context = [])
121:     {
122:         $message = $this->_format($message, $context);
123:         $output = date('Y-m-d H:i:s') . ' ' . ucfirst($level) . ': ' . $message . "\n";
124:         $filename = $this->_getFilename($level);
125:         if ($this->_size) {
126:             $this->_rotateFile($filename);
127:         }
128: 
129:         $pathname = $this->_path . $filename;
130:         $mask = $this->_config['mask'];
131:         if (!$mask) {
132:             return file_put_contents($pathname, $output, FILE_APPEND);
133:         }
134: 
135:         $exists = file_exists($pathname);
136:         $result = file_put_contents($pathname, $output, FILE_APPEND);
137:         static $selfError = false;
138: 
139:         if (!$selfError && !$exists && !chmod($pathname, (int)$mask)) {
140:             $selfError = true;
141:             trigger_error(vsprintf(
142:                 'Could not apply permission mask "%s" on log file "%s"',
143:                 [$mask, $pathname]
144:             ), E_USER_WARNING);
145:             $selfError = false;
146:         }
147: 
148:         return $result;
149:     }
150: 
151:     /**
152:      * Get filename
153:      *
154:      * @param string $level The level of log.
155:      * @return string File name
156:      */
157:     protected function _getFilename($level)
158:     {
159:         $debugTypes = ['notice', 'info', 'debug'];
160: 
161:         if ($this->_file) {
162:             $filename = $this->_file;
163:         } elseif ($level === 'error' || $level === 'warning') {
164:             $filename = 'error.log';
165:         } elseif (in_array($level, $debugTypes)) {
166:             $filename = 'debug.log';
167:         } else {
168:             $filename = $level . '.log';
169:         }
170: 
171:         return $filename;
172:     }
173: 
174:     /**
175:      * Rotate log file if size specified in config is reached.
176:      * Also if `rotate` count is reached oldest file is removed.
177:      *
178:      * @param string $filename Log file name
179:      * @return bool|null True if rotated successfully or false in case of error.
180:      *   Null if file doesn't need to be rotated.
181:      */
182:     protected function _rotateFile($filename)
183:     {
184:         $filePath = $this->_path . $filename;
185:         clearstatcache(true, $filePath);
186: 
187:         if (!file_exists($filePath) ||
188:             filesize($filePath) < $this->_size
189:         ) {
190:             return null;
191:         }
192: 
193:         $rotate = $this->_config['rotate'];
194:         if ($rotate === 0) {
195:             $result = unlink($filePath);
196:         } else {
197:             $result = rename($filePath, $filePath . '.' . time());
198:         }
199: 
200:         $files = glob($filePath . '.*');
201:         if ($files) {
202:             $filesToDelete = count($files) - $rotate;
203:             while ($filesToDelete > 0) {
204:                 unlink(array_shift($files));
205:                 $filesToDelete--;
206:             }
207:         }
208: 
209:         return $result;
210:     }
211: }
212: 
Follow @CakePHP
#IRC
OpenHub
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Logos & Trademarks
  • Community
  • Team
  • Issues (Github)
  • YouTube Channel
  • Get Involved
  • Bakery
  • Featured Resources
  • Newsletter
  • Certification
  • My CakePHP
  • CakeFest
  • Facebook
  • Twitter
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs